/**************************************************************************
 Oled 0.96 i2c       SDA-A4
                     SCL-A5
 RES                 4
 DS1307              SDA-A4
                     SCK-A5
 LDR                 A0
 Vys.pro rele        A1 (tranzistor TIP120)                                           
 **************************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

#define OLED_RESET 4

Adafruit_SSD1306 display(OLED_RESET);
tmElements_t tm;

float RatioFactor=4.115;           //Resistors Ration FActor

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
  // 'skoda Logo', 128x64px
  const unsigned char myBitmap [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbf, 0xfa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x80, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x0b, 0x50, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0xff, 0xff, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x8f, 0xe0, 0x3f, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x3d, 0x00, 0x00, 0xf1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x70, 0x07, 0x80, 0x3c, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0xc0, 0x7f, 0xc0, 0x0e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x80, 0x3f, 0xe0, 0x03, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x1e, 0x1f, 0xf8, 0x03, 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0x3f, 0x87, 0xfc, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x01, 0x18, 0x7f, 0xe7, 0xfe, 0x00, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x01, 0x78, 0xff, 0xf1, 0xfe, 0x00, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x02, 0x70, 0x7f, 0xf9, 0xfe, 0x00, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x06, 0x60, 0x0f, 0xff, 0xff, 0x00, 0x0c, 0x80, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x04, 0xc7, 0x07, 0xff, 0xff, 0x80, 0x0c, 0xc0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x04, 0xc7, 0xc3, 0xff, 0xff, 0x80, 0x0e, 0x40, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x09, 0xcf, 0xf9, 0xff, 0xff, 0x80, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x09, 0x8f, 0xff, 0xff, 0xff, 0x80, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x09, 0x8f, 0xff, 0xff, 0xff, 0xc0, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x09, 0x1f, 0xff, 0xff, 0xf7, 0x80, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x13, 0x9f, 0xff, 0xff, 0xff, 0xc0, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x13, 0x01, 0xff, 0xff, 0xff, 0xc0, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x3f, 0xff, 0xff, 0xc0, 0x03, 0xa0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x1f, 0xff, 0xff, 0x80, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x13, 0x80, 0x07, 0xff, 0xff, 0xc0, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x03, 0xff, 0x8f, 0x80, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x13, 0x80, 0x01, 0xff, 0x17, 0x80, 0x07, 0x20, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x09, 0x80, 0x00, 0xff, 0x07, 0x80, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x09, 0x80, 0x01, 0xff, 0x87, 0x00, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x09, 0xc0, 0x00, 0xff, 0xcf, 0x80, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x04, 0xcf, 0xf8, 0xff, 0xfb, 0x7c, 0x0e, 0x40, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x04, 0xe7, 0xff, 0xff, 0xfe, 0x7e, 0x0c, 0x80, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x02, 0x67, 0xff, 0xff, 0xff, 0xd7, 0x9c, 0x80, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x02, 0x71, 0xff, 0xff, 0xff, 0xff, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x01, 0x38, 0xf8, 0x00, 0x00, 0x7c, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x01, 0x18, 0x60, 0x00, 0x00, 0x28, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x00, 0x00, 0x03, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x80, 0x00, 0x00, 0x07, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0xe0, 0x00, 0x00, 0x0e, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x78, 0x00, 0x00, 0x3c, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x01, 0xf1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0xf1, 0x5f, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc1, 0xff, 0xfc, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x0d, 0x40, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x05, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xbf, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x12, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x22, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xff, 0xe7, 0x1e, 0x3f, 0xf1, 0xff, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0x3c, 0x70, 0x39, 0xe3, 0x83, 0xf8, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xe0, 0x06, 0x70, 0x70, 0x39, 0xe1, 0x83, 0xb8, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xff, 0xe7, 0xf0, 0x70, 0x39, 0xe1, 0x87, 0x1c, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x3f, 0xe7, 0xf8, 0x70, 0x39, 0xe1, 0x8f, 0x1c, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x67, 0x1c, 0x70, 0x39, 0xe3, 0x8f, 0xfe, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xff, 0xf6, 0x1e, 0x3f, 0xf1, 0xff, 0x0e, 0xae, 0x00, 0x00, 0x00, 0x00 
};

void setup() {                
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  display.clearDisplay(); 
  display.drawBitmap(0, 0, myBitmap, 128, 64, WHITE);
  display.display();
  delay(4000); 
  display.clearDisplay();
  pinMode(A0,INPUT);               //Analog A2 in LDR Pin
  pinMode(A1,OUTPUT);              //Světla On/Off Pin
  
}

void loop() {
  RTC.read(tm);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(17,0);                    
  display.print(tm.Hour);
  display.print(":");
  display.print(tm.Minute);
  display.print(":");
  display.print(tm.Second);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(35,18);  
  display.print(tm.Day);
  display.print('.');                   
  display.print(tm.Month);
  display.print('.');  
  display.print(tmYearToCalendar(tm.Year));
  display.display();
  delay(1000);
  display.clearDisplay();


//Head Light On/Off
 int digital=analogRead(A0);    //LDR in
     display.println(digital);
     if(digital>=700){
     digitalWrite(A1,HIGH);    //Switch on Lights
     display.clearDisplay();
     display.setTextSize(2);
     display.setTextColor(WHITE);
     display.setCursor(35, 30); 
     display.println("Zapla");
     display.setTextSize(2);
     display.setTextColor(WHITE);
     display.setCursor(28, 50); 
     display.println("svetla");
     delay(300);
        
   } else{
     digitalWrite(A1,LOW);    //Switch off Lights
     display.clearDisplay();
     display.setTextSize(2);
     display.setTextColor(WHITE);
     display.setCursor(35, 30); 
     display.println("Denni");
     display.setTextSize(2);
     display.setTextColor(WHITE);
     display.setCursor(28, 50);
     display.println("Svetla");
     delay(300);
     
  } 
     
}   
   
