// Definición de pines
const int pinLedRojo = 17;
const int pinLedAzul = 18;
const int pinLedVerde = 19;
const int pinLedAmarillo = 32;
const int pinPotenciometro = 34;
// Tiempos de encendido y apagado (milisegundos)
const int tiempoEncendidoRojo = 300;
const int tiempoApagadoRojo = 1000;
const int tiempoEncendidoAzul = 400;
const int tiempoApagadoAzul = 400;
const int tiempoEncendidoVerde = 2300;
const int tiempoApagadoVerde = 700;
// Prototipos de las funciones de las tareas
void tareaLedRojo(void *pvParameters);
void tareaLedAzul(void *pvParameters);
void tareaLedVerde(void *pvParameters);
void tareaControlPWM(void *pvParameters);
void setup() {
Serial.begin(115200);
// Configurar pines de LEDs como salidas
pinMode(pinLedRojo, OUTPUT);
pinMode(pinLedAzul, OUTPUT);
pinMode(pinLedVerde, OUTPUT);
pinMode(pinLedAmarillo, OUTPUT);
// Configuración del PWM para el LED amarillo
ledcAttachPin(pinLedAmarillo, 0);
ledcSetup(0, 5000, 8);
// Crear tareas y asignarlas a los núcleos
xTaskCreatePinnedToCore(tareaLedRojo, "TareaLedRojo", 1000, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(tareaLedAzul, "TareaLedAzul", 1000, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(tareaLedVerde, "TareaLedVerde", 1000, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(tareaControlPWM, "TareaControlPWM", 1000, NULL, 1, NULL, 0);
}
void loop() {
int valorPot = analogRead(pinPotenciometro);
float voltaje = (valorPot / 4095.0) * 3.3;
Serial.print("Voltaje Potenciometro: ");
Serial.println(voltaje, 3);
delay(1000);
}
// Funciones de las tareas
void tareaLedRojo(void *pvParameters) {
for (;;) {
digitalWrite(pinLedRojo, HIGH);
vTaskDelay(tiempoEncendidoRojo / portTICK_PERIOD_MS);
digitalWrite(pinLedRojo, LOW);
vTaskDelay(tiempoApagadoRojo / portTICK_PERIOD_MS);
}
}
void tareaLedAzul(void *pvParameters) {
for (;;) {
digitalWrite(pinLedAzul, HIGH);
vTaskDelay(tiempoEncendidoAzul / portTICK_PERIOD_MS);
digitalWrite(pinLedAzul, LOW);
vTaskDelay(tiempoApagadoAzul / portTICK_PERIOD_MS);
}
}
void tareaLedVerde(void *pvParameters) {
for (;;) {
digitalWrite(pinLedVerde, HIGH);
vTaskDelay(tiempoEncendidoVerde / portTICK_PERIOD_MS);
digitalWrite(pinLedVerde, LOW);
vTaskDelay(tiempoApagadoVerde / portTICK_PERIOD_MS);
}
}
void tareaControlPWM(void *pvParameters) {
for (;;) {
int valorPot = analogRead(pinPotenciometro);
int brilloLed = map(valorPot, 0, 4095, 0, 255);
ledcWrite(0, brilloLed);
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}