diff options
Diffstat (limited to 'mussel5/mussel5.ino')
-rw-r--r-- | mussel5/mussel5.ino | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/mussel5/mussel5.ino b/mussel5/mussel5.ino index f44c907..12f42d6 100644 --- a/mussel5/mussel5.ino +++ b/mussel5/mussel5.ino @@ -1,33 +1,41 @@ -const int buttonPin = 9; -const int redLED = 3; -const int greenLED = 4; -const int yellowLED = 5; +/// 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. -int buttonState = HIGH; -int lastButtonState = HIGH; -int clickCount = 0; -unsigned long lastDebounceTime = 0; -const unsigned long debounceDelay = 50; +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(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor for button pinMode(redLED, OUTPUT); pinMode(greenLED, OUTPUT); pinMode(yellowLED, OUTPUT); - Serial.begin(9600); + Serial.begin(9600); // Initialize serial monitor for debugging } void loop() { - int reading = digitalRead(buttonPin); + int reading = digitalRead(buttonPin); // Read button state - // Debounce logic + // Debounce logic: Ensures a single press isn't detected multiple times if (reading != lastButtonState) { - lastDebounceTime = millis(); + lastDebounceTime = millis(); // Reset debounce timer } if ((millis() - lastDebounceTime) > debounceDelay) { + // Check for button press (transition from HIGH to LOW) if (reading == LOW && lastButtonState == HIGH) { - clickCount++; + clickCount++; // Increment click count if (clickCount > 3) { clickCount = 0; // Reset cycle after 3 clicks } @@ -35,15 +43,17 @@ void loop() { } } - lastButtonState = reading; + 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); |