#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16x2 LCD
volatile bool running = false;
volatile bool lap = false;
volatile bool reset = false;
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
unsigned long lapTime = 0;
bool lastButtonState = HIGH; // Pro detekci START/STOP
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Stopwatch: 0.0s");
lcd.setCursor(0, 1);
lcd.print("Lap: ");
pinMode(4, INPUT_PULLUP); // START/STOP
pinMode(3, INPUT_PULLUP); // LAP
pinMode(2, INPUT_PULLUP); // CLEAR
interrupts();
attachInterrupt(digitalPinToInterrupt(3), lapButton, FALLING); // LAP
attachInterrupt(digitalPinToInterrupt(2), clear, FALLING); // CLEAR
}
void loop() {
// DetekceSTART/STOP na pinu 4
bool currentButtonState = digitalRead(4);
if (currentButtonState == LOW && lastButtonState == HIGH) {
if (!running) {
startTime = millis() - elapsedTime;
running = true;
} else {
running = false;
}
delay(50);
}
lastButtonState = currentButtonState;
if (reset) {
elapsedTime = 0;
startTime = 0;
running = false;
lcd.setCursor(0, 0);
lcd.print("Stopwatch: 0.0s");
lcd.setCursor(0, 1);
lcd.print("Lap: ");
reset = false;
}
if (running) {
elapsedTime = millis() - startTime;
lcd.setCursor(0, 0);
lcd.print("Stopwatch: ");
lcd.setCursor(10, 0);
lcd.print(elapsedTime / 1000.0, 1);
lcd.print("s");
}
if (lap) {
lapTime = elapsedTime;
lcd.setCursor(0, 1);
lcd.print("Lap: ");
lcd.setCursor(5, 1);
lcd.print(lapTime / 1000.0, 1);
lcd.print("s");
lap = false;
}
}
void lapButton() {
if (running) {
lap = true;
}
}
void clear() {
reset = true;
}