/* * MECRO FLIGHT CORE - PROYECTO 04: AUTO FANTÁSTICO
* Arquitectura: Serialización de Datos (Shift Register)
* Hardware: ESP32 + 74HC595 + 8 LEDs
*/
// --- 1. DEFINICIÓN DE PINES DE CONTROL ---
// Solo necesitamos 3 pines para controlar infinitos LEDs (en cascada)
const int PIN_DATA = 23; // DS (Entrada de Datos Serial)
const int PIN_LATCH = 22; // ST_CP (Reloj de Almacenamiento / Cerrojo)
const int PIN_CLOCK = 21; // SH_CP (Reloj de Desplazamiento)
// Variable que representa el estado de los 8 LEDs (1 Byte = 8 Bits)
byte leds = 0;
void setup() {
pinMode(PIN_DATA, OUTPUT);
pinMode(PIN_LATCH, OUTPUT);
pinMode(PIN_CLOCK, OUTPUT);
Serial.begin(115200);
Serial.println("--- MECRO SERIAL BUS: ONLINE ---");
}
// Función auxiliar para enviar el byte al chip
void actualizarRegistro() {
// 1. Bajamos el Latch (Cerramos la "puerta" para que no se vea el cambio)
digitalWrite(PIN_LATCH, LOW);
// 2. Enviamos el byte completo bit a bit
// MSBFIRST = Most Significant Bit First (Envía el bit 7 primero)
shiftOut(PIN_DATA, PIN_CLOCK, MSBFIRST, leds);
// 3. Subimos el Latch (Abrimos la "puerta" y mostramos el resultado)
digitalWrite(PIN_LATCH, HIGH);
}
void loop() {
// EFECTO 1: Ida (Izquierda a Derecha)
// Usamos el operador '<<' (Bitwise Left Shift)
// 1 << 0 = 00000001
// 1 << 1 = 00000010
// ...
for (int i = 0; i < 8; i++) {
leds = 1 << i;
actualizarRegistro();
delay(100); // Velocidad del escáner
}
// EFECTO 2: Vuelta (Derecha a Izquierda)
for (int i = 6; i > 0; i--) { // Empezamos en 6 para no repetir el último
leds = 1 << i;
actualizarRegistro();
delay(100);
}
}