Code

/*
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
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

* —————————————————————————-
* “THE BEER-WARE LICENSE” (Revision 42):
* <helboj@rpi.edu> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return Janus Helbo
* —————————————————————————-
*/

#include <DHT22.h>
#include <LiquidCrystal.h>
#include <stdio.h>

// Data wire is plugged into port 6 on the Arduino
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
#define DHT22_PIN 6

int MoiPin = A3;
int LigPin = A1;

int SwitchPin = 8;
int ButtonPin = 9;

int LcdPowerPin = 10;
int LcdContrastPin = 7;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);

int i=1;
int On=1;
float Tem;
float Hum;
float Lig;
float Moi;
float MaxTem=0;
float MinTem=0;
float MaxHum=0;
float MinHum=0;
float MaxLig=0;
float MinLig=0;
float MaxMoi=0;
float MinMoi=0;

void setup() {
// put your setup code here, to run once:
pinMode(LcdPowerPin, OUTPUT);
pinMode(LcdContrastPin, OUTPUT);
digitalWrite(LcdPowerPin, HIGH);
analogWrite(LcdPowerPin, 200);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(“EDU-Ponic system”);
lcd.setCursor(0, 1);
lcd.print(“Starting up !”);
delay(3000); // time to read
}

void loop() {

if(digitalRead(SwitchPin)==1 && On==1){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Edu-Ponic System”);
lcd.setCursor(3, 1);
lcd.print(“SLEEPING”);
delay(2000);
lcd.noDisplay();
digitalWrite(LcdPowerPin, LOW);
On=0;
}
else if(digitalRead(SwitchPin)==0 && On==0){
lcd.display();
digitalWrite(LcdPowerPin, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Edu-Ponic System”);
lcd.setCursor(7, 1);
lcd.print(“ON”);
delay(2000);
i=1; // try i=-2000 instead later
On=1;
}

if(digitalRead(ButtonPin)==1 && On==1){
MaxTem=0;
MinTem=0;
MaxHum=0;
MinHum=0;
MaxMoi=0;
MinMoi=0;
MaxLig=0;
MinLig=0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Edu-Ponic System”);
lcd.setCursor(5, 1);
lcd.print(“RESET”);
delay(2000);
}

switch (i) {
case 1:
DHT22_ERROR_t errorCode;
errorCode = myDHT22.readData();
if(errorCode==DHT_ERROR_NONE){
Tem = (myDHT22.getTemperatureC() * 9.00)/5.00 + 32.00;
Hum = myDHT22.getHumidity();
}
Moi = map(analogRead(MoiPin),0,650,0,110);
Lig = map(analogRead(LigPin),160,1023,0,110);

// Save values of max and min if we have new extremes
if(Tem>MaxTem || MaxTem==0){
MaxTem=Tem;
}
if(Tem<MinTem || MinTem==0){
MinTem=Tem;
}

if(Hum>MaxHum || MaxHum==0){
MaxHum=Hum;
}
if(Hum<MinHum || MinHum==0){
MinHum=Hum;
}

if(Moi>MaxMoi || MaxMoi==0){
MaxMoi=Moi;
}
if(Moi<MinMoi || MinMoi==0){
MinMoi=Moi;
}

if(Lig>MaxLig || MaxLig==0){
MaxLig=Lig;
}
if(Lig<MinLig || MinLig==0){
MinLig=Lig;
}

if(On==0){
i=0;
delay(1000*60);
}

break;
case 10:
Display(“Temperature”, “Now:”, Tem, “F”);
break;
case 20:
Display(“Temperature”, “Min:”, MinTem, “F”);
if(MinTem<32){
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Edu-Ponic System”);
lcd.setCursor(5, 1);
lcd.print(“FREEZING !”);
}
break;
case 30:
Display(“Temperature”, “Max:”, MaxTem, “F”);
break;
case 40:
Display(“Humidity”, “Now:”, Hum, “%”);
break;
case 50:
Display(“Humidity”, “Min:”, MinHum, “%”);
break;
case 60:
Display(“Humidity”, “Max:”, MaxHum, “%”);
if(MaxHum>80){
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Humidity High”);
lcd.setCursor(0, 1);
lcd.print(“Eval water needs”);
}
break;
case 70:
if(Moi>0){
Display(“Moisture”, “Now:”, Moi, “%”);
}
else{i=79;}
break;
case 80:
if(MinMoi>0){
Display(“Moisture”, “Min:”, MinMoi, “%”);
}
else{i=89;}
break;
case 90:
if(MaxMoi>0){
Display(“Moisture”, “Max:”, MaxMoi, “%”);
}
else{i=99;}
break;
case 100:
Display(“Lightlevel”, “Now:”, Lig, “%”);
break;
case 110:
Display(“Lightlevel”, “Min:”, MinLig, “%”);
break;
case 120:
Display(“Lightlevel”, “Max:”, MaxLig, “%”);
if(MaxLig<20){
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Lightlevel: Low”);
lcd.setCursor(0, 1);
lcd.print(“Is it night?”);
}
if(MaxLig>90){
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Lightlevel: High”);
lcd.setCursor(0, 1);
lcd.print(“Awesome !”);
}
i=0;
break;
}
i++;
delay(200);
}

void Display(char* Name, char* Time, float Value, char* Unit ) {
// Display a reading
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(Name);
lcd.setCursor(0, 1);
lcd.print(Time);
lcd.setCursor(5, 1);
lcd.print(Value);
lcd.setCursor(11, 1);
lcd.print(Unit);
}