Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TM1637Display.h>
- #include "Button.h"
- const int CLK_PIN = 2;
- const int DIO_PIN = 3;
- const int BUZZER_PIN = 6;
- Button thirtySecButton(4);
- Button fifteenSecButton(5);
- Button topButton(8);
- TM1637Display display(CLK_PIN, DIO_PIN);
- enum Mode {
- start,
- countdown30,
- countdown15,
- stopped,
- timeOut,
- };
- Mode mode = start;
- unsigned long lastTicTacTime = 0;
- unsigned long lastDisplayTime = 0;
- void setup() {
- display.setBrightness(1);
- thirtySecButton.begin();
- fifteenSecButton.begin();
- topButton.begin();
- }
- void loop() {
- static unsigned long timer = millis();
- static int currentDisplayTime = 0;
- unsigned long currentTime = millis();
- if (mode == start) {
- if (currentTime - lastDisplayTime >= 300) {
- display.showNumberDecEx(0, 0b01000000, true, 4, 0);
- display.showSegment((uint8_t *)"rdy ", 4, 0);
- lastDisplayTime = currentTime;
- }
- }
- if (thirtySecButton.pressed()) {
- mode = countdown30;
- currentDisplayTime = 300;
- timer = currentTime;
- }
- if (fifteenSecButton.pressed()) {
- mode = countdown15;
- currentDisplayTime = 150;
- timer = currentTime;
- }
- if (topButton.pressed() && (mode == countdown30 || mode == countdown15)) {
- mode = stopped;
- }
- if (mode == stopped && currentTime - lastDisplayTime >= 4000) {
- mode = start;
- }
- if (mode == countdown30 || mode == countdown15) {
- if (currentTime - lastTicTacTime >= (1000 - currentDisplayTime)) {
- tone(BUZZER_PIN, 1000);
- delay(50);
- noTone(BUZZER_PIN);
- lastTicTacTime = currentTime;
- }
- if (currentTime - timer >= 100) {
- if (currentDisplayTime > 0) {
- timer += 100;
- display.showNumberDecEx(currentDisplayTime, 0b01000000, true, 3, 0);
- currentDisplayTime--;
- } else {
- tone(BUZZER_PIN, 2000, 500); // Game over sound
- display.showNumberDecEx(0, 0b01000000, true, 4, 0);
- mode = timeOut;
- }
- }
- }
- if (mode == timeOut && currentTime - lastDisplayTime >= 4000) {
- mode = start;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement