#define BTN_SELECT 6
#define BTN_MODE 5
int selectLeds[] = {13, 12, 11};
int modeLeds[] = {10, 9, 8};
int selectIndex = 0;
int activeModeLed = 0;
int modeStates[3] = {0, 0, 0}; // Guarda el estado de cada LED
unsigned long previousMillis[3] = {0, 0, 0};
bool blinkState[3] = {LOW, LOW, LOW};
void setup() {
pinMode(BTN_SELECT, INPUT_PULLUP);
pinMode(BTN_MODE, INPUT_PULLUP);
for (int i = 0; i < 3; i++) {
pinMode(selectLeds[i], OUTPUT);
pinMode(modeLeds[i], OUTPUT);
}
activeModeLed = selectIndex; // Inicializa el primer LED seleccionado
}
void loop() {
static bool lastSelectState = HIGH;
static bool lastModeState = HIGH;
bool selectState = digitalRead(BTN_SELECT);
bool modeState = digitalRead(BTN_MODE);
// Botón de selección de LED
if (selectState == LOW && lastSelectState == HIGH) {
selectIndex = (selectIndex + 1) % 3;
updateSelectedLed();
activeModeLed = selectIndex; // No resetea, mantiene el estado previo
delay(200); // Antirrebote
}
lastSelectState = selectState;
// Botón de modo
if (modeState == LOW && lastModeState == HIGH) {
modeStates[selectIndex] = (modeStates[selectIndex] + 1) % 6; // Cambia solo el modo del LED activo
delay(200); // Antirrebote
}
lastModeState = modeState;
updateModeLeds();
}
// Función para encender solo el LED seleccionado por el botón SELECT
void updateSelectedLed() {
for (int i = 0; i < 3; i++) {
digitalWrite(selectLeds[i], i == selectIndex ? HIGH : LOW);
}
}
// Aplica el modo correspondiente a cada LED en la lista de modos guardados
void updateModeLeds() {
static unsigned long fadeMillis[3] = {0, 0, 0};
static int fadeValue[3] = {0, 0, 0};
static bool fadeDirection[3] = {true, true, true};
for (int i = 0; i < 3; i++) {
int ledPin = modeLeds[i];
switch (modeStates[i]) {
case 0: blinkLed(i, 250); break;
case 1: blinkLed(i, 500); break;
case 2: blinkLed(i, 1000); break;
case 3: // Fade-out
if (millis() - fadeMillis[i] >= 15) {
fadeMillis[i] = millis();
analogWrite(ledPin, fadeValue[i]);
fadeValue[i] += (fadeDirection[i] ? 5 : -5);
if (fadeValue[i] >= 255 || fadeValue[i] <= 0) fadeDirection[i] = !fadeDirection[i];
}
break;
case 4: digitalWrite(ledPin, HIGH); break;
case 5: digitalWrite(ledPin, LOW); break;
}
}
}
// Parpadeo sin bloquear el código
void blinkLed(int index, int delayTime) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis[index] >= delayTime) {
previousMillis[index] = currentMillis;
blinkState[index] = !blinkState[index];
digitalWrite(modeLeds[index], blinkState[index]);
}
}