#include <TM1637Display.h>
// Define the connections pins for display
#define CLK 2
#define DIO 3
// Define other pin connections
#define UP_BUTTON 8
#define DOWN_BUTTON 9
#define START_BUTTON 4
#define BUZZER 5
int duration; // Duration in seconds
int setDuration;
byte inProcess = 0;
unsigned long startTime = millis();
unsigned long timer = 1000ul ;// DURATION TIME
// Create display object of type TM1637Display:
TM1637Display display = TM1637Display(CLK, DIO);
// Set the individual segments for the word displays:
const uint8_t seg_end[] = {
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // E
SEG_C | SEG_E | SEG_G, // n
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
0x00 // All off
};
const uint8_t seg_go[] = {
SEG_A | SEG_C | SEG_D | SEG_F | SEG_G, // s
SEG_D | SEG_E | SEG_F | SEG_G, // t
0x00, // All off
0x00 // All off
};
void setup() {
pinMode(UP_BUTTON, INPUT_PULLUP);
pinMode(DOWN_BUTTON, INPUT_PULLUP);
pinMode(START_BUTTON, INPUT_PULLUP);
pinMode(BUZZER, OUTPUT);
Serial.begin(9600);
duration = 0; // Default to 10 seconds
display.setBrightness(5); // 0 to 7 change if required
ShowTime(duration);
}
void loop() {
// Function will checks for time change buttons and only return
// when start button pressed
WaitForStart();
// Start the duration timer
TimeDuration();
}
void WaitForStart() {
// Check for button presses every 0.15 seconds
if (digitalRead(START_BUTTON) == HIGH) {
// inProcess=0;
}
if (digitalRead(START_BUTTON) == LOW) {
inProcess = inProcess == 0 ? 1 : 0;
if (inProcess == 0) {
duration = 0;
ShowTime(duration);
tone(BUZZER, 1500, 100);
display.clear();
display.setSegments(seg_end);
Serial.println("Stop Counter");
delay(150);
}
if (inProcess == 1) {
Serial.println("Start Counter");
timer = 1000ul * duration;
startTime = millis();
tone(BUZZER, 1500, 100);
display.clear();
display.setSegments(seg_go);
delay(150);
}
}
//while (digitalRead(START_BUTTON) == HIGH){
//inProcess=1;
// Check if up or down has been pressed
//if time > 60 then increment by 10 seconds
if (digitalRead(UP_BUTTON) == LOW) {
if (duration < 900) {
Serial.println("Increment Duration");
duration += 60;
}
ShowTime(duration);
delay(150);
}
if (digitalRead(DOWN_BUTTON) == LOW) {
if (duration > 0) {
Serial.println("Decrement Duration");
duration -= 60;
}
ShowTime(duration);
delay(150);
}
//}
// Start button has been pressed
//tone(BUZZER, 1500, 100);
//display.clear();
//display.setSegments(seg_go);
}
void TimeDuration() {
// While loop will continue until time up
// Repeatedly check if time is up
if ((millis() - startTime) <= timer && inProcess == 1) {
Serial.print("millis: ");
Serial.print(millis());
Serial.print(" startTime: ");
Serial.print(startTime);
Serial.print(" timer: ");
Serial.print(timer);
// Calculate time elapsed in seconds
int elapsed = int((millis() - startTime) / 1000);
Serial.print(" duration: ");
Serial.print(duration);
Serial.print(" Elapsed: ");
Serial.println(elapsed);
ShowTime(duration - elapsed);
delay(250);
// Only start to display countdown after 3 seconds
// if ((millis() - startTime) > 1000) {
// ShowTime(duration - elapsed);
// }
//startTime = millis();
}
// Time up
//tone(BUZZER, 500, 1000);
// Wait 5 seconds and reset display
delay(000);
// Show duration for next player
//ShowTime(duration);
//if (lastTime == 0) {
//display.clear();
//display.setSegments(seg_end);
//}
}
void ShowTime(int value) {
static int lastTime = -1;
// Update the display if time has changed
if (1 == 1) {
//lastTime = value;
int iMinutes = value / 60;
int iSeconds = value - (iMinutes * 60);
// Show on 4 digit display
uint16_t number = iMinutes * 100 + iSeconds;
display.showNumberDecEx(number, 0b01000000, true, 4, 0);
}
}