//Arduino Greenhouse project. Coded by Aaron Maybury A.K.A GlassCurtain
//Used sample code for the DHT11 By Ladyada
#include <LiquidCrystal.h>
#include <DFR_Key.h>
#include <DHT.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
DFR_Key keypad;
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
int sensePin = 21; //Arduino Pin number the sensor plugged into. The schematics that come with the LCD shield are incorrect.
//Pin 2 on the arduino is actually pin 5 on the LCD shield. The headers that plug into the arduino's Digital pins 0 - 7, on the LCD Shield the exact middle pin is pin 2 on the arduino.
//Use your multimeter to test continuity to find the pin you want.
int currentButton = 0; //Current button pressed
int displayTemp = 1; //Keeps track of what screen is currently being displayed. If displatTemp is 1 temp is showing. If 0 Humidity is showing.
int displayHumid = 0;
int setTemp = 25; //Default to set the desired temp
int setHumid = 50; //Defualt to set the desired humidity
int outputPin = 28; //Output pin to the relay to trip the fans
DHT dht(sensePin, DHTTYPE);
void setup()
{
pinMode(outputPin, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
//Serial.begin(9600);
dht.begin();
}
void loop() {
currentButton = keypad.getKey(); //Read button press
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float temp = t-3 ; //Convert *C to *F.
if (displayTemp == 1) //Shows temp screen upon first boot
{
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TEMP ");
lcd.print(temp);
lcd.print(" ");
lcd.print((char)223); // degree symbol
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("SET TEMP ");
lcd.print(setTemp);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
displayHumid = 0;
}
else{
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HUMIDITY ");
lcd.print(int(h));
lcd.print(" %");
lcd.setCursor(0,1);
lcd.print("SET HUMID ");
lcd.print(setHumid);
lcd.print(" %");
displayHumid = 1;
}
// Reads which buttons are pressed.
// No button = 0
// Select button = 1
// Left button = 2
// Up Button = 3
// Down Button = 4
// Right Button = 5
if (currentButton == 5 && displayTemp == 1)
{
displayTemp = 0; //Turns display temp off so it will show humidity after pressing right button
}
// The following code toggles the screen when the corresponding button is pressed.
// Also sets the screens to wrap around the display.
// So if you hit right button once it will show humidity and if you hit right button again it will show temp.
if (currentButton == 5 && displayHumid == 1)
{
displayTemp = 1;
}
if (currentButton == 2 && displayTemp == 1)
{
displayTemp = 0;
}
if (currentButton == 2 && displayHumid == 1)
{
displayTemp = 1;
}
if (currentButton == 3 && displayTemp == 1) //When up button is pressed and the temp screen is showing, increment the set temp
{
++setTemp;
}
if (currentButton == 4 && displayTemp == 1) //When down button is pressed with the temp screen showing, decrement set temp
{
--setTemp;
}
if (currentButton == 3 && displayTemp == 0) //When up button is pressed and the humidity screen is showing, inc the set humidity
{
++setHumid;
}
if (currentButton == 4 && displayTemp == 0) //When down button is pressed and humidity screen is showing, dec the set humidity
{
--setHumid;
}
if(temp >= setTemp) //Trip relay if temp is exceeded
{
//Serial.print(temp);
digitalWrite(outputPin, HIGH);
}
else
{
digitalWrite(outputPin, LOW);
}
//if (h >= setHumid && digitalRead(outputPin) == LOW) //If humidity is exceeded and fan is not on due to temp exceeded, turn fan on.
//{
// digitalWrite(outputPin, HIGH);
//}
}