#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// House Icon
byte housechar1[8]={B00000, B00001, B00011, B00011, //Row 0, Col 0
B00111, B01111, B01111, B11111,};
byte housechar2[8]={B11111, B11111, B11100, B11100, //Row 1, Col 0
B11100, B11100, B11100, B11100,};
byte housechar3[8]={B00000, B10010, B11010, B11010, //ROW 0, Col 1
B11110, B11110, B11110, B11111,};
byte housechar4[8]={B11111, B11111, B11111, B10001, //Row 1, Col 1
B10001, B10001, B11111, B11111,};
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// INPUT:
int TDButPin = 13; // Temp Up Button connected to pin 13
int TUButPin = 14; // Temp Down Button connected to pin 13
// OUTPUT:
int redLEDPin = 9; // Red LED, connected to digital pin 9
int bluLEDPin = 10; // Blue LED, connected to digital pin 10
int redBUTPin = 11; // Red Button, connected to digital pin 11
int bluBUTPin = 12; // Blue Button, connected to digital pin 12
// Program Variables
int temp = 72;
int redVal = 0;
int bluVal = 0;
int redOldValue = LOW;
int bluOldValue = LOW;
int tuVal = 0;
int tdVal = 0;
int tuOldValue = LOW;
int tdOldValue = LOW;
void setup() {
// initialize lcd screen
lcd.init();
lcd.backlight();
// create and add thermostat icon
lcd.createChar(1,housechar1);
lcd.createChar(2,housechar2);
lcd.createChar(3,housechar3);
lcd.createChar(4,housechar4);
lcd.setCursor(0,0);
lcd.write(1);
lcd.setCursor(0,1);
lcd.write(2);
lcd.setCursor(1,0);
lcd.write(3);
lcd.setCursor(1,1);
lcd.write(4);
// temp and mode
lcd.setCursor(4, 0);
lcd.print("Mode: Off");
lcd.setCursor(4, 1);
lcd.print("Temp: ");
lcd.print((char)223);
lcd.print("F");
// initialize LEDs
pinMode(redLEDPin, OUTPUT);
pinMode(bluLEDPin, OUTPUT);
// initialize the buttons
pinMode(redBUTPin, INPUT);
pinMode(bluBUTPin, INPUT);
pinMode(TDButPin, INPUT);
pinMode(TUButPin, INPUT);
}
void loop() {
// display the temp on the LCD
lcd.setCursor(10, 1);
lcd.print(" ");
lcd.setCursor(10, 1);
lcd.print(temp);
// read the value of buttons
int redNewValue = digitalRead(redBUTPin);
int bluNewValue = digitalRead(bluBUTPin);
int tuNewValue = digitalRead(TUButPin);
int tdNewValue = digitalRead(TDButPin);
// check if the red button was pressed
if (redNewValue == HIGH && redOldValue == LOW) {
Serial.println("Red button pressed.");
if (redVal == HIGH) { // if red LED is ON, turn it OFF
analogWrite(redLEDPin, LOW);
redVal = LOW;
// display updated mode
lcd.setCursor(10, 0);
lcd.print(" ");
lcd.setCursor(10, 0);
lcd.print("Off");
} else { // if red LED is OFF, turn it ON and turn blue LED off
analogWrite(redLEDPin, HIGH);
analogWrite(bluLEDPin, LOW);
redVal = HIGH;
bluVal = LOW;
// display updated mode
lcd.setCursor(10, 0);
lcd.print(" ");
lcd.setCursor(10, 0);
lcd.print("Heat");
}
}
// check if the blue button was pressed
if (bluNewValue == HIGH && bluOldValue == LOW) {
Serial.println("Blue button pressed.");
if (bluVal == HIGH) { // if blue LED is ON, turn it OFF
analogWrite(bluLEDPin, LOW);
bluVal = LOW;
// display updated mode
lcd.setCursor(10, 0);
lcd.print(" ");
lcd.setCursor(10, 0);
lcd.print("Off");
} else { // if blue LED is OFF, turn it ON and turn red LED off
analogWrite(bluLEDPin, HIGH);
analogWrite(redLEDPin, LOW);
bluVal = HIGH;
redVal = LOW;
// display updated mode
lcd.setCursor(10, 0);
lcd.print(" ");
lcd.setCursor(10, 0);
lcd.print("Cool");
}
}
// check if the temp up button was pressed
if (tuNewValue == HIGH && tuOldValue == LOW) {
Serial.println("Temp Up button pressed.");
++temp;
}
// check if the temp down button was pressed
if (tdNewValue == HIGH && tdOldValue == LOW) {
Serial.println("Temp Down button pressed.");
--temp;
}
// update button states
redOldValue = redNewValue;
bluOldValue = bluNewValue;
tuOldValue = tuNewValue;
tdOldValue = tdNewValue;
// slow down for debouncing
delay(100);
}