#include <LiquidCrystal.h>
#include <DHT.h>
//Constants
#define DHTPIN 13 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
//Key connections with arduino
const byte down_key = 2; // the pin that the pushbutton is attached to
const byte up_key = 3;
//Led connections with arduino
const int LED_GREEN = 10; //Green LED
const int LED_RED = 11; //Red LED
const int LED_BLUE = 12; //Blue LED
// Variables will change:
int RELAY1 = A1; // Heater
int RELAY2 = A2; // Fan
int Range = 3; // Temp +/- Range
int SetPoint = 30; // Default temp
int chk;
float hum; //Stores humidity value
// Buttons
int buttonPushCounter_down_key = 0; // counter for the number of button presses
boolean ButtonState_down_key = 0; // current state of the button
boolean lastButtonState_down_key = 0; // previous state of the button
int buttonPushCounter_up_key = 0; // counter for the number of button presses
boolean ButtonState_up_key = 0; // current state of the button
boolean lastButtonState_up_key = 0; // previous state of the button
unsigned long previousMillis = 0;
const long interval = 2000;
//=================================================================
// SETUP
//=================================================================
void setup()
{
analogReference(INTERNAL);
//initialize outputs
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
// initialize the button pin as a input:
pinMode(down_key, INPUT_PULLUP);
pinMode(up_key, INPUT_PULLUP);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Temp Controller");
lcd.setCursor(0, 1); //Move coursor to second Line
lcd.print(" Version 1");
//Turn on leds at start up
digitalWrite(LED_GREEN, HIGH); //Green LED on
digitalWrite(LED_RED, HIGH); //Red LED On
digitalWrite(LED_BLUE, HIGH); //blue LED on
digitalWrite(RELAY1, LOW); //Turn off Heater Relay
digitalWrite(RELAY2, LOW); //Turn off Fan Relay
delay(1000);
//turn off blue & red led
digitalWrite(LED_RED, LOW); //Red LED Off
digitalWrite(LED_BLUE, LOW); //blue LED off
dht.begin();
}
//=================================================================
// LOOP
//=================================================================
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
// read temp status
float Temperature = dht.readTemperature();
//analogRead(0) * 0.1039;
//Print temp status to lcd
lcd.setCursor(0, 0);
lcd.print("Temperature:"); //Do not display entered keys
lcd.print(Temperature);
//Check Temperature is in limit
if (Temperature == SetPoint)
{
digitalWrite(RELAY1, LOW); //Turn off heater
digitalWrite(LED_RED, LOW); //Turn off Red LED
digitalWrite(LED_BLUE, LOW); //Turn off blue LED
}
// If temp is over limit turn on fan /Turn off heater
if (Temperature > (SetPoint + Range))
{
digitalWrite(RELAY1, LOW); //Turn off heater
digitalWrite(RELAY2, HIGH); //Turn on fan
digitalWrite(LED_RED, LOW); //Turn off Red LED
digitalWrite(LED_BLUE, HIGH); //Turn on blue LED
}
//if temp is below limt turn on heater / turn off fan
if (Temperature < (SetPoint - Range) || Temperature < SetPoint)
{
digitalWrite(RELAY1, HIGH); //Turn on heater
digitalWrite(RELAY2, LOW); //Turn off fan
digitalWrite(LED_BLUE, LOW); //Turn off blue LED
digitalWrite(LED_RED, HIGH); //Turn on RED LED
}
}
//Display Set point on LCD
lcd.setCursor(0, 1);
lcd.print("Set Point:");
lcd.print(SetPoint);
lcd.print("C ");
//=================================================================
// Button timer
//=================================================================
static unsigned long timer = 0;
unsigned long interval = 20;
if (millis() - timer >= interval)
{
timer = millis();
// read the pushbuttons input pin:
ButtonState_down_key = digitalRead(down_key);
ButtonState_up_key = digitalRead(up_key);
// compare the ButtonState_down_key to its previous state
if (ButtonState_down_key != lastButtonState_down_key)
{
// if the state has changed, increment the counter
if (ButtonState_down_key == LOW)
{
// if the current state is LOW then the button went from off to on:
buttonPushCounter_down_key++;
if (SetPoint > 0)
{
SetPoint--;
}
}
lastButtonState_down_key = ButtonState_down_key;
}
// compare the ButtonState_up_key to its previous state
if (ButtonState_up_key != lastButtonState_up_key)
{
// if the state has changed, increment the counter
if (ButtonState_up_key == LOW)
{
// if the current state is LOW then the button went from off to on:
buttonPushCounter_up_key++;
if (SetPoint < 100)
{
SetPoint++;
}
}
lastButtonState_up_key = ButtonState_up_key;
}
}
}