#include <ezButton.h>
// Define LED pins
const int greenLED = 32; // GPIO for green LED
const int yellowLED = 33; // GPIO for yellow LED
const int redLED = 25; // GPIO for red LED
// Initialize ezButton objects for each button
ezButton greenButton(12); // GPIO for green button
ezButton yellowButton(14); // GPIO for yellow button
ezButton redButton(27); // GPIO for red button
void setup() {
// Initialize LED pins as output
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
// Set debounce time for each button
greenButton.setDebounceTime(30); // Set debounce time for green button
yellowButton.setDebounceTime(30); // Set debounce time for yellow button
redButton.setDebounceTime(30); // Set debounce time for red button
// Start serial communication
Serial.begin(115200);
Serial.println("Starting...");
}
void loop() {
// Update button states
greenButton.loop();
yellowButton.loop();
redButton.loop();
// Check for button releases using ezButton's isReleased() method
if (greenButton.isReleased()) {
buttonPressed(greenLED, "green");
} else if (yellowButton.isReleased()) {
buttonPressed(yellowLED, "yellow");
} else if (redButton.isReleased()) {
buttonPressed(redLED, "red");
}
delay(50); // Small delay to prevent multiple button presses being detected
}
void buttonPressed(int ledPin, String color) {
// Turn on the corresponding LED
digitalWrite(ledPin, HIGH);
// Print to the serial monitor with the time in milliseconds
Serial.print(millis());
Serial.print(" ms, button chosen: ");
Serial.println(color);
// Wait 500 ms
delay(100);
// Turn off the LED
digitalWrite(ledPin, LOW);
}