#include <LiquidCrystal_I2C.h>
//Buttons with internal Pullup, so if they get pressed we get LOW
const int start = 4; //Start the stopwatch
int run;
//chronometer
int cents = 0;
int seconds = 0;
int minutes = 0;
int hours = 0;
const int interval = 10; //Every 10 milliseconds i increase 1 cent
unsigned long previousMillis = 0;
int c1, c2, s1, s2, m1, m2, h; //Variables used to put in the form
//h:m2m1:s2s1:c2c1
LiquidCrystal_I2C lcd(0x27, 20, 4); // RS-Enable-D4-D5-D6-D7 in that digitalPin
void setup() {
pinMode(start, INPUT); //In the schematic from right to left
lcd.init();
lcd.begin(16, 2);
lcd.print(" Press start");
}
void loop() {
if (digitalRead(start) == HIGH) //funcitons based off of button pulling input pin LOW
{
if (run == 0)
{
run = 255;
}
else
{
run = 0;
}
}
if (run > 0)
{
lcd.home();
lcd.clear();
lcd.print("Now: ");
chronometer();//code you only run if button was pressed, stops running when button pressed again, so forth...
}
}
void chronometer(void) {
//This function print: "New: Actual time"
unsigned long currentMillis = millis(); //If for the updating. If it is true, it means 1 cent of a second had passed. Update cents, minutes, seconds, hours and then i write on the lcd
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
cents++;
if (cents == 100) {
cents = 0;
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
if (minutes == 60) {
minutes = 0;
hours++;
if (hours == 24)
hours = 0;
}
}
}
int cent = cents;
int sec = seconds;
int minu = minutes; //Taking the digits separeted
h = hours; //For the other funcionts, so i can put hours = 0 and h still is the last value
c1 = cent % 10;
cent /= 10;
c2 = cent % 10;
s1 = sec % 10;
sec /= 10;
s2 = sec % 10;
m1 = minu % 10;
minu /= 10;
m2 = minu % 10;
lcd.setCursor(6, 0);
lcd.print(h);
lcd.print(':');
lcd.print(m2);
lcd.print(m1);
lcd.print(':');
lcd.print(s2);
lcd.print(s1);
lcd.print(':');
lcd.print(c2);
lcd.print(c1);
}
}