#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(0x3F, 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(2,INPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
}
void loop()
{
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("");
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);
if (t >35)
{
digitalWrite(6,HIGH); //exhaust
Serial.println("Exhaust fan ON");
}
if (t <33)
{
digitalWrite(6,LOW); //exhaust
Serial.println("Exhaust fan OFF");
}
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");
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
r1:1
r1:2