/**************************************************************************
 This is an example for our Monochrome OLEDs based on SSD1306 drivers

 Pick one up today in the adafruit shop!
 ------> http://www.adafruit.com/category/63_98

 This example is for a 128x64 pixel display using I2C to communicate
 3 pins are required to interface (two I2C and one reset).

 Adafruit invests time and resources providing this open
 source code, please support Adafruit and open-source
 hardware by purchasing products from Adafruit!

 Written by Limor Fried/Ladyada for Adafruit Industries,
 with contributions from the open source community.
 BSD license, check license.txt for more information
 All text above, and the splash screen below must be
 included in any redistribution.
 **************************************************************************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"


RTC_DS3231 rtc_obj;
char days_week[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const float BETA = 3950; // should match the Beta Coefficient of the thermistor

bool output;

const byte interruptPin = 19;
volatile byte state = LOW;

  int buttonState;
  int count;



//// SWITCH ////

static const int buttonPin = 12;                    // switch pin
int buttonStatePrevious = LOW;                      // previousstate of the switch

unsigned long minButtonLongPressDuration = 3000;    // Time we wait before we see the press as a long press
unsigned long buttonLongPressMillis;                // Time in ms when we the button was pressed
bool buttonStateLongPress = false;                  // True if it is a long press

const int intervalButton = 50;                      // Time between two readings of the button state
unsigned long previousButtonMillis;                 // Timestamp of the latest reading

unsigned long buttonPressDuration;                  // Time the button is pressed in ms

//// GENERAL ////

unsigned long currentMillis;          // Variabele to store the number of milleseconds since the Arduino has started

float celsius;
int SWITCHER=1;


void setup() {

    Serial.begin(9600);
    Serial.println("STARTED...");
    Serial.println("Long Press to Turn on Temperature Sensor..");
    Serial.println("Short Press to Turn off Temperature Sensor..");

    pinMode(LED_BUILTIN, OUTPUT);

  pinMode(interruptPin, INPUT_PULLUP);

  //Serial.begin(9600);
  delay(3000); // wait for console opening
  if (! rtc_obj.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (rtc_obj.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    rtc_obj.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);
  


}
void loop(){

   currentMillis = millis(); 

     button_modes();
 

}




void Draw_Text(byte xPosition, byte yPosition, char *text, byte text_size) {
  display.setCursor(xPosition, yPosition);
  display.setTextSize(text_size);
  display.print(text);
  display.display();
}


void wrongTime(){
  while(1){
      //rtc.adjust(DateTime(2020, 1, 28, 12, 56, 0));

      int analogValue = analogRead(A0);
 //  int analogValue = analogRead(A0);
  float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
  Serial.print("Temperature: ");
  Serial.print(celsius);
  Serial.println(" ℃");
  //delay(1000);
       DateTime now = rtc_obj.now();
  /*============Display Date=================*/
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();
//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 Temperature=====================*/
display.setTextSize(1);
display.setCursor(82,25);
display.print(celsius);
display.display(); 
  }

}





// Function for reading the button state
void button_modes() {


      int analogValue = analogRead(A0);
 //  int analogValue = analogRead(A0);
   celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;

  // If the difference in time between the previous reading is larger than intervalButton
  if(currentMillis - previousButtonMillis > intervalButton) {
    
    // Read the digital value of the button (LOW/HIGH)
    int buttonState = digitalRead(buttonPin);    

    // If the button has been pushed AND
    // If the button wasn't pressed before AND
    // IF there was not already a measurement running to determine how long the button has been pressed
    if (buttonState == HIGH && buttonStatePrevious == LOW && !buttonStateLongPress) {
      buttonLongPressMillis = currentMillis;
      buttonStatePrevious = HIGH;
      Serial.println("Button pressed");
       // display.clearDisplay();
        SWITCHER = 1;
        


    }

    // Calculate how long the button has been pressed
    buttonPressDuration = currentMillis - buttonLongPressMillis;

    // If the button is pressed AND
    // If there is no measurement running to determine how long the button is pressed AND
    // If the time the button has been pressed is larger or equal to the time needed for a long press
    if (buttonState == HIGH && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
      buttonStateLongPress = true;
      Serial.println("Button long pressed");
      SWITCHER = 0;

     

    }
      
    // If the button is released AND
    // If the button was pressed before
    if (buttonState == LOW && buttonStatePrevious == HIGH) {
      buttonStatePrevious = LOW;
      buttonStateLongPress = false;
      Serial.println("Button released");

      // If there is no measurement running to determine how long the button was pressed AND
      // If the time the button has been pressed is smaller than the minimal time needed for a long press
      
      if (buttonPressDuration < minButtonLongPressDuration) {
       // Serial.println("Button pressed shortly");
          
      }
    }
    
    // store the current timestamp in previousButtonMillis
    previousButtonMillis = currentMillis;

  }

  if (SWITCHER == 0){
          DateTime now = rtc_obj.now();
  /*============Display Date=================*/
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();
sprintf (buffer, "%02d:%02d:%02d", this_Hour, this_Minute, this_Second);
//sprintf (buffer, "%02d:%02d:%02d", 00, 00, 00);
 display.setTextColor(WHITE, BLACK);
  display.drawRect(117, 25, 3, 3, WHITE);     // Put degree symbol ( ° )
  Draw_Text(0, 25, "TEMPERATURE =", 1);
  Draw_Text(122, 25, "C", 1);
display.setTextSize(2);
display.setCursor(15,9);
display.print(buffer);
display.setTextSize(1);
display.setCursor(82,25);
display.print(celsius);


display.display();
/*=============Display Temperature=====================*/

 }else if(SWITCHER == 1){

   swytch_temp();


}

}


void swytch_temp(){
   
    DateTime now = rtc_obj.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();

//Serial.print("Day:");
//Serial.println(this_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();
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(); 

if(this_Hour == 14 && this_Minute==17){
  Serial.println("ALARM!!");
}



}

void alarmSystem(){
  //display alarm Screen
 // Serial.println();
    
    DateTime now = rtc_obj.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();
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(); 



}
GND5VSDASCLSQWRTCDS1307+