#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
// 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() {
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);
// 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);
}
//=================================================================
// LOOP
//=================================================================
void loop() {
int analogValue = analogRead(A0);
float Temperature = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
lcd.setCursor(0, 0);
lcd.print("Temperature:"); //Do not display entered keys
lcd.print(Temperature);
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
lcd.setCursor(0, 1);
lcd.print("Set Point:");
lcd.print(SetPoint);
lcd.print("C ");
//Check Temperature is in limit
if (Temperature == 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 (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_GREEN, HIGH); //Turn on Green 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_GREEN, LOW); //Turn off Green LED
digitalWrite(LED_RED, HIGH); //Turn on RED LED
}
delay(100); //Update at every 100mSeconds
}