#include <LiquidCrystal_I2C.h>


#include <DHT.h>

#define DHTPIN 2     //  D4 what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT22   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

#define EXE_INTERVAL_1 10000 // for using millis
#define EXE_INTERVAL_2 30000  // for millis

unsigned long lastExecutedMillis_1 = 0; // vairable to save the last executed time for code block 1
unsigned long lastExecutedMillis_2 = 0; // vairable to save the last executed time for code block 2


// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
/*  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 */
 

void setup() {
  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
  lcd.begin(16, 2);
  dht.begin();
  pinMode(A0,INPUT);
   pinMode(A1,INPUT);
  pinMode(2,INPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);

  lcd.setCursor(0, 0);
  lcd.print("Medhayantra");
  lcd.setCursor(0, 1);
  lcd.print("GROW CHAMBER");
  lcd.setCursor(10, 1);
  delay(2000);
  lcd.clear();
  
}
void loop() 
{
  
  int del= analogRead(A1);
  int dell=map(del,0,1024,0,255);
  lcd.setCursor(0, 0);
  lcd.print("timer start");
  lcd.setCursor(0, 1);
  lcd.print(del);
  lcd.setCursor(8, 1);
  lcd.print(dell);
  delay(1000);
  lcd.clear();
  delay(1000);

  
  int lux,lux1;
lux1 = analogRead(A0);  //put Sensor insert into soil
Serial.println(lux1);
lux = map(lux1, 0, 1024, 0, 100);
Serial.println(lux);
if(lux <=50)
{
  digitalWrite(5,LOW);
  Serial.println("GrowLight off");
  lcd.setCursor(0, 0);
  lcd.print("Light Off");
  lcd.setCursor(0, 1);
  lcd.print("GrowLight Off");
  delay(1000);
  lcd.clear();
}
 if (lux >50)
{
  digitalWrite(5,HIGH);
  Serial.println("Grow LIGHT on");
  lcd.setCursor(0, 0);
  lcd.print("LIGHT on");
  lcd.setCursor(0, 1);
  lcd.print("");
  delay(250);
  lcd.clear();
}
 float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) ) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.println(t);
  lcd.setCursor(0, 0);
  lcd.print("Temperature:");
  lcd.setCursor(12, 0);
  lcd.print(t);
  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
  lcd.setCursor(10, 1);
  lcd.print(h);

  delay(1500);
  lcd.clear();

  if (t >35)
    {
digitalWrite(6,HIGH); //exhaust
Serial.println("Exhaust fan ON");
} 
if (t <33)
    {
digitalWrite(6,LOW); //exhaust
Serial.println("Exhaust fan OFF");
} 

if (h<50)
{
  digitalWrite(7,HIGH);
  delay(1000);
  
}

    unsigned long currentMillis = millis();

  if (currentMillis - lastExecutedMillis_1 >= EXE_INTERVAL_1) {
    lastExecutedMillis_1 = currentMillis; // save the last executed time

  // your code block 1

    digitalWrite(7,HIGH);
Serial.println("fan On");
    
  }

  if (currentMillis - lastExecutedMillis_2 >= EXE_INTERVAL_2) {
    lastExecutedMillis_2 = currentMillis; // save the last executed time
//Block 2

digitalWrite(7,LOW);
Serial.println("fan Off");
    
  }
}