blob: 12f42d6344f83f08621ec059f4839ccb5f411c57 (
plain)
- /// mussel_mood.ino - Button-controlled LED system with state tracking
- ///
- /// SPDX-License-Identifier: GPL-3.0-or-later
- /// SPDX-FileCopyrightText: 2025 Tanishka Suwalka <tanishkas@ruc.dk>
- ///
- /// This code toggles LEDs based on button presses, cycling through Red, Green, and Yellow states.
- 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); // Enable internal pull-up resistor for button
- pinMode(redLED, OUTPUT);
- pinMode(greenLED, OUTPUT);
- pinMode(yellowLED, OUTPUT);
- Serial.begin(9600); // Initialize serial monitor for debugging
- }
- void loop() {
- int reading = digitalRead(buttonPin); // Read button state
- // Debounce logic: Ensures a single press isn't detected multiple times
- if (reading != lastButtonState) {
- lastDebounceTime = millis(); // Reset debounce timer
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- // Check for button press (transition from HIGH to LOW)
- if (reading == LOW && lastButtonState == HIGH) {
- clickCount++; // Increment click count
- if (clickCount > 3) {
- clickCount = 0; // Reset cycle after 3 clicks
- }
- updateLEDs();
- }
- }
- lastButtonState = reading; // Update button state
- }
- /// updateLEDs() - Updates LED states based on click count
- void updateLEDs() {
- // Turn off all LEDs first
- digitalWrite(redLED, LOW);
- digitalWrite(greenLED, LOW);
- digitalWrite(yellowLED, LOW);
- // Determine which LED to turn on
- 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;
- }
- }
|