// ESP32 - DIP 4 bits -> seleccionar 1 de 9 secuencias para 8 LEDs
#include <Arduino.h>
// Mapea DIP switches (bit0 LSB .. bit3 MSB)
const int dipPins[4] = {32, 33, 25, 26};
// Pines de los 8 LEDs (bit0 .. bit7)
const int ledPins[8] = {2, 4, 16, 17, 5, 18, 19, 27};
// 9 secuencias (cada byte = bits LED7..LED0, usamos bitRead(patron,i))
const uint8_t secuencias[9] = {
B00000000, // 0 - todos apagados
B11111111, // 1 - todos encendidos
B10101010, // 2 - alterno 1
B01010101, // 3 - alterno 2
B10000001, // 4 - extremos
B11000011, // 5 - extremos + vecinos
B11100111, // 6 - patrón centrado
B11111100, // 7 - primeros 6 encendidos
B00011111 // 8 - últimos 5 encendidos
};
int lastValor = -1;
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("Inicio: DIP->Secuencias (0..8)");
// config DIP con pullups internos
for (int i = 0; i < 4; i++) {
pinMode(dipPins[i], INPUT_PULLUP);
}
// config LEDs como salidas
for (int i = 0; i < 8; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
}
void mostrarSecuencia(int idx) {
if (idx < 0 || idx > 8) {
// valor fuera de rango -> apagar todos
for (int i = 0; i < 8; i++) digitalWrite(ledPins[i], LOW);
return;
}
uint8_t patron = secuencias[idx];
for (int i = 0; i < 8; i++) {
bool estado = bitRead(patron, i);
digitalWrite(ledPins[i], estado ? HIGH : LOW);
}
}
int leerDIP() {
int valor = 0;
for (int i = 0; i < 4; i++) {
// usamos pull-up: cuando switch conecta a GND -> digitalRead = LOW -> queremos bit = 1
int bit = !digitalRead(dipPins[i]); // invertimos
valor |= (bit << i);
}
return valor;
}
void loop() {
int valor = leerDIP(); // 0..15
if (valor != lastValor) {
lastValor = valor;
// imprime binario formado (bit3..bit0)
Serial.print("DIP value decimal: ");
Serial.print(valor);
Serial.print(" bin: ");
for (int b = 3; b >= 0; b--) Serial.print( (valor >> b) & 1 );
Serial.println();
if (valor <= 8) {
Serial.print("Mostrando secuencia #");
Serial.println(valor);
mostrarSecuencia(valor);
} else {
Serial.println("Valor >=9 -> Apagando LEDs");
mostrarSecuencia(-1);
}
}
delay(80);
}