#include <Wire.h>
#include <iarduino_RTC.h>
#include <LiquidCrystal_I2C.h>

iarduino_RTC rtc(RTC_DS1307);
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int pin_button_stw = 2;
const int pin_button_clock = 4;
const int interval = 1000; 

unsigned long currentMillis = 0;
unsigned long prevTime = 0; 

unsigned long lastButtonPress = 0; 
const int debounceDelay = 200; 

int stopwatchState = 0;
int h = 0, m = 0, s = 0;

int clock_state = 0;

void setup() {
  Wire.begin();
  lcd.init();
  lcd.backlight();
  rtc.begin();
  pinMode(pin_button_stw, INPUT_PULLUP);
  pinMode(pin_button_clock, INPUT_PULLUP);
  printStopwatch();
}

void loop() {
  checkButtonStpW();
  check_Button_Clock();
  currentMillis = millis();
  if (currentMillis - prevTime >= interval) {
    prevTime = currentMillis;

    if (clock_state == 0)
      updateClock12();
    else 
      updateClock24();

    if (stopwatchState == 1)
      updateStopwatch();
  }
}

void check_Button_Clock() {
  if (digitalRead(pin_button_clock) == LOW) {
        if (millis() - lastButtonPress > debounceDelay) {
            lastButtonPress = millis();

            if (clock_state == 0) {
                clock_state = 1;
                lcd.clear();
                printStopwatch();
            }
            else {
              clock_state = 0;
              printStopwatch();
            }
        }
    }
}

void checkButtonStpW() {
    if (digitalRead(pin_button_stw) == LOW) {
        if (millis() - lastButtonPress > debounceDelay) {
            lastButtonPress = millis();
            switch (stopwatchState) {
              case 0:
                stopwatchState = 1;
              break;
              case 1:
                stopwatchState = 2;
              break;
              case 2:
                resetStopwatch(); 
                stopwatchState = 0;
              break;
            }
        }
    }
}

void updateClock24() {
    rtc.gettime();
    int hours = rtc.Hours;
    char buffer[20];
    lcd.setCursor(0, 0);
    sprintf(buffer, "Clock: %02d:%02d:%02d", hours, rtc.minutes, rtc.seconds);
    lcd.print(buffer);
}

void updateClock12() {
    rtc.gettime();
    lcd.setCursor(0, 0);
    char buffer [20];
    sprintf(buffer, "Clock: %02d:%02d:%02d", rtc.hours, rtc.minutes, rtc.seconds);
    lcd.print(buffer);
    lcd.print(rtc.midday == 1 ? " PM" : " AM");
}

void updateStopwatch() {
    s++;
    if (s >= 60) {
        s = 0;
        m++;
        
        if (m >= 60) {
          m = 0;
          h++;
        }
    }
    printStopwatch();
}

void resetStopwatch() {
  h = m = s = 0;
  printStopwatch();
}

void printStopwatch() {
  lcd.setCursor(0, 1);
  char buffer[20];
  sprintf(buffer, "Stopwatch: %02d:%02d:%02d", h, m, s);
  lcd.print(buffer);
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
GND5VSDASCLSQWRTCDS1307+
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r