#include <LiquidCrystal_I2C.h>
//initialize lcd
LiquidCrystal_I2C lcd(0x27,16,2);
//setting hours, minutes, secound and miliseconds to 0
int h=0;
int m=0;
int s=0;
int ms=0;
//defines pin for all buttons
int startstopPin = 3;
int startstopLED = 4;
int resetPin = 2;
//defines starting points
int start=0;
int stop=0;
int reset=0;
int startstopState=0;
void setup()
{
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
//defining pins if they are INPUT or OUTPUT pins
pinMode(startstopPin, INPUT_PULLUP);
pinMode(startstopLED, OUTPUT);
pinMode(resetPin, INPUT_PULLUP);
}
void loop()
{
lcd.setCursor(3,1);
lcd.print("STOPWATCH");
lcd.setCursor(3,0);
lcd.print("TIME:");
lcd.print(h);
lcd.print(":");
lcd.print(m);
lcd.print(":");
lcd.print(s);
start = digitalRead(startstopPin); //reading buton state
if(start == LOW)
{
stopwatch(); //calls the stopwatch function
digitalWrite(startstopPin, HIGH);
}
}
void stopwatch()
{
lcd.setCursor(3,0); //setting start point on lcd
lcd.print("TIME:"); //display TIME
lcd.print(h); //display hours
lcd.print(":");
lcd.print(m); //display minutes
lcd.print(":");
lcd.print(s); //display seconds
lcd.setCursor(3,1);
lcd.print(" ");
ms=ms+100;
delay(10);
if(ms==590)
{
ms=0;
s=s+1;
}
if(s==60) //if state for counting up minutes
{
s=0;
m=m+1;
}
if(m==60) //if state for counting up hours
{
m=00;
h=h+01;
}
stop = digitalRead(startstopPin); //reading buton state
if(stop == LOW) //checking if button is pressed
{
stopwatch_stop(); //calls the stopwatch_stop function
digitalWrite(startstopPin, LOW);
}
else
{
stopwatch(); //calls the stopwatch function
}
}
void stopwatch_stop()
{
lcd.setCursor(3,0);
lcd.print("TIME:");
lcd.print(h);
lcd.print(":");
lcd.print(m);
lcd.print(":");
lcd.print(s);
lcd.setCursor(3,1);
lcd.print(" ");
start = digitalRead(startstopPin); //reading buton state
if(start == LOW)
{
stopwatch(); //calls the stopwatch function
digitalWrite(startstopPin, HIGH);
}
reset = digitalRead(resetPin); //reading buton state
if(reset == LOW)
{
stopwatch_reset(); //calls the stopwatch_reset function
loop();
}
else
{
stopwatch_stop(); //calls the stopwatch_stop function
}
}
void stopwatch_reset()
{
lcd.clear();
h=00; //seting hours to 0
m=00; //seting minutes to 0
s=00; //seting seconds to 0
return; //exiting the program and returning to the point where entered the program
}