#include <TM1637Display.h>
// Define the connections pins:
#define increment_pin 2
#define next_pin 3
#define start_pin 4
#define buzzer 5
#define CLK 6
#define DIO 7
// Define a variable to set minutes or seconds
boolean minutes = true;
// Variable for storing minutes and seconds
int m = 0;
int s = 0;
// Create display object of type TM1637Display:
TM1637Display display = TM1637Display(CLK, DIO);
// Variables for debouncing of the Next and Increment buttons
int NextState;
int lastNext = LOW;
int IncrementState;
int lastIncrement = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
display.clear();
delay(1000);
display.setBrightness(7);
pinMode(increment_pin, INPUT_PULLUP);
pinMode(next_pin, INPUT_PULLUP);
pinMode(start_pin, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
// Show seconds initially (s = 0 seconds)
display.showNumberDecEx(0, 0b11100000, true, 4, 0);
}
void loop() {
increment(digitalRead(increment_pin)); // when incerement button pressed go to increment function
next(digitalRead(next_pin)); // when next button pressed go to next function
// Check if start button pressed
if (digitalRead(start_pin) == LOW) {
int i, j;
display.clear();
delay(500);
//Start coundtdown timer
for (i = m; i >= 0; i--) {
if (i == m) j = s; else j = 59;
for (j; j >= 0 ; j--) {
display.showNumberDecEx(i, 0b11100000, true, 2, 0);
display.showNumberDecEx(j, 0b11100000, true, 2, 2);
delay(1000);
}
}
// if timer finished then start the buzzer forever
while (1) {
tone(buzzer, 1000);
delay(200);
tone(buzzer, 700);
delay(200);
}
}
}
//Function to set seconds or minutes
void next(int reading) {
if (reading != lastNext) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != NextState) {
NextState = reading;
if (NextState == LOW) {
minutes = !minutes;
}
}
}
lastNext = reading;
}
//Function to increment the digits of seconds or minutes
void increment(int reading) {
if (reading != lastIncrement) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != IncrementState) {
IncrementState = reading;
if (IncrementState == LOW && minutes) {
if (m < 59)
m+=5;
else
m = 0;
display.showNumberDecEx(m, 0b11100000, true, 2, 0);
}
else if (IncrementState == LOW && !minutes) {
if (s < 59)
s++;
else
s = 0;
display.showNumberDecEx(s, 0b11100000, true, 2, 2);
}
}
}
lastIncrement = reading;
}