#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
#include "pitches.h"
RTC_DS3231 rtc;
char days_week[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);



int HOURS;
int MINUTES;
int SECONDS;
int alarm_switch =0;




#define LED 13

#define buzzerpin 7
int time = 500;
int double_frequency_switch =0;


void setup() {
  // putting setup code here, to run once
  pinMode(buzzerpin, OUTPUT);
  Serial.begin(9600); // setup serial


       //Serial.println("Press button to set time");
       Serial.println("ENTER HOURS, press Enter. Then enter MINUTES press enter");
       Serial.println("After Entering, press the button to submit the time");




  //Serial.begin(9600);
  delay(3000); // wait for console opening
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    rtc.adjust(DateTime(2020, 1, 28, 12, 56, 0));



  }
  


display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)// Check your I2C address and enter it here, in Our case address is 0x3C
  display.clearDisplay();
  display.display(); // this command will display all the data which is in buffer
  display.setTextColor(WHITE, BLACK);
  display.drawRect(117, 25, 3, 3, WHITE);     // Put degree symbol ( ° )
  draw_text(0, 25, "ENTER TIME, PRESS BTN", 1);




  

 attachInterrupt(digitalPinToInterrupt(3), printer, RISING); // option 2 to configure hardware/external interupt

}


void printer(){
 // Serial.println("INTERRUPT TEST");
  double_frequency_switch = 6;

}




void draw_text(byte x_pos, byte y_pos, char *text, byte text_size) {
  display.setCursor(x_pos, y_pos);
  display.setTextSize(text_size);
  display.print(text);
  display.display();
}



void loop() {
  // putting the main code here, to run repeatedly:
 // delay(2000);

  


  if(double_frequency_switch == 5 ){ // checks if double frequency is 1
  
    

        
        
  }else if(double_frequency_switch == 6){
 
//Serial.println("ENTER HOURS, press Enter. Then enter MINUTES press enter");

    delay(1000);
    if( Serial.available() >0){
     HOURS = Serial.parseInt(); //dataIn now holds 314
      // say what you got:
      if(HOURS > 0){
        Serial.print("hours received: ");
    Serial.println(HOURS);

      }
    
    }

      delay(1000);
    if( Serial.available() >0){
     MINUTES = Serial.parseInt(); //dataIn now holds 314
      // say what you got:
    Serial.print("mins received: ");
    Serial.println(MINUTES);
       // delay(1000);
     // update_Screen(HOURS, MINUTES,00);
     swytch_temp(HOURS,MINUTES);
     


    }

  }
       //update_Screen(HOURS, MINUTES, SECONDS);

       //swytch_temp(HOURS,MINUTES);

}




void swytch_temp(int h, int m){
   while(1){

     if(alarm_switch == 1){
      // digitalWrite(buzzerpin, HIGH);
      tone(buzzerpin, NOTE_C5, 2000);
        display.clearDisplay();
        display.setTextSize(2);
       display.setCursor(15,9);
         display.print("ALARM!");

       display.display(); 

    delay(4000);
    display.clearDisplay();
    alarm_switch =0;
     }else if(alarm_switch == 0){
       noTone(buzzerpin);


    DateTime now = rtc.now();
  /*============Display Date=================*/
  display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print(days_week[now.dayOfTheWeek()]);
char current_Date [16];
uint8_t this_Day, this_Month ;
this_Day = now.day();
this_Month = now.month();
sprintf (current_Date, "%02d/%02d/", this_Day, this_Month); //add leading zeros to the day and month
display.setTextSize(1);
display.setCursor(62,0);
display.print(current_Date);
display.setTextSize(1);
display.setCursor(102,0);
display.print(now.year(), DEC);
/*================Display Time================*/
char buffer [16];
uint8_t this_Second, this_Minute, this_Hour;
this_Second = now.second();
this_Minute = now.minute();
this_Hour = now.hour();

/*
this_Second = 00;
this_Minute = 00;
this_Hour = 00;
*/

sprintf (buffer, "%02d:%02d:%02d", this_Hour, this_Minute, this_Second);
//sprintf (buffer, "%02d:%02d:%02d", 00, 00, 00);
display.setTextSize(2);
display.setCursor(15,9);
display.print(buffer);
display.display(); 
//display.clearDisplay();
/*
delay(2000);
     display.setTextSize(2);
     display.setCursor(15,9);
     display.print(h);
     display.print(m);
    display.display(); 

*/


if(this_Hour==h && this_Minute==m){

    alarm_switch =1;

   





}
}

     }

   
}








GND5VSDASCLSQWRTCDS1307+