
//Libraries
#include <Time.h>
//#include <TimeLib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <OneWire.h>
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"

#define DHTPIN 7         //Pin on which is DHT sensor conected
#define DHTYPE DHT22      //Type of DHT sensor(I tried program with DHT11 works the same)

const int chipSelect = 10; //cs or the save select pin from the sd shield is connected to 10.
RTC_DS1307 RTC;


File dataFile;
DateTime now;

LiquidCrystal_I2C lcd(0x27, 16, 2);

DHT dht(DHTPIN, DHTYPE);


int shortDelay = 200;
int nCounter = 3;

float humidity = 0;
float tempCels = 0;

String getTemperature(){
  String txt = "TEMPERATURE:";
  txt.concat(String(tempCels, 0));
  txt.concat((char)223);
  txt.concat("C");
  return txt;
}

String getHumidity(){
  return "HUMIDITY: " +String(humidity, 0) + "%";
}


//method for reading data from dht sensor
void readSensorData(){
  humidity = dht.readHumidity();
  tempCels = dht.readTemperature(); // dht.readTemperature(true); if you want to measure temp in farenheit 
}

//printing temperature on LCD 
void printLcdTemperature(){
  lcd.setCursor(0,0);  
  lcd.print(getTemperature());
}

//humidity printing on LCD  
void printLcdHumidity(){
  lcd.setCursor(0,1);
  lcd.print(getHumidity());
}


void setup() {
  Serial.begin(9600);
  //setup clock
  Wire.begin();
  RTC.begin();
  //Look out for this part, uncoment it if you want to set time on your RTC 
  // following line sets the RTC to the date & time this sketch was compiled
    // uncomment it & upload to set the time, date and start run the RTC!
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  //check or the Real Time Clock is on
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    while(1){}
  }
//setup SD card
   Serial.print("Initializing SD card...");

  // see if the SD card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  //write down the date (year / month / day         prints only the start, so if the logger runs for sevenal days you only findt the start back at the begin.
    now = RTC.now();
    readSensorData();
    dataFile = SD.open("datalog.txt", FILE_WRITE);
    dataFile.print("Start logging on: ");
    dataFile.print(now.year(),DEC);
    dataFile.print('/');
    dataFile.print(now.month(),DEC);
    dataFile.print('/');
    dataFile.print(now.day(),DEC);
    dataFile.println(" ");
    dataFile.println("Celsius              Humidity              Time              Date");
  /*  dataFile.print(tempCels);
    dataFile.print("                 ");
    dataFile.print(humidity);
    dataFile.print("              ");
    dataFile.print(now.hour(),DEC);
    dataFile.print(":");
    dataFile.print(now.minute(),DEC);
    dataFile.print(":");
    dataFile.print(now.second(),DEC);
    dataFile.print("              ");
    dataFile.print(now.year(),DEC);
    dataFile.print('/');
    dataFile.print(now.month(),DEC);
    dataFile.print('/');
    dataFile.println(now.day(),DEC);*/
    dataFile.close();
  
  lcd.clear();
  lcd.backlight();
  lcd.init();
  lcd.begin(16,2);

  dht.begin();
}

void loop() {



  if(nCounter > 2){
    nCounter = 0; //reset counter 
    readSensorData();
  }
  int minute, second;
  now = RTC.now();

// at this part you can change time when you want something to be saved, this can also be used if you are doing something else with RTC and if you want to do something at some exact time 
//this one is used to save data from sensor 
if((now.minute() == 0 || now.minute() == 5 || now.minute() == 10 || now.minute() == 15 || now.minute() == 20 || now.minute() == 25 || now.minute() == 30 || now.minute() == 35 || now.minute() == 40 || now.minute() == 45 || now.minute() == 50 || now.minute() == 55) && now.second() < 5){
  dataFile = SD.open("datalog.txt",FILE_WRITE);

  if (dataFile) {
    dataFile.print(tempCels);
    dataFile.print("                 ");
    dataFile.print(humidity);
    dataFile.print("              ");
    dataFile.print(now.hour(),DEC);
    dataFile.print(":");
    dataFile.print(now.minute(),DEC);
    dataFile.print(":");
    dataFile.print(now.second(),DEC);
  dataFile.print("              ");
  dataFile.print(now.year(),DEC);
    dataFile.print('/');
    dataFile.print(now.month(),DEC);
    dataFile.print('/');
    dataFile.println(now.day(),DEC);
   
    dataFile.close();
    // print to the serial port too:
    Serial.println("data stored");
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
  delay(5000);
  }
  
  //delay(3000);
  
  //writing everything on screen
  printLcdTemperature();
  printLcdHumidity();


  nCounter++;
  delay(shortDelay);
  

}