// Definimos los pines para el LED RGB
const int redPin = 18;
const int greenPin = 19;
const int bluePin = 21;
// Pin del potenciómetro
#define POT_PIN 36
// Definimos los canales PWM para cada color
const int redChannel = 0;
const int greenChannel = 1;
const int blueChannel = 2;
// Frecuencia y resolución del PWM
const int freq = 5000;
const int resolution = 8;
void setup() {
// Configuramos los pines del LED RGB como salidas PWM
ledcSetup(redChannel, freq, resolution);
ledcAttachPin(redPin, redChannel);
ledcSetup(greenChannel, freq, resolution);
ledcAttachPin(greenPin, greenChannel);
ledcSetup(blueChannel, freq, resolution);
ledcAttachPin(bluePin, blueChannel);
// Configuramos el pin del potenciómetro como entrada
pinMode(POT_PIN, INPUT);
}
void loop() {
// Leemos el valor del potenciómetro
int potValue = analogRead(POT_PIN);
// Mapeamos el valor del potenciómetro (0-4095) a valores de PWM (0-255)
int brightness = map(potValue, 0, 4095, 0, 255);
// Para este ejemplo, simplemente dividimos el rango en tres partes para los tres colores
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
if (brightness <= 85) {
redValue = brightness * 3;
} else if (brightness <= 170) {
greenValue = (brightness - 85) * 3;
} else {
blueValue = (brightness - 170) * 3;
}
// Establecemos los valores PWM a los canales del LED RGB
ledcWrite(redChannel, redValue);
ledcWrite(greenChannel, greenValue);
ledcWrite(blueChannel, blueValue);
// Pequeño retardo para estabilizar la lectura del potenciómetro
delay(10);
}