// ESP32 Pins connected to 74HC595
const int latchPin = 13; // ST_CP pin of both 74HC595s
const int clockPin = 12; // SH_CP pin of both 74HC595s
const int dataPin = 14; // DS pin of the first 74HC595
// IR Sensors
const int irup = 34; // IR at the bottom (analog-capable)
const int irdown = 35; // IR at the top (analog-capable)
// Variables to store LED states (for 15 LEDs)
uint16_t ledStates = 0; // Using 16 bits, only 15 for LEDs
// Function to send data to both shift registers
void updateShiftRegister() {
digitalWrite(latchPin, LOW); // Start the transmission
shiftOut(dataPin, clockPin, MSBFIRST, ledStates >> 8); // Send high byte (LED 9-15)
shiftOut(dataPin, clockPin, MSBFIRST, ledStates & 0xFF); // Send low byte (LED 1-8)
digitalWrite(latchPin, HIGH); // End transmission, update LEDs
}
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(irup, INPUT);
pinMode(irdown, INPUT);
}
void loop() {
// Read the state of the IR sensors
int irUpState = digitalRead(irup);
int irDownState = digitalRead(irdown);
// If IR sensor at the bottom is triggered (irup)
if (irUpState == HIGH) {
// Turn on LEDs sequentially
for (int i = 0; i < 15; i++) {
ledStates |= (1 << i); // Set bit for LED i
updateShiftRegister();
delay(300);
}
delay(5000); // Wait for 5 seconds
// Turn off LEDs sequentially
for (int i = 0; i < 15; i++) {
ledStates &= ~(1 << i); // Clear bit for LED i
updateShiftRegister();
delay(1500 - i * 100); // Vary delay
}
}
// If IR sensor at the top is triggered (irdown)
if (irDownState == HIGH) {
// Turn on LEDs sequentially in reverse order
for (int i = 14; i >= 0; i--) {
ledStates |= (1 << i); // Set bit for LED i
updateShiftRegister();
delay(300);
}
delay(5000); // Wait for 5 seconds
// Turn off LEDs sequentially in reverse order
for (int i = 14; i >= 0; i--) {
ledStates &= ~(1 << i); // Clear bit for LED i
updateShiftRegister();
delay(1500 - (14 - i) * 100); // Vary delay
}
}
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1