This is one of those "used to work, but I turned my back on it and it broke" situations. A few months ago, I successfully used the LED control functions to control a transducer (a US Electronics USE8530DT23SLF). I had it working (volume at least; didn't care about tone), and commented it out because it was annoying. Tried to turn it back on, and...you guessed it.
Here's my code; can anyone see something I'm doing wrong?
Code: Select all
#define BUZZER_TIMER (LEDC_TIMER_1)
#define BUZZER_CHANNEL (LEDC_CHANNEL_1)
#define BUZZER_GPIO_NBR (GPIO_NUM_5)
#define BUZZER_GPIO_PIN (GPIO_SEL_5)
#define BUZZER_FREQ (2670)
#define BUZZER_TIMER_RESOLUTION (LEDC_TIMER_10_BIT)
#define BUZZER_MAX_DUTY ((1 << (BUZZER_TIMER_RESOLUTION)) - 1)
// set the "on" duty (volume) to 1/2 the max.
#define BUZZER_ON_DUTY (BUZZER_MAX_DUTY / 2)
#define BUZZER_OFF_DUTY (0)
#define BUZZER_QUEUE_TIMEOUT (10) // ticks; = .1 second.
const ledc_timer_config_t buzzer_timer =
{
LEDC_HIGH_SPEED_MODE,
{BUZZER_TIMER_RESOLUTION},
BUZZER_TIMER,
BUZZER_FREQ
};
const ledc_channel_config_t buzzer_channel =
{
BUZZER_GPIO_NBR,
LEDC_HIGH_SPEED_MODE,
BUZZER_CHANNEL,
LEDC_INTR_DISABLE,
BUZZER_TIMER,
0,
LEDC_HPOINT_HSCH0
};
void Buzzer::setDuty(uint32_t duty)
{
ESP_ERROR_CHECK(ledc_set_duty(LEDC_HIGH_SPEED_MODE, BUZZER_CHANNEL, duty));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_HIGH_SPEED_MODE, BUZZER_CHANNEL));
ESP_LOGI(TAG, "buzzer duty set to %d.", ledc_get_duty(LEDC_HIGH_SPEED_MODE, BUZZER_CHANNEL));
}
...
for (unsigned int i = BUZZER_ON_DUTY; i < BUZZER_MAX_DUTY; i += 10)
{
setDuty(i);
vTaskDelay(10);
}