#include <Wire.h>
#include <LiquidCrystal.h>
#include <Bounce2.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Arduino pins to lcd
const int ledRedPin = 11;
const int ledGreenPin = 12;
const int buttonStartStopPin = A0;
const int buttonResetPin = A1;
const int buzzerPin = 13;
Bounce buttonStartStop = Bounce(); // Tombol debounce object
Bounce buttonReset = Bounce(); // Tombol debounce object
bool isRunning = false;
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("INDRAROCK");
lcd.setCursor (0,1);
lcd.print(" AAIPSC Timer ");
delay(1500); // Tampilkan "JUDUL" selama 1,5 detik
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Press Start");
delay(2000);// Tampilkan "Press Start" selama 2 detik
pinMode(ledRedPin, OUTPUT);
pinMode(ledGreenPin, OUTPUT);
pinMode(buttonStartStopPin, INPUT_PULLUP);
pinMode(buttonResetPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
buttonStartStop.attach(buttonStartStopPin);
buttonStartStop.interval(10);
buttonReset.attach(buttonResetPin);
buttonReset.interval(10);
updatelcd();
}
void loop() {
buttonStartStop.update();
buttonReset.update();
if (buttonStartStop.fell()) {
if (!isRunning) {
isRunning = true;
startTime = millis() - elapsedTime;
digitalWrite(ledRedPin, LOW);
digitalWrite(ledGreenPin, HIGH);
// Nada START
tone(buzzerPin, 659, 50);delay(130);
tone(buzzerPin, 659, 50);delay(230);
tone(buzzerPin, 659, 50);delay(230);
tone(buzzerPin, 523, 50);delay(130);
tone(buzzerPin, 659, 50);delay(230);
tone(buzzerPin, 784, 50);delay(100);
} else {
isRunning = false;
elapsedTime = millis() - startTime;
digitalWrite(ledGreenPin, LOW);
digitalWrite(ledRedPin, HIGH);
}
}
if (buttonReset.fell()) {
if (!isRunning) {
elapsedTime = 0;
updatelcd();
digitalWrite(ledRedPin, HIGH);
digitalWrite(ledGreenPin, LOW);
tone(buzzerPin, 700, 100); delay(80);
tone(buzzerPin, 300, 100); delay(80);
tone(buzzerPin, 500, 100); delay(80);
tone(buzzerPin, 700, 100); delay(80);
tone(buzzerPin, 300, 100); delay(80);
tone(buzzerPin, 500, 100); delay(80);
tone(buzzerPin, 700, 100); delay(80);
tone(buzzerPin, 300, 100); delay(80);
tone(buzzerPin, 500, 100); delay(100);
noTone(buzzerPin);
}
}
if (isRunning) {
elapsedTime = millis() - startTime;
updatelcd();
}
}
void updatelcd() {
lcd.clear();
int minutes = (elapsedTime / 60000) % 60;
int seconds = (elapsedTime / 1000) % 60;
int microseconds = elapsedTime % 1000;
lcd.setCursor(2, 0);
lcd.print("Run : ");
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) lcd.print("0");
lcd.print(seconds);
lcd.setCursor(2, 10);
lcd.print(" : ");
lcd.print(microseconds);
lcd.clear();
}