#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <Adafruit_NeoPixel.h>
#define DHTPIN 5 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define RELAYPIN_HH 6
#define RELAYPIN_HF 7
#define RELAYPIN_VF 8
#define NEOPIXEL_PIN 10
#define NUMPIXELS 3
const int BUTTONPIN_HH = 2;
const int BUTTONPIN_HF = 3;
const int BUTTONPIN_VF = 4;
const int BLSWITCH = 9;
#define TEMP_UPPER_THRESHOLD 65
#define TEMP_LOWER_THRESHOLD 55
bool hhActive = false;
bool hfActive = false;
bool vfActive = false;
bool heat = false;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
Adafruit_NeoPixel pixels(NUMPIXELS, NEOPIXEL_PIN, NEO_RGB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
lcd.init();
// lcd.backlight();
dht.begin();
pixels.begin();
pixels.setBrightness(100);
pixels.setPixelColor(0, pixels.Color(0, 255, 0));
pixels.setPixelColor(1, pixels.Color(0, 255, 0));
pixels.setPixelColor(2, pixels.Color(0, 255, 0));
pixels.show();
}
void loop() {
// delay(1000); // Delay between sensor readings
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println("Failed to read temp from DHT sensor!");
return;
}
float humidity = dht.readHumidity();
if (isnan(humidity)) {
Serial.println("Failed to read RH from DHT sensor!");
return;
}
lcd.setCursor(0, 0);
lcd.print(temperature);
lcd.print("C ");
lcd.print(humidity);
lcd.print("%RH");
if (digitalRead(BUTTONPIN_HH) == LOW) {
hhActive = !hhActive;
delay(200); // Debounce delay
}
if (hhActive) {
// Check temperature range and activate/deactivate relay
if (temperature < TEMP_LOWER_THRESHOLD && hhActive) {
heat = true;
// digitalWrite(RELAYPIN_HH, HIGH); // Activate relay
// lcd.setCursor(0, 1);
// lcd.print("hH-A");
} else if (temperature > TEMP_UPPER_THRESHOLD) {
heat = false;
// digitalWrite(RELAYPIN_HH, LOW); // Deactivate relay
// lcd.setCursor(0, 1);
// lcd.print("hH-I");
}
if (heat) {
digitalWrite(RELAYPIN_HH, HIGH); // Activate relay
// add code here to change heater LED to green
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
pixels.show();
lcd.setCursor(0, 1);
lcd.print("hH-On ");
} else {
digitalWrite(RELAYPIN_HH, LOW); // Deactivate relay
// add code here to change heater LED to green blinking
pixels.setPixelColor(0, pixels.Color(0, 0, 255));
pixels.show();
lcd.setCursor(0, 1);
lcd.print("hH-Off");
}
} else {
lcd.setCursor(0, 1);
lcd.print(" ");
digitalWrite(RELAYPIN_HH, LOW);
pixels.setPixelColor(0, pixels.Color(0, 255, 0));
pixels.show();
}
if (digitalRead(BUTTONPIN_HF) == LOW) {
hfActive = !hfActive;
delay(200); // Debounce delay
if (hfActive) {
digitalWrite(RELAYPIN_HF, HIGH);
// add code here to change heaterFan LED to green
pixels.setPixelColor(1, pixels.Color(255, 0, 0));
pixels.show();
lcd.setCursor(9, 1);
lcd.print("hF");
} else {
digitalWrite(RELAYPIN_HF, LOW);
// add code here to change heaterFan LED to green
pixels.setPixelColor(1, pixels.Color(0, 255, 0));
pixels.show();
lcd.setCursor(9, 1);
lcd.print(" ");
}
}
if (digitalRead(BUTTONPIN_VF) == LOW) {
vfActive = !vfActive;
delay(200); // Debounce delay
if (vfActive) {
digitalWrite(RELAYPIN_VF, HIGH);
// add code here to change ventFan LED to green
pixels.setPixelColor(2, pixels.Color(255, 0, 0));
pixels.show();
lcd.setCursor(14, 1);
lcd.print("vF");
} else {
digitalWrite(RELAYPIN_VF, LOW);
// add code here to change ventFan LED to green
pixels.setPixelColor(2, pixels.Color(0, 255, 0));
pixels.show();
lcd.setCursor(14, 1);
lcd.print(" ");
}
}
if (digitalRead(BLSWITCH) == LOW) {
lcd.backlight();
} else {
lcd.noBacklight();
}
}