#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
long int msec = 0;
int sec = 0;
int minutes = 0;
int period = 99;
unsigned long time_now = 10;
unsigned long time_pre = 10;
const int button = 2;
const int resetbutton = 3;
byte newButtonState = LOW;
byte oldButtonState = LOW;
byte oldResetButtonState = LOW;
boolean running = false;
double i = 0;
double a = millis();
double c ;
void setup() {
lcd.begin(16, 0);
pinMode(button, INPUT);
pinMode(resetbutton, INPUT);
timershow();
}
void loop() {
byte newButtonState;
byte newResetButtonState;
newResetButtonState = digitalRead(resetbutton);
if (newResetButtonState == HIGH && oldResetButtonState == LOW) {
delay(50); //debounce
reset();
timershow();
}
oldResetButtonState = newResetButtonState;
newButtonState = digitalRead(button);
if (newButtonState == HIGH && oldButtonState == LOW) {
delay(50); //debounce
running = !running;
}
oldButtonState = newButtonState;
if (running) {
timer();
}
}
void reset() { //set the clock to 0
msec = 0;
sec = 0;
minutes = 0;
}
void timer() { //counting millisecounds, secounds and minutes
time_now = millis();
if ((time_now - time_pre) > period) {
time_pre = time_now;
msec ++;
if (msec > 9) {
msec = 0; //if the timer goes to 100 it will reset to 0 and become +1 in the next column
sec++; //incrementing seconds by 1
}
if (sec > 59) //Allows for the minute to occur
{ minutes++;
sec = 0;
}
timershow();
}
}
void timershow() { //printing time to LCD
lcd.setCursor(0, 1);
if (minutes < 10) {
lcd.print("0");
lcd.print(minutes);
}
else {
lcd.print(minutes);
}
lcd.print(":");
if (sec < 10) {
lcd.print("0");
lcd.print(sec);
lcd.print(":");
}
else {
lcd.print(sec);
lcd.print(":");
}
if (msec < 10) {
lcd.print("0");
lcd.print(msec);
}
else {
lcd.print(msec);
}
}
/*while(digitalRead(3) == HIGH)
{
c = millis();
i = (c - a) / 1000;
lcd.print(i);
lcd.setCursor(7,0);
lcd.print("Sec's");
lcd.setCursor(0,0);*/