//http://robojax.com/learn/arduino/?vid=robojax-DS1307-rtc-clock
/*#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
  while (!Serial); // for Leonardo/Micro/Zero

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

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
     //rtc.adjust(DateTime(2019, 3, 3, 20, 0, 0));
  }
  //rtc.adjust(DateTime(2019, 3, 3, 20, 0, 0));
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

void loop () {
    DateTime now = rtc.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println();
    delay(3000);
}
 */

 /*
 * Alarm Sketch for DS1307 Real time clock module 
 * To turn ON or OFF AC or DC load
 * 
 * Code Written/modefied by Ahmad Shamshiri on Jun 15, 2019
 * in Ajax, Ontario, Canada @ www.Robojax.com
 Watch video instruction for this video: https://youtu.be/ykgXKUuvtbE
 * 
 * 
 * for library of this code visit http://robojax.com/
 * Original library from https://github.com/adafruit/RTClib
 */
//DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec)

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

#define alarmPin 12

const int alarm[]={2024,1,19,16,35,0};
const int waitTime =4000;


RTC_DS1307 rtc;


void setup () {
  pinMode(alarmPin,OUTPUT);//set pin 2 as output
 
  while (!Serial); // for Leonardo/Micro/Zero

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

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
     //rtc.adjust(DateTime(2019, 3, 3, 20, 0, 0));
  }
  //rtc.adjust(DateTime(2019, 3, 3, 20, 0, 0));
   //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

void loop () {
    DateTime robojax = rtc.now();
    Serial.print("Alarm: ");
    for(int i=0;i<6;i++){
      Serial.print(alarm[i]);
      if(i !=5){
        Serial.print("-");
      }
    }
    Serial.println();
    Serial.print("Time:  ");
    Serial.print(robojax.year(), DEC);
    Serial.print("-");
    Serial.print(robojax.month(), DEC);
    Serial.print("-");
    Serial.print(robojax.day(), DEC);
    Serial.print("-");    
    Serial.print(robojax.hour(), DEC);
    Serial.print("-");
    Serial.print(robojax.minute(), DEC);
    Serial.print("-");
    Serial.print(robojax.second(), DEC);
    Serial.println();
 
   if(checkAlarm(robojax)){
    digitalWrite(alarmPin, HIGH);// 
    tone(12,31);
    Serial.print("Alarm Triggered");
    delay(waitTime);
    //while (1);// wait for ever 
   }else{
    digitalWrite(alarmPin, LOW);//
   }
    Serial.println();
    delay(2000);
}

/*
 * 
 * @brief returns true if alarm array matches the current time
 * @param timeNow, is the object for RTC clock
 * @return true if alarm values matches current time
 * Written by Ahmad Shamshiri for Robojax.com 
 * on Friday Jun 14th, 2019 at 20:28 in Ajax, Ontatio, Canada
 * 
 */

boolean checkAlarm(DateTime timeNow){

  if(
   alarm[0] ==timeNow.year()
   &&
   alarm[1] ==timeNow.month()
   &&
   alarm[2] ==timeNow.day()
   &&
   alarm[3] ==timeNow.hour()
   &&
   alarm[4] ==timeNow.minute()
   &&
   alarm[5] ==timeNow.second()
   ){
      return true;
   }else{
      return false;
     
   }
  
}//checkAlarm
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
lcd:GND
lcd:VCC
lcd:SDA
lcd:SCL
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
bz1:1
bz1:2
r1:1
r1:2
gnd1:GND
gnd2:GND
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
GND5VSDASCLSQWRTCDS1307+
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW
gnd3:GND