#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <IRremote.h> // Only works with 2.0 Version
const int redPin = 5; // Red pin of the RGB LED
const int greenPin = 10; // Green pin of the RGB LED
const int bluePin = 9; // Blue pin of the RGB LED
#define DHTPIN 13 // Pin for the Temp/ humidity sensor
#define DHTTYPE DHT22 // Define the type of sensor (simulations only have 22 IRL 11)
DHT dht(DHTPIN, DHTTYPE);
const int MODE_PIN = 4; // Pin for switch
const int buttonPin = 6; // Pin for the push-button
const int irReceiverPin = 3; // Pin for the IR receiver
IRrecv irReceiver(irReceiverPin);
decode_results irResults;
bool systemOn = false; // Variable to track whether the system is on or off
bool isFahrenheit = true; // Variable to track temperature scale
#define DEBUG_MODE
// Define the IR codes for the buttons
#define POWER_BUTTON_CODE 0xFFA25D // POWER
#define MINUS_BUTTON_CODE 0xFFA857 // C ("-")
#define PLUS_BUTTON_CODE 0xFF906F // F ("+")
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Function to set the RGB LED color based on the temperature
void setLedColor(float temperature) {
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
if (isFahrenheit) {
temperature = (temperature - 32) * 5.0 / 9.0;
}
if (temperature < 10) {
// Blue gradient for temperatures under 50°F (10°C)
blueValue = map(temperature, -20, 10, 255, 0);
} else {
// Red gradient for temperatures above 50°F (10°C)
redValue = map(temperature, 10, 50, 0, 255);
}
analogWrite(redPin, redValue); // Write out to red LED
analogWrite(greenPin, greenValue); // Write out to Green LED
analogWrite(bluePin, blueValue); // Write out to blue LED
}
bool checkButton() {
static bool oldBtnState = true; // INPUT_PULLUP idles high
bool isButtonPressed = false;
int btnState = digitalRead(buttonPin);
if (btnState != oldBtnState) {
oldBtnState = btnState;
if (btnState == LOW) {
isButtonPressed = true;
Serial.println("Button pressed");
} else {
Serial.println("Button released");
}
delay(20); // for debounce
}
return isButtonPressed;
}
// Function to switch LCD from F TO C (or vice versa)
void handleTemperatureScaleChange() {
isFahrenheit = !isFahrenheit;
lcd.setCursor(0, 1);
lcd.print(isFahrenheit ? "Temperature: F " : "Temperature: C ");
delay(500);
}
// Function to update the LED
void updateLedColor() {
float temperature;
if (isFahrenheit) {
// Read temperature in Fahrenheit directly
temperature = dht.readTemperature(true);
} else {
// Read temperature in Celsius directly
temperature = dht.readTemperature();
}
// Update the LED color based on the temperature
setLedColor(temperature);
}
void setup() {
lcd.init();
lcd.backlight();
lcd.print("System OFF");
dht.begin();
irReceiver.enableIRIn(); // Start the IR receiver
#ifdef DEBUG_MODE
Serial.begin(9600); // Initialize Serial communication
#endif
pinMode(MODE_PIN, INPUT_PULLUP);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
float temperature;
char tempString[17];
char humidityString[17];
char scale;
// Check if the button is pressed
if (checkButton()) {
systemOn = !systemOn; // Toggle the system on/off
// Display the system state on the LCD without clearing
lcd.setCursor(0, 0);
lcd.print(systemOn ? "System ON " : "System OFF ");
lcd.setCursor(0, 1);
lcd.print(systemOn ? " " : " ");
if (!systemOn) {
#ifdef DEBUG_MODE
Serial.println("-----------------");
#endif
}
delay(500); // Debouncing
}
// Check for IR remote input
if (irReceiver.decode(&irResults)) {
// Check if the received code matches the power button code
if (irResults.value == POWER_BUTTON_CODE) {
// If the power button is pressed, toggle the system state
systemOn = !systemOn;
lcd.setCursor(0, 0);
lcd.print(systemOn ? "System ON " : "System OFF ");
lcd.setCursor(0, 1);
lcd.print(systemOn ? " " : " ");
if (!systemOn) {
#ifdef DEBUG_MODE
Serial.println("-----------------");
#endif
}
delay(500); // Debouncing
// If the + or - is pressed, change the temperature scale
} else if (irResults.value == MINUS_BUTTON_CODE || irResults.value == PLUS_BUTTON_CODE) {
handleTemperatureScaleChange();
// Update based on new scale
updateLedColor();
}
irReceiver.resume(); // Resume IR receiver for the next signal
}
// Read temperature based on the selected mode (switch)
if (digitalRead(MODE_PIN)) {
temperature = dht.readTemperature(isFahrenheit);
scale = isFahrenheit ? 'F' : 'C';
} else {
temperature = dht.readTemperature();
scale = 'C';
}
// If the system is on, update the display and LED color
if (systemOn) {
delay(2000);
float humidity = dht.readHumidity(); // Read humidity
#ifdef DEBUG_MODE // Print temperature and humidity in the debug mode
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °");
Serial.print(scale);
Serial.print(", Humidity: ");
Serial.print(humidity);
Serial.println(" %");
#endif
// Formating temperature and humidity strings for LCD display
sprintf(tempString, "Temp: %8d%c%c", (int)(temperature + 0.5), char(223), scale);
lcd.setCursor(0, 0); // LINE 1
lcd.print(tempString);
sprintf(humidityString, "Humidity: %5d%%", (int)(humidity + 0.5));
lcd.setCursor(0, 1); // LINE 2
lcd.print(humidityString);
updateLedColor();
} else {
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
}