#include <Wire.h>
#include <RTClib.h>
#include <TM1637.h>

RTC_DS1307 rtc;
TM1637 display(27, 14);

const int buttonPin = 34;  
bool lastButtonState = HIGH;
bool currentButtonState = HIGH;
unsigned long lastDebounceTime = 0;  
const unsigned long debounceDelay = 50;  
const unsigned long displayTime= 10000;  
bool displayOn = false;
void setup() {
  Serial.begin(115200);
  Wire.begin(19, 18);  
  
  pinMode(buttonPin, INPUT); 

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);  
  }

  if (!rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));  
  }

  display.init();
  display.set(7);  
}

void loop() {
  int buttonState = digitalRead(buttonPin);  

  if (buttonState != lastButtonState) {
    lastDebounceTime = millis();  
  }

  if ((millis() - lastDebounceTime) > displayTime) {

    if (buttonState != currentButtonState) {
      currentButtonState = buttonState;
      
      if (currentButtonState == LOW) {
        DateTime now = rtc.now();  
        int hours = now.hour();
        int minutes = now.minute();  
        displayTime(hours, minutes);
        displayOn = true;
      }

    }
  }

  lastButtonState = buttonState;  

   if (displayOn && (millis() - buttonPressTime >= displayDuration)) {
    displayOn = false; 
    clearDisplay();
}
}
void displayTime(int hours, int minutes) {
  int8_t displayData[4];
  

  displayData[0] = hours / 10;  
  displayData[1] = hours % 10;  
  displayData[2] = minutes / 10;  
  displayData[3] = minutes % 10; 

  display.display(displayData);
}
void clearDisplay() {
  int8_t clearData[4] = { 0x7F, 0x7F, 0x7F, 0x7F }; 
  display.display(clearData);
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
4-Digit Display
sevseg1:CLK
sevseg1:DIO
sevseg1:VCC
sevseg1:GND
GND5VSDASCLSQWRTCDS1307+
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r