#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C dan ukuran LCD (16 kolom, 2 baris)
int startStopButton = 13; // Pin tombol start/stop
int resetButton = 12; // Pin tombol reset
volatile boolean timerEnabled = false;
volatile boolean resetRequested = false;
volatile unsigned long startTime = 0;
volatile unsigned long elapsedTime = 0;
void IRAM_ATTR startStopTimer() {
timerEnabled = !timerEnabled;
if (timerEnabled) {
startTime = millis() - elapsedTime;
}
}
void IRAM_ATTR resetTimer() {
resetRequested = true;
}
void setup() {
pinMode(startStopButton, INPUT_PULLUP);
pinMode(resetButton, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(startStopButton), startStopTimer, FALLING);
attachInterrupt(digitalPinToInterrupt(resetButton), resetTimer, FALLING);
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Elapsed Time:");
lcd.setCursor(0, 1);
lcd.print("0 ms");
Serial.begin(9600); // Inisialisasi Serial Monitor
Serial.println("Stopwatch started");
}
void loop() {
if (timerEnabled) {
unsigned long currentTime = millis();
elapsedTime = currentTime - startTime;
lcd.setCursor(0, 1);
lcd.print(elapsedTime);
lcd.print(" ms");
Serial.print("Elapsed Time: ");
Serial.print(elapsedTime);
Serial.println(" ms");
}
if (resetRequested && !timerEnabled) {
resetRequested = false;
elapsedTime = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Elapsed Time:");
lcd.setCursor(0, 1);
lcd.print("0 ms");
Serial.println("Timer reset");
}
}