//referensi: https://mechatrofice.com/arduino/arduino-counter-code-circuit-working
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define buttonReset 2
#define buttonStop 3
#define buttonPoint 6
#define buttonStart 7
int startState=0;
int pointState=0;
int stopState=0;
int resetState=0;
int miliPresState=0;
int miliValue=0;
int detikPresState=0;
int detikValue=0;
int menitPresState=0;
int menitValue=0;
int putaran=0;
int running=false;
int alarmState=0;
String alarmValue = "OFF";
void setup() {
pinMode(buttonStart, INPUT_PULLUP);
pinMode(buttonPoint, INPUT_PULLUP);
pinMode(buttonStop, INPUT_PULLUP);
pinMode(buttonReset, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Stopwatchh");
lcd.setCursor(4,1);
lcd.print("00:00:00");
Serial.begin(9600);
}
void loop(){
start();
point();
reset();
stop();
}
void start(){
if(!digitalRead(buttonStart)) running = true;
if (running){
lcd.setCursor(10,1);
if(miliValue<59){
miliValue++;
}
if(miliValue<10){
lcd.print("0");
}
lcd.print(miliValue);
delay(1);
if(miliValue==59){
miliValue=0;
detikValue++;
}
lcd.setCursor(7,1);
if(detikValue<10){
lcd.print("0");
}
lcd.print(detikValue);
if(detikValue==59){
detikValue=0;
menitValue++;
}
lcd.setCursor(4,1);
if(menitValue<10){
lcd.print("0");
}
lcd.print(menitValue);
}
}
void point(){
pointState = digitalRead(buttonPoint);
if(pointState==LOW){
putaran++;
Serial.print("Putaran ke-");
Serial.print(putaran);
Serial.print(" = ");
if(menitValue<10){
Serial.print("0");
}
Serial.print(menitValue);
Serial.print(":");
if(detikValue<10){
Serial.print("0");
}
Serial.print(detikValue);
Serial.print(":");
if(miliValue<10){
Serial.print("0");
}
Serial.println(miliValue);
delay(10);
}
}
void reset(){
resetState = digitalRead(buttonReset);
if(resetState==LOW){
menitValue=0;
detikValue=0;
miliValue=0;
lcd.setCursor(4,1);
lcd.print("00:00:00");
}
}
void stop(){
if(!digitalRead(buttonStop)) running = false;
}