/*
 * First RTC program
 */
#include <RTClib.h>;
#include <Wire.h>;
RTC_DS1307 rtc;
//   Set Reminder Change Time
int buzz8amHH = 8;          //    HH - hours         ##Set these for reminder time in 24hr Form
int buzz8amMM = 00;          //    MM - Minute
int buzz8amSS = 00;          //    SS - Seconds
int buzz2pmHH = 14;          //    HH - hours
int buzz2pmMM = 00;          //    MM - Minute
int buzz2pmSS = 00;          //    SS - Seconds
int buzz8amHH2 = 8;          //    HH - hours after meal
int buzz8amMM2 = 30;          //    MM - Minute after meal
int buzz8amSS2 = 00;          //    SS - Seconds after meal
//int nowHr, nowMin, nowSec;                     // to show current mm,hh,ss
int ledState = LOW;
int ledPin = 2;
int keyinp = 3;
int keypressed = 1;

void setup() {
Serial.begin(9600);
 if (! rtc.begin()) {                      // check if rtc is connected 
   Serial.println("Couldn't find RTC");
   while (1);
 }
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));            // uncomment this to set the current time and then comment in next upload when u set the time
rtc.adjust(DateTime(2024, 1, 22, 8, 29, 00));                // manual time 
pinMode(ledPin, OUTPUT); 
pinMode(keyinp,INPUT_PULLUP);
}

void StartBuzz() {
   keypressed = digitalRead(keyinp);
   Serial.println(keypressed);
    if (keypressed == 1) {  
     if (ledState == LOW) {                  // if the LED is off turn it on and vice-versa:
       ledState = HIGH;
       digitalWrite(ledPin, ledState);
       delay(2000);
     }  
     else {
       ledState = LOW;    
       digitalWrite(ledPin, ledState);
       delay(2000);   
     }
    }
      else if (keypressed == 0) {
      ledState = LOW;
      Serial.println(keypressed);
      Serial.println(ledState);
      delay(500);   
  }
} 

void loop() {                      // function to start buzzing at 8am
  DateTime now = rtc.now();
     Serial.print("Date & Time: ");
     Serial.print(now.year(), DEC);
     Serial.print('/');
     Serial.print(now.month(), DEC);
     Serial.print('/');
     Serial.print(now.day(), DEC);
     Serial.print(" (");
     Serial.print(now.dayOfTheWeek());
     Serial.print(") ");
     Serial.print(now.hour(), DEC);
     Serial.print(':');
     Serial.print(now.minute(), DEC);
     Serial.print(':');
     Serial.println(now.second(), DEC);     
  if (int(now.hour()) == buzz8amHH) {
    if (int(now.minute()) == buzz8amMM) {
      if (int(now.second()) > buzz8amSS) {
        /////////////////////////////////////////////////////
        Serial.print(" Blink 1 ");
        StartBuzz();
        /////////////////////////////////////////////////////
      }
    }
  }

if (int(now.hour()) == buzz8amHH2) {
    if (int(now.minute()) == buzz8amMM2) {
      if (int(now.second()) > buzz8amSS2) {
        /////////////////////////////////////////////////////
        Serial.print(" Blink x ");
        StartBuzz();
        /////////////////////////////////////////////////////
      }
    }
  }

if (int(now.hour()) == buzz2pmHH) {
    if (int(now.minute()) == buzz2pmMM) {
      if (int(now.second()) > buzz2pmSS) {
        /////////////////////////////////////////////////////
        Serial.print(" Blink 2 ");
        StartBuzz();
        /////////////////////////////////////////////////////
      }
    }
  }






}
GND5VSDASCLSQWRTCDS1307+