// Slave (ESP32)
#include <Wire.h>
#define SLAVE_ADDR 0x10 // This device's I2C address
// LED pins
const int ledPins[] = {4, 18, 19}; // LED1, LED2, LED3
// Button pins (with INPUT_PULLUP)
const int buttonPins[] = {2, 25, 26};
uint8_t currentPick = 0;
void setup() {
Serial.begin(115200);
Serial.println("Slave ready");
// Initialize LEDs and buttons
for (int i = 0; i < 3; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
pinMode(buttonPins[i], INPUT_PULLUP);
}
Wire.begin(SLAVE_ADDR); // Join I2C bus as slave
Wire.onReceive(receivePickCommand);
}
void loop() {
// Monitor buttons
for (int i = 0; i < 3; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
Serial.printf("Button %d pressed\n", i + 1);
// Turn off LEDs once pressed (optional)
clearLEDs();
currentPick = 0;
delay(300); // debounce
}
}
}
void receivePickCommand(int bytesReceived) {
if (bytesReceived > 0) {
currentPick = Wire.read();
Serial.printf("Received pick: LED #%d\n", currentPick);
showLED(currentPick);
}
}
void showLED(uint8_t pickNumber) {
clearLEDs();
if (pickNumber >= 1 && pickNumber <= 3) {
digitalWrite(ledPins[pickNumber - 1], HIGH);
}
}
void clearLEDs() {
for (int i = 0; i < 3; i++) {
digitalWrite(ledPins[i], LOW);
}
}