blob: f44c907a870b16bdcc904687f9bd314414c6710f (
plain)
- const int buttonPin = 9;
- const int redLED = 3;
- const int greenLED = 4;
- const int yellowLED = 5;
- int buttonState = HIGH;
- int lastButtonState = HIGH;
- int clickCount = 0;
- unsigned long lastDebounceTime = 0;
- const unsigned long debounceDelay = 50;
- void setup() {
- pinMode(buttonPin, INPUT_PULLUP);
- pinMode(redLED, OUTPUT);
- pinMode(greenLED, OUTPUT);
- pinMode(yellowLED, OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- int reading = digitalRead(buttonPin);
- // Debounce logic
- if (reading != lastButtonState) {
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- if (reading == LOW && lastButtonState == HIGH) {
- clickCount++;
- if (clickCount > 3) {
- clickCount = 0; // Reset cycle after 3 clicks
- }
- updateLEDs();
- }
- }
- lastButtonState = reading;
- }
- void updateLEDs() {
- // Turn off all LEDs first
- digitalWrite(redLED, LOW);
- digitalWrite(greenLED, LOW);
- digitalWrite(yellowLED, LOW);
- switch (clickCount) {
- case 1:
- digitalWrite(redLED, HIGH);
- Serial.println("State: Angry (Red LED ON)");
- break;
- case 2:
- digitalWrite(greenLED, HIGH);
- Serial.println("State: Happy (Green LED ON)");
- break;
- case 3:
- digitalWrite(yellowLED, HIGH);
- Serial.println("State: Unsure (Yellow LED ON)");
- break;
- case 0:
- Serial.println("State: Off (All LEDs OFF)");
- break;
- }
- }
|