#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <IRremote.h>
const int redPin = 11; // 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
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const int MODE_PIN = 4;
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 // Comment out this line to disable serial output
// Define the IR codes for the buttons
#define POWER_BUTTON_CODE 0xFFA25D
#define MINUS_BUTTON_CODE 0xFFA857
#define PLUS_BUTTON_CODE 0xFF906F
// 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 (temperature < 50) {
// Blue gradient for temperatures under 50°F
blueValue = map(temperature, -20, 50, 255, 0);
} else {
// Red gradient for temperatures 50°F and above
redValue = map(temperature, 50, 100, 0, 255);
}
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
// Create an LCD object with I2C address 0x27 (use your actual I2C address if different)
LiquidCrystal_I2C lcd(0x27, 16, 2);
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;
}
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 handleTemperatureScaleChange() {
isFahrenheit = !isFahrenheit;
lcd.setCursor(0, 1);
lcd.print(isFahrenheit ? "Temperature: F " : "Temperature: C ");
delay(500); // Add a small delay for debouncing
}
void loop() {
float temperature;
char tempString[17];
char humidityString[17];
char scale;
// Check if the button is pressed
if (checkButton()) {
// Toggle the system on/off
systemOn = !systemOn;
// 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 ? " " : " ");
// Display dash lines if turning off after being on
if (!systemOn) {
#ifdef DEBUG_MODE
Serial.println("-----------------");
#endif
}
delay(500); // Add a small delay for 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;
// 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 ? " " : " ");
// Display dash lines if turning off after being on
if (!systemOn) {
#ifdef DEBUG_MODE
Serial.println("-----------------");
#endif
}
delay(500); // Add a small delay for debouncing
} else if (irResults.value == MINUS_BUTTON_CODE) {
// If the minus button is pressed, change the temperature scale to Celsius
handleTemperatureScaleChange();
} else if (irResults.value == PLUS_BUTTON_CODE) {
// If the plus button is pressed, change the temperature scale to Fahrenheit
handleTemperatureScaleChange();
}
irReceiver.resume(); // Receive the next value
}
if (digitalRead(MODE_PIN)) {
temperature = dht.readTemperature(isFahrenheit); // Convert to Fahrenheit if the scale is set to Fahrenheit
scale = isFahrenheit ? 'F' : 'C';
} else {
temperature = dht.readTemperature();
scale = 'C';
}
// If the system is on, proceed with temperature and humidity sensing
if (systemOn) {
delay(2000);
float humidity = dht.readHumidity(); // Read humidity
#ifdef 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
// Use sprintf to format the temperature and humidity values
sprintf(tempString, "Temp: %8d%c%c", (int)(temperature + 0.5), char(223), scale);
lcd.setCursor(0, 0);
lcd.print(tempString);
sprintf(humidityString, "Humidity: %5d%%", (int)(humidity + 0.5));
lcd.setCursor(0, 1);
lcd.print(humidityString);
// Set LED color based on temperature
setLedColor(temperature);
} else {
// If the system is off, turn off the LED
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
}