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

RTC_DS1307 RTC;
LiquidCrystal_I2C lcd (0x27,20,4);


//RTC_DS1307 RTC; // เวลา

const int soilMoisture = 34; // soil moisture
const float BETA = 3950; // should match the Beta Coefficient of the thermistor

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.begin(20,4);//กา หนดชนิดจอแสดงผลที่ใชข้นาด 16x2
  Wire.begin();//เรียกใชก้ารส่งขอ้มูลI2C
  RTC.begin();   // เรียกใช้งานก่อน RTC.adjust
  //RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  RTC.adjust(DateTime(__DATE__, __TIME__)); // เวลา
                            // เวลา
  analogReadResolution(10);

  lcd.setCursor(0,1);
    lcd.print("     WELCOME TO");
  lcd.setCursor(0,2);
    lcd.print("   >>> MyFarm <<<");
    delay(2000);
    lcd.clear();

  pinMode(soilMoisture, INPUT);  //


}

void loop() {

  DateTime time = RTC.now();

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

  //Serial.print(soilMoisture);
  lcd.setCursor(0,0);
    lcd.print("Temperature: ");
  lcd.setCursor(0,1);
    lcd.print(celsius);
    lcd.print((char)223);
    lcd.println("C");


//Full Timestamp
 //Serial.println(String("DateTime::TIMESTAMP_FULL:\t")+time.times(DateTime::TIMESTAMP_FULL));
 //Date Only
 Serial.println(String("DateTime::TIMESTAMP_DATE:\t")+time.timestamp(DateTime::TIMESTAMP_DATE));
 //Full Timestamp
 Serial.println(String("DateTime::TIMESTAMP_TIME:\t")+time.timestamp(DateTime::TIMESTAMP_TIME));
 Serial.println("\n");

 lcd.setCursor(2,2);
 lcd.print(time.day());
 //lcd.print(time.timestamp(DateTime::TIMESTAMP_DATE));
 lcd.setCursor(4,3);
 lcd.print(time.timestamp(DateTime::TIMESTAMP_TIME));
 //Delay 5s


  delay(1000);

}
GND5VSDASCLSQWRTCDS1307+