I can't get this working - what am I missing? Also tried using two different Timers for this. Both tasks are running as far as I can tell, but the second output stays shut. I tried at first fading led1 and as it finishes fading led2 without using tasks with the same configuration and that worked.
Code: Select all
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/ledc.h"
#include "esp_err.h"
#include "esp_log.h"
#define LEDC_FAST_FADE_TIME (1000)
#define LEDC_SLOW_FADE_TIME (1000)
#define LEDC_DUTY (4000)
#define LED_GREEN_GPIO (GPIO_NUM_22)
#define LED_RED_GPIO (GPIO_NUM_21)
ledc_channel_config_t green_led_conf;
ledc_channel_config_t red_led_conf;
static const char *TAG = "#LOG:";
static void blink_red (void)
{
ledc_set_fade_with_time(red_led_conf.speed_mode,red_led_conf.channel, LEDC_DUTY, LEDC_SLOW_FADE_TIME);
ledc_fade_start(red_led_conf.speed_mode, red_led_conf.channel, LEDC_FADE_NO_WAIT);
vTaskDelay(LEDC_SLOW_FADE_TIME / portTICK_PERIOD_MS);
ledc_set_fade_with_time(red_led_conf.speed_mode, red_led_conf.channel, 0, LEDC_SLOW_FADE_TIME);
ledc_fade_start(red_led_conf.speed_mode, red_led_conf.channel, LEDC_FADE_NO_WAIT);
vTaskDelay(LEDC_SLOW_FADE_TIME / portTICK_PERIOD_MS);
}
static void blink_green (void)
{
ledc_set_fade_with_time(green_led_conf.speed_mode, green_led_conf.channel, LEDC_DUTY, LEDC_FAST_FADE_TIME);
ledc_fade_start(green_led_conf.speed_mode, green_led_conf.channel, LEDC_FADE_NO_WAIT);
vTaskDelay(LEDC_FAST_FADE_TIME / portTICK_PERIOD_MS);
ledc_set_fade_with_time(green_led_conf.speed_mode, green_led_conf.channel, 0, LEDC_FAST_FADE_TIME);
ledc_fade_start(green_led_conf.speed_mode, green_led_conf.channel, LEDC_FADE_NO_WAIT);
vTaskDelay(LEDC_FAST_FADE_TIME / portTICK_PERIOD_MS);
}
static void blink_red_task (void *pvParameter)
{
while (1)
{
ESP_LOGI(TAG, "RED LED IS BLINKING.. TASK RUNNING");
blink_red();
}
}
static void blink_green_fast_task (void *pvParameter)
{
while (1)
{
ESP_LOGI(TAG, "GREEN LED IS BLINKING FAST.. TASK RUNNING");
blink_green();
}
}
static void init_ledc (void)
{
ledc_timer_config_t ledc_timer = {
.bit_num = LEDC_TIMER_13_BIT, // resolution
.freq_hz = 5000, // frequency of PWM signal
.speed_mode = LEDC_HIGH_SPEED_MODE, // timer mode
.timer_num = LEDC_TIMER_0 // timer index
};
// Set configuration of timer0 for high speed channels
ledc_timer_config(&ledc_timer);
// Prepare channel configs
ledc_channel_config_t green_led_conf =
{
.channel = LEDC_CHANNEL_0,
.duty = 0,
.gpio_num = (GPIO_NUM_22),//LED_GREEN_GPIO,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE
};
ledc_channel_config_t red_led_conf =
{
.channel = LEDC_CHANNEL_1,
.duty = 0,
.gpio_num = (GPIO_NUM_21), //LED_RED_GPIO,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE
};
// Set LED Controller with previously prepared configuration
ledc_channel_config(&red_led_conf);
ledc_channel_config(&green_led_conf);
// Initialize fade service.
ledc_fade_func_install(0);
}
void app_main()
{
init_ledc();
blink_red();
blink_green();
xTaskCreate(&blink_red_task, "blink_red_task", 2048, NULL, 5, NULL);
vTaskDelay(100);
xTaskCreate(&blink_green_fast_task, "blink_green_fast_task", 2048, NULL, 5, NULL);
}
Any suggestions are welcome Thank you!