// ME 106
// Homework 5 - Auto Climate Control Simulation
// by Yasar Iqbal
// Add libraries
#include "DHT.h" // Temperature sensor library
#include <LiquidCrystal_I2C.h> // LCD library
#include <IRremote.h> // IR remote library
// Define integers
int TempCurrent = 0; // Set the current temperature initially to 0
int TempSet = 0; // Set the set temperature initially to 0
int TempAdjust = 0; // Set the adjust temperature initially to 0
// IR remote button definitions
#define Button_1 0xFF40BF // IR remote + button
#define Button_2 0xFF19E6 // IR remote - button
// The following buttons used for troubleshooting
#define Button_3 0xFF0CF3 // IR remote 1 button set to show TempCurrent
#define Button_4 0xFF18E7 // IR remote 2 button set to show TempSet
#define Button_5 0xFF5EA1 // IR remote 3 button set to show TempAdjust
//Temperature sensor stuff
#define DHTPIN 2 // Temperature sensor input pin
// Comment out what type of sensors you are not using
// #define DHTTYPE DHT11 // Temperature Sensor DHTTYPE DHT11
// #define DHTTYPE DHT21 // Temperature Sensor DHTTYPE DHT21
#define DHTTYPE DHT22 // Temperature Sensor DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE); // Temperature sensor pin and type
// LCD stuff
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD we are using
// IR remote stuff
int receiver = 5; //initialize pin 5 as recevier pin.
uint32_t Previous;
IRrecv irrecv(receiver); //create a new instance of receiver
decode_results results;
// LED stuff
int RedLED = 8; // Red led is board input 8
int BlueLED = 9; // Blue led is board input 9
// Millis stuff
const unsigned long eventInterval = 60000; // Run millis function every 1 minute = 60 sec = 60,000 ms
unsigned long previousTime = 0;
void setup() {
Serial.begin(9600);
pinMode(8, OUTPUT); // Red led is an output
pinMode(9, OUTPUT); // Blue led is an output
dht.begin(); // initialize the temperature sensor
lcd.backlight(); // turn on lcd backlight
lcd.init(); // initialize lcd
irrecv.enableIRIn(); //start the IR receiver
TempCurrent = (int)dht.readTemperature(true);
}
// Function for millis (To check temperature sensor every 1 minute)
void millisfunction() {
unsigned long currentTime = millis(); // Updates frequently
// This event happens every 60,000 ms = 60 seconds = 1 minute
if (currentTime - previousTime >= eventInterval) {
// dht.readTemperature(true) shows temperature in drgrees F
// dht.readTemperature(false) shows temperature in degrees C
TempCurrent = (int)dht.readTemperature(true);// Update the current temperature
// Update the timing for the next time around
previousTime = currentTime;
}
}
// Function to update the LCD screen
void lcdfunction() {
lcd.clear();
//TempCurrent = (int)dht.readTemperature(true);
TempSet = TempCurrent + TempAdjust;
lcd.setCursor(0,0); // Set the cursor on the first row and column
lcd.print("Temp. "); // Print the current temperature
lcd.print(TempCurrent);
lcd.print(" F");
lcd.setCursor(0,1); // Set the cursor on the second row, first column
lcd.print("Setpoint "); // Print the set point temperature
lcd.print(TempSet);
lcd.print(" F");
delay(1000); // Wait 1000 ms or 1 second for LCD refresh
lcd.clear();
}
// Function to read the remote input
void IRinputfunction() {
if (irrecv.decode(&results)) { //if we have received an IR signal
if (results.value==0xFFFFFFFF) {
results.value=Previous;
}
switch(results.value) {
case Button_1 : TempAdjust = TempAdjust + 1;
break;
case Button_2 : TempAdjust = TempAdjust - 1;
break;
// Buttons set up to troubleshoot and find out what the temp values are
case Button_3 : Serial.println (TempCurrent); //digitalWrite(12, LOW);
break;
case Button_4 : Serial.println (TempSet); //digitalWrite(12, LOW);
break;
case Button_5 : Serial.println (TempAdjust);; //digitalWrite(12, LOW);
break;
}
Serial.println (results.value, HEX); //display HEX results
irrecv.resume(); //next value
}
Previous=results.value;
}
// Function to turn on correct led and fan
void ledfunction() {
if (TempAdjust > 2) // If the adjustment is >2
digitalWrite(8, HIGH); // Then turn on the red led
else
digitalWrite(8, LOW); // Else, turn off the red led
if (TempAdjust < -2) // If the adjustment is <-2
digitalWrite(9, HIGH); // Then turn on the blue led
else
digitalWrite(9, LOW); // Else, turn off the blue led
}
// The mail loop
void loop() {
millisfunction(); // Call the millis function
lcdfunction(); // Call the lcd function
IRinputfunction(); // Call the IR input function
ledfunction(); // Call the led function
}