#include <Arduino.h>
#include <stdbool.h>
#define NUM_LEDS 10
#define MAX_NIVEL_VEL 10
// Pines asignados a los LEDs (D2 hasta D11)
const int LED_PIN[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
// --- PINES DIGITALES REALES Y SEGUROS EN WOKWI ---
#define BTN_MENU_PIN 12 // Botón de arriba en el pin D12 (MISO)
#define BTN_FAST_PIN 13 // Botón del medio en el pin D13 (SCK)
#define BTN_SLOW_PIN 14 // Botón de abajo en el pin D14 (SDA/D14)
// Tabla de velocidades (Tiempos en milisegundos)
uint16_t tabla_velocidad[11] = {
1000, // Nivel 0: lento inicial
850, // Nivel 1
700, // Nivel 2
550, // Nivel 3
420, // Nivel 4
320, // Nivel 5
230, // Nivel 6
160, // Nivel 7
100, // Nivel 8
60, // Nivel 9
30 // Nivel 10: máxima velocidad
};
// Variables de control de la animación
int cantidad_leds = 1;
int nivel_velocidad = 3; // Inicia en un nivel intermedio (550ms)
int velocidad = 550;
int posicion = 0;
int direccion = 1;
uint32_t tiempoAnterior = 0;
// Estados anteriores de los botones para detectar pulsación única
int menuAnterior = HIGH;
int fastAnterior = HIGH;
int slowAnterior = HIGH;
// Tiempos para el anti-rebote (Debounce)
uint32_t tiempoMenu = 0;
uint32_t tiempoFast = 0;
uint32_t tiempoSlow = 0;
void apagarTodos(void) {
for(int i = 0; i < NUM_LEDS; i++) {
digitalWrite(LED_PIN[i], LOW);
}
}
void mostrarAutoFantastico(void) {
apagarTodos();
for(int i = 0; i < cantidad_leds; i++) {
int led_actual = posicion + i;
if(led_actual >= 0 && led_actual < NUM_LEDS) {
digitalWrite(LED_PIN[led_actual], HIGH);
}
}
}
void secuenciaAutoFantastico(void) {
mostrarAutoFantastico();
posicion = posicion + direccion;
if(posicion >= NUM_LEDS - cantidad_leds) {
posicion = NUM_LEDS - cantidad_leds;
direccion = -1;
}
if(posicion <= 0) {
posicion = 0;
direccion = 1;
}
}
void revisarBotones(void) {
int menuActual = digitalRead(BTN_MENU_PIN);
int fastActual = digitalRead(BTN_FAST_PIN);
int slowActual = digitalRead(BTN_SLOW_PIN);
uint32_t ahora = millis();
// BOTÓN MENÚ: Cambia la cantidad de LEDs de 1 en 1 hasta 5
if(menuAnterior == HIGH && menuActual == LOW) {
if(ahora - tiempoMenu > 200) {
cantidad_leds++;
if(cantidad_leds > 5) {
cantidad_leds = 1;
}
if(posicion > NUM_LEDS - cantidad_leds) {
posicion = NUM_LEDS - cantidad_leds; // <-- Corregido aquí
if(posicion < 0) posicion = 0;
}
mostrarAutoFantastico();
tiempoMenu = ahora;
}
}
// BOTÓN MÁS VELOCIDAD
if(fastAnterior == HIGH && fastActual == LOW) {
if(ahora - tiempoFast > 200) {
if(nivel_velocidad < MAX_NIVEL_VEL) {
nivel_velocidad++;
}
velocidad = tabla_velocidad[nivel_velocidad];
tiempoFast = ahora;
}
}
// BOTÓN MENOS VELOCIDAD
if(slowAnterior == HIGH && slowActual == LOW) {
if(ahora - tiempoSlow > 200) {
if(nivel_velocidad > 0) {
nivel_velocidad--;
}
velocidad = tabla_velocidad[nivel_velocidad];
tiempoSlow = ahora;
}
}
menuAnterior = menuActual;
fastAnterior = fastActual;
slowAnterior = slowActual;
}
void setup() {
// Configurar pines de los 10 LEDs como salida
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(LED_PIN[i], OUTPUT);
}
// Configurar los 3 botones con Pull-Up en los nuevos pines del lado derecho
pinMode(BTN_MENU_PIN, INPUT_PULLUP);
pinMode(BTN_FAST_PIN, INPUT_PULLUP);
pinMode(BTN_SLOW_PIN, INPUT_PULLUP);
apagarTodos();
mostrarAutoFantastico();
}
void loop() {
revisarBotones();
if(millis() - tiempoAnterior >= (uint32_t)velocidad) {
tiempoAnterior = millis();
secuenciaAutoFantastico();
}
}