#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/ledc.h"
#include "esp_err.h"
#define LEDC_TIMER LEDC_TIMER_0
#define LEDC_MODE LEDC_LOW_SPEED_MODE
#define LEDC_DUTY_RES LEDC_TIMER_8_BIT
#define LEDC_FREQUENCY 500
#define LEDC_CHANNEL_R LEDC_CHANNEL_0
#define LEDC_CHANNEL_G LEDC_CHANNEL_1
#define LEDC_CHANNEL_B LEDC_CHANNEL_2
#define LEDC_GPIO_R 33
#define LEDC_GPIO_G 25
#define LEDC_GPIO_B 26
static void set_color(uint8_t r, uint8_t g, uint8_t b)
{
ledc_set_duty(LEDC_MODE, LEDC_CHANNEL_R, r);
ledc_update_duty(LEDC_MODE, LEDC_CHANNEL_R);
ledc_set_duty(LEDC_MODE, LEDC_CHANNEL_G, g);
ledc_update_duty(LEDC_MODE, LEDC_CHANNEL_G);
ledc_set_duty(LEDC_MODE, LEDC_CHANNEL_B, b);
ledc_update_duty(LEDC_MODE, LEDC_CHANNEL_B);
}
void app_main(void)
{
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_MODE,
.timer_num = LEDC_TIMER,
.duty_resolution = LEDC_DUTY_RES,
.freq_hz = LEDC_FREQUENCY,
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t ch[] = {
{ .channel = LEDC_CHANNEL_R, .duty = 0, .gpio_num = LEDC_GPIO_R, .speed_mode = LEDC_MODE, .hpoint = 0, .timer_sel = LEDC_TIMER },
{ .channel = LEDC_CHANNEL_G, .duty = 0, .gpio_num = LEDC_GPIO_G, .speed_mode = LEDC_MODE, .hpoint = 0, .timer_sel = LEDC_TIMER },
{ .channel = LEDC_CHANNEL_B, .duty = 0, .gpio_num = LEDC_GPIO_B, .speed_mode = LEDC_MODE, .hpoint = 0, .timer_sel = LEDC_TIMER },
};
for (int i = 0; i < 3; i++)
ledc_channel_config(&ch[i]);
uint8_t r = 255, g = 0, b = 0;
int state = 0;
while (1) {
switch (state) {
case 0: g++; if (g == 255) state++; break; // Красный -> Жёлтый
case 1: r--; if (r == 0) state++; break; // Жёлтый -> Зелёный
case 2: b++; if (b == 255) state++; break; // Зелёный -> Голубой
case 3: g--; if (g == 0) state++; break; // Голубой -> Синий
case 4: r++; if (r == 255) state++; break; // Синий -> Фиолетовый
case 5: b--; if (b == 0) state = 0; break; // Фиолетовый -> Красный
}
set_color(r, g, b);
vTaskDelay(pdMS_TO_TICKS(10));
}
}