#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Wire.h>
#include <DHT.h>
#define DHTPIN 1
DHT dht( DHTPIN, DHT22); //(sensor pin,sensor type)
// this constant won't change:
//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_RED = 10; //Red LED
const int LED_GREEN = 11; //Green LED
//thermistor
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
// Variables will change:
int RELAY1 = A1; // Heater
int RELAY2 = A2; // Fan
int Range = 3; // Temp +/- Range
int SetPoint = 30; // Default temp
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
//=================================================================
// SETUP
//=================================================================
void setup() {
lcd.init();
lcd.backlight();
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
// initialize the button pin as a input:
pinMode(down_key, INPUT_PULLUP);
pinMode(up_key, INPUT_PULLUP);
dht.begin();
// timer.setInterval(2500L, sendSensor);
// 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");
digitalWrite(LED_GREEN, HIGH); //Green LED Off
digitalWrite(LED_RED, LOW); //Red LED On
digitalWrite(RELAY1, LOW); //Turn off Heater Relay
digitalWrite(RELAY2, LOW); //Turn off Fan Relay
delay(2000);
lcd.clear();
}
//=================================================================
// LOOP
//=================================================================
void loop() {
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;
}
}
//Display Set point on LCD
float t = dht.readTemperature();
lcd.setCursor(0, 0);
lcd.print("Temperature:"); //Do not display entered keys
lcd.print(t);
lcd.setCursor(0, 1);
lcd.print("Set Point:");
lcd.print(SetPoint);
lcd.print("C ");
delay(100); //Update at every 100mSeconds
}
void sendSensor(){
float h = dht.readHumidity();
float t = dht.readTemperature();
//Check Temperature is in limit
if (t == SetPoint)
{
digitalWrite(RELAY1, LOW); //Turn off heater
digitalWrite(LED_RED, LOW); //Turn off Red LED
digitalWrite(LED_GREEN, HIGH); //Turn on Green LED
}
// If temp is over limit turn on fan /Turn off heater
if (t > (SetPoint + Range))
{
digitalWrite(RELAY1, LOW); //Turn off heater
Serial.print("heater off");
digitalWrite(RELAY2, HIGH); //Turn on fan
digitalWrite(LED_RED, LOW); //Turn off Red LED
digitalWrite(LED_GREEN, HIGH); //Turn on Green LED
}
//if temp is below limt turn on heater / turn off fan
if (t< (SetPoint - Range) || t < SetPoint)
{
digitalWrite(RELAY1, HIGH); //Turn on heater
Serial.print("heater on");
digitalWrite(RELAY2, LOW); //Turn off fan
digitalWrite(LED_GREEN, LOW); //Turn off Green LED
digitalWrite(LED_RED, HIGH); //Turn on RED LED
}
}