// Example for the TM1637 display with TM1637_RT library.
// By Koepel, 28 February 2023.
//
// Library: "TM1637_RT", https://github.com/RobTillaart/TM1637_RT
#include <TM1637.h>
#include <Servo.h>
Servo Clochette;
TM1637 TM1;
unsigned long previousMillisClock; // for the millis timer of the first display
const unsigned long intervalClock = 1000;
bool colon = false; // for a toggling of the colon in the middle.
unsigned long previousMillisCountDown; // for a millis timer of second display
const unsigned long intervalCountDown = 100;
int clockSeconds; // the seconds counter.
int clockMinutes=55; // the minute counter.
int clockHours=9; // the hour counter.
int const BpH=3; //Bp to set H, M and start the watch.
int const BpM=2;
int const BpStart=4;
int const Buzzer=5;
int clochette1=45;
int clochette2=0;
int NbCoup=0;
void setup()
{
Serial.begin(9600);
pinMode(BpH, INPUT_PULLUP);
pinMode(BpM, INPUT_PULLUP);
pinMode(BpStart, INPUT_PULLUP);
pinMode(Buzzer, OUTPUT);
Clochette.attach(7);
Clochette.write(clochette1);
// Afficheur
TM1.begin(8, 9, 4); // clockpin, datapin, #digits
TM1.displayClear();
TM1.setBrightness(7); // full brightness, default is 3
}
void loop()
{
//Clock Set : Hour and Minute.
SET:
if(digitalRead(BpH)==LOW){++clockHours;}
if(digitalRead(BpM)==LOW){++clockMinutes;}
if(clockHours>=24){clockHours=0;}
if(clockMinutes>=60){clockMinutes=0;}
TM1.displayTime(clockHours,clockMinutes, colon);
// Serial.print("H=");
// Serial.print(clockHours);
// Serial.print(" M= ");
// Serial.println(clockMinutes);
delay(500);
if(digitalRead(BpStart)==LOW){tone(Buzzer,100,1000);goto CLOCK;}
goto SET;
//Clock Start
CLOCK:
unsigned long currentMillis = millis();
if( currentMillis - previousMillisClock >= intervalClock)
{
previousMillisClock += intervalClock; // for a clock-accurate millis timer
TM1.displayTime(clockHours,clockMinutes, colon); // display the numbers
colon = !colon; // toggle colon
// increment time
clockSeconds++;
if (clockSeconds >= 60)
{
clockSeconds = 0;
clockMinutes++;
if (clockMinutes >= 60)
{
clockMinutes = 0;
clockHours++;
buzzer();
clochette();
if(clockHours>=23)
{clockHours = 0;}
}
}
}
goto CLOCK;
}
void buzzer()
{
tone(Buzzer,100,1000);
}
void clochette()
{
CLOCHETTE:
Serial.print("Hours ");
Serial.println(clockHours);
Clochette.write(clochette2);
delay(500);
Clochette.write(clochette1);
NbCoup++;
Serial.print("Nb de coup ");
Serial.println(NbCoup);
delay(650);
if(NbCoup<clockHours)
{goto CLOCHETTE;}
NbCoup=0;
}