#define LED1 3
#define LED2 4
#define LED3 5
#define LED4 6
#define PIN_POTENCIOMETRO A0
#define NUM_LEDS 4
#define pul1 2
int estado = 0;
int estadoActualLed = 0;
unsigned long tiempoAnterior = 0; // Declaración de la variable
void setup() {
// Inicializar pines de LEDs como salida
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(LED1 + i, OUTPUT);
}
// Inicializar pin del potenciómetro como entrada
pinMode(PIN_POTENCIOMETRO, INPUT);
// Iniciar comunicación serial
Serial.begin(9600);
}
void loop() {
if (digitalRead(pul1) == HIGH) {
estado = !estado;
delay(500);
}
if (estado == 1) {
// Leer valor del potenciómetro
int valorPotenciometro = analogRead(PIN_POTENCIOMETRO);
// Calcular tiempo de retardo según valor del potenciómetro
int tiempoRetardo = map(valorPotenciometro, 0, 1023, 200, 2500);
// Si ha pasado el tiempo de retardo
if (millis() - tiempoAnterior >= tiempoRetardo) {
// Encender el nuevo LED
digitalWrite(LED1 + estadoActualLed, HIGH);
// Avanzar al siguiente LED
estadoActualLed = (estadoActualLed + 1) % NUM_LEDS;
// Actualizar tiempo anterior
tiempoAnterior = millis();
// Si se ha llegado al final de la secuencia
if (estadoActualLed == 0) {
// Reiniciar la secuencia
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(LED1 + i, LOW);
}
estadoActualLed = 0;
}
}
// Mostrar información por el puerto serial
Serial.print("Valor potenciómetro: ");
Serial.print(valorPotenciometro);
Serial.print(" - Retardo: ");
Serial.println(tiempoRetardo);
}else{
int valorPotenciometro = analogRead(PIN_POTENCIOMETRO);
// Calcular tiempo de retardo según valor del potenciómetro
int tiempoRetardo = map(valorPotenciometro, 0, 1023, 200, 2500);
// Si ha pasado el tiempo de retardo
if (millis() - tiempoAnterior >= tiempoRetardo) {
// Encender el nuevo LED
digitalWrite(LED4 + estadoActualLed, HIGH);
// Avanzar al siguiente LED
estadoActualLed = (estadoActualLed - 1) % NUM_LEDS;
// Actualizar tiempo anterior
tiempoAnterior = millis();
// Si se ha llegado al final de la secuencia
delay(1000);
if (estadoActualLed == 0) {
// Reiniciar la secuencia
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(LED1 + i, LOW);
}
estadoActualLed = 0;
}
}
}
}