// LCD1602 to Arduino Uno connection example
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
long mulai, selesai, dataStopWatch;
int i=0;
int fpause = 0;
long lasttombol =0;
long delayantibouncing =50;
long datapause = 0;
void setup() {
pinMode(A0, INPUT);
pinMode(A1, INPUT);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print(" ARDUINO");
lcd.setCursor(0,1);
lcd.print(" STOPWATCH");
delay(2000);
lcd.clear();
lcd.print("TEKAN TOMBOL");
lcd.setCursor(0,1);
lcd.print("START/STOP");
}
void loop() {
if (digitalRead(A0)==0){
if((millis() - lasttombol)>delayantibouncing) {
if (i==0){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Start Timer");
mulai = millis();
fpause=0;
}
else if (i==1){
lcd.setCursor(0,0);
lcd.print("Stop Timer");
datapause = dataStopWatch;
fpause = 0;
}
i=!i;
}
lasttombol = millis();
}
else if(digitalRead(A1)==0 && fpause == 1){
dataStopWatch = 0;
datapause = 0;
lcd.clear();
lcd.print("RESET STOPWATCH");
lcd.setCursor(0,1);
lcd.print("0:0:0:0");
delay(2000);
lcd.clear();
lcd.print("TEKAN TOMBOL");
lcd.setCursor(0,1);
lcd.print("START/STOP");
}
if (i==1){
selesai = millis();
float jam ,menit, detik, milidetik;
long over;
dataStopWatch = selesai - mulai;
dataStopWatch = datapause + dataStopWatch;
jam = int (dataStopWatch / 3600000);
over = dataStopWatch % 3600000;
menit = int (over / 60000);
over = over % 60000;
detik = int(over / 1000);
milidetik = over % 1000;
lcd.setCursor(0,1);
lcd.print(jam,0);
lcd.print(":");
lcd.print(menit,0);
lcd.print(":");
lcd.print(detik,0);
lcd.print(".");
if (jam < 10);{
lcd.print(milidetik, 0);
lcd.print("");
}
}
}