Advertisement
bitwise_gamgee

Untitled

Oct 13th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include <TM1637Display.h>
  2. #include "Button.h"
  3.  
  4. const int CLK_PIN = 2;
  5. const int DIO_PIN = 3;
  6. const int BUZZER_PIN = 6;
  7. Button thirtySecButton(4);
  8. Button fifteenSecButton(5);
  9. Button topButton(8);
  10.  
  11. TM1637Display display(CLK_PIN, DIO_PIN);
  12.  
  13. enum Mode {
  14.   start,
  15.   countdown30,
  16.   countdown15,
  17.   stopped,
  18.   timeOut,
  19. };
  20.  
  21. Mode mode = start;
  22. unsigned long lastTicTacTime = 0;
  23. unsigned long lastDisplayTime = 0;
  24.  
  25. void setup() {
  26.   display.setBrightness(1);
  27.   thirtySecButton.begin();
  28.   fifteenSecButton.begin();
  29.   topButton.begin();
  30. }
  31.  
  32. void loop() {
  33.   static unsigned long timer = millis();
  34.   static int currentDisplayTime = 0;
  35.   unsigned long currentTime = millis();
  36.  
  37.   if (mode == start) {
  38.     if (currentTime - lastDisplayTime >= 300) {
  39.       display.showNumberDecEx(0, 0b01000000, true, 4, 0);
  40.       display.showSegment((uint8_t *)"rdy ", 4, 0);
  41.       lastDisplayTime = currentTime;
  42.     }
  43.   }
  44.  
  45.   if (thirtySecButton.pressed()) {
  46.     mode = countdown30;
  47.     currentDisplayTime = 300;
  48.     timer = currentTime;
  49.   }
  50.   if (fifteenSecButton.pressed()) {
  51.     mode = countdown15;
  52.     currentDisplayTime = 150;
  53.     timer = currentTime;
  54.   }
  55.   if (topButton.pressed() && (mode == countdown30 || mode == countdown15)) {
  56.     mode = stopped;
  57.   }
  58.  
  59.   if (mode == stopped && currentTime - lastDisplayTime >= 4000) {
  60.     mode = start;
  61.   }
  62.  
  63.   if (mode == countdown30 || mode == countdown15) {
  64.     if (currentTime - lastTicTacTime >= (1000 - currentDisplayTime)) {
  65.       tone(BUZZER_PIN, 1000);
  66.       delay(50);
  67.       noTone(BUZZER_PIN);
  68.       lastTicTacTime = currentTime;
  69.     }
  70.  
  71.     if (currentTime - timer >= 100) {
  72.       if (currentDisplayTime > 0) {
  73.         timer += 100;
  74.         display.showNumberDecEx(currentDisplayTime, 0b01000000, true, 3, 0);
  75.         currentDisplayTime--;
  76.       } else {
  77.         tone(BUZZER_PIN, 2000, 500); // Game over sound
  78.         display.showNumberDecEx(0, 0b01000000, true, 4, 0);
  79.         mode = timeOut;
  80.       }
  81.     }
  82.   }
  83.  
  84.   if (mode == timeOut && currentTime - lastDisplayTime >= 4000) {
  85.     mode = start;
  86.   }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement