#include <Arduino.h>
#include "driver/ledc.h"
#define pwmChannel 0
#define LedGChannel 1
#define LedRChannel 2
#define LedYChannel 3
#define pinBtnB3 2
#define pinBtnB4 4
#define pinLED1 34
#define pinLED2 33
#define pinLED3 25
#define freqPWM 5000
#define freqPWM1 1000
#define freqPWM2 500
#define resolution 8
volatile bool btnB3Pressed = false;
volatile bool btnB4Pressed = false;
volatile unsigned long lastDebounceTimeB3 = 0;
volatile unsigned long lastDebounceTimeB4 = 0;
unsigned long debounceDelay = 50;
int currentLED = 1;
int brightness = 0;
void IRAM_ATTR ISR_btn3() {
if ((millis() - lastDebounceTimeB3) > debounceDelay) {
btnB3Pressed = true;
}
lastDebounceTimeB3 = millis();
}
void IRAM_ATTR ISR_btn4() {
if ((millis() - lastDebounceTimeB4) > debounceDelay) {
btnB4Pressed = true;
}
lastDebounceTimeB4 = millis();
}
ledc_channel_config_t channel_configs[3]; // Configuraciones PWM para 3 LEDs
void setup() {
pinMode(pinBtnB3, INPUT_PULLUP);
pinMode(pinBtnB4, INPUT_PULLUP);
pinMode(pinLED1, OUTPUT);
pinMode(pinLED2, OUTPUT);
pinMode(pinLED3, OUTPUT);
// Configuración PWM para los tres LEDs
channel_configs[1].gpio_num = pinLED1;
channel_configs[2].gpio_num = pinLED2;
channel_configs[3].gpio_num = pinLED3;
for (int i = 0; i < 3; i++) {
channel_configs[i].speed_mode = LEDC_HIGH_SPEED_MODE;
channel_configs[i].channel = static_cast<ledc_channel_t>(i);
channel_configs[i].intr_type = LEDC_INTR_DISABLE;
channel_configs[i].timer_sel = LEDC_TIMER_0;
channel_configs[i].duty = 0; // Inicialmente apagado
ledc_channel_config(&channel_configs[i]);
}
// Configuración del timer PWM
ledc_timer_config_t timer_config;
timer_config.speed_mode = LEDC_HIGH_SPEED_MODE;
timer_config.duty_resolution = LEDC_TIMER_8_BIT; // Reducimos la resolución para ajustar el brillo en un rango de 0 a 255
timer_config.timer_num = LEDC_TIMER_0;
timer_config.freq_hz = freqPWM; // Frecuencia de PWM
ledc_timer_config(&timer_config);
attachInterrupt(digitalPinToInterrupt(pinBtnB3), ISR_btn3, FALLING);
attachInterrupt(digitalPinToInterrupt(pinBtnB4), ISR_btn4, FALLING);
}
void loop() {
if (btnB3Pressed) {
// Cambiar el LED seleccionado
currentLED = (currentLED % 3) + 1;
btnB3Pressed = false;
}
if (btnB4Pressed) {
// Ajustar el brillo del LED seleccionado
if (brightness < 255) {
brightness += 25;
} else {
brightness = 0;
}
btnB4Pressed = false;
}
// Actualizar el brillo del LED seleccionado
ledc_set_duty(LEDC_HIGH_SPEED_MODE, static_cast<ledc_channel_t>(currentLED - 1), brightness);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, static_cast<ledc_channel_t>(currentLED - 1));
delay(100); // Pequeño retardo para evitar lecturas erráticas del botón
}