aboutsummaryrefslogtreecommitdiff
path: root/mussel5/mussel5.ino
blob: 12f42d6344f83f08621ec059f4839ccb5f411c57 (plain)
  1. /// mussel_mood.ino - Button-controlled LED system with state tracking
  2. ///
  3. /// SPDX-License-Identifier: GPL-3.0-or-later
  4. /// SPDX-FileCopyrightText: 2025 Tanishka Suwalka <tanishkas@ruc.dk>
  5. ///
  6. /// This code toggles LEDs based on button presses, cycling through Red, Green, and Yellow states.
  7. const int buttonPin = 9;
  8. const int redLED = 3;
  9. const int greenLED = 4;
  10. const int yellowLED = 5;
  11. int buttonState = HIGH;
  12. int lastButtonState = HIGH;
  13. int clickCount = 0;
  14. unsigned long lastDebounceTime = 0;
  15. const unsigned long debounceDelay = 50;
  16. void setup() {
  17. pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor for button
  18. pinMode(redLED, OUTPUT);
  19. pinMode(greenLED, OUTPUT);
  20. pinMode(yellowLED, OUTPUT);
  21. Serial.begin(9600); // Initialize serial monitor for debugging
  22. }
  23. void loop() {
  24. int reading = digitalRead(buttonPin); // Read button state
  25. // Debounce logic: Ensures a single press isn't detected multiple times
  26. if (reading != lastButtonState) {
  27. lastDebounceTime = millis(); // Reset debounce timer
  28. }
  29. if ((millis() - lastDebounceTime) > debounceDelay) {
  30. // Check for button press (transition from HIGH to LOW)
  31. if (reading == LOW && lastButtonState == HIGH) {
  32. clickCount++; // Increment click count
  33. if (clickCount > 3) {
  34. clickCount = 0; // Reset cycle after 3 clicks
  35. }
  36. updateLEDs();
  37. }
  38. }
  39. lastButtonState = reading; // Update button state
  40. }
  41. /// updateLEDs() - Updates LED states based on click count
  42. void updateLEDs() {
  43. // Turn off all LEDs first
  44. digitalWrite(redLED, LOW);
  45. digitalWrite(greenLED, LOW);
  46. digitalWrite(yellowLED, LOW);
  47. // Determine which LED to turn on
  48. switch (clickCount) {
  49. case 1:
  50. digitalWrite(redLED, HIGH);
  51. Serial.println("State: Angry (Red LED ON)");
  52. break;
  53. case 2:
  54. digitalWrite(greenLED, HIGH);
  55. Serial.println("State: Happy (Green LED ON)");
  56. break;
  57. case 3:
  58. digitalWrite(yellowLED, HIGH);
  59. Serial.println("State: Unsure (Yellow LED ON)");
  60. break;
  61. case 0:
  62. Serial.println("State: Off (All LEDs OFF)");
  63. break;
  64. }
  65. }