#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// ---- SENSOR SETTINGS ----
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// ---- LCD SETTINGS ----
LiquidCrystal_I2C lcd(0x27, 20, 4);
// ---- OUTPUT PINS ----
#define FAN_PIN 3
#define HEATER_PIN 4
#define HUMIDIFIER_PIN 5
// ---- CONTROL THRESHOLDS ----
#define TEMP_HIGH 27
#define TEMP_LOW 25
#define HUMIDITY_LOW 65
#define HUMIDITY_HIGH 75
#define HUMIDITY_FAN 80
// ---- STATE VARIABLES ----
bool fanState = false;
bool heaterState = false;
bool humidifierState = false;
// ---- For scrolling ----
char row1Message[] = " MANTIS CLIMATE CONTROL ";
char row4Message[64]; // big enough for status line
int scrollIndex1 = 0;
int scrollIndex4 = 0;
unsigned long lastScroll1 = 0;
unsigned long lastScroll4 = 0;
const int scrollDelay = 400;
unsigned long lastSensorRead = 0;
const int sensorInterval = 5000;
void setup() {
Serial.begin(9600);
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Climate Control");
lcd.setCursor(0,2);
lcd.print("Mantis Care");
delay(3000);
lcd.clear();
pinMode(FAN_PIN, OUTPUT);
pinMode(HEATER_PIN, OUTPUT);
pinMode(HUMIDIFIER_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW);
digitalWrite(HEATER_PIN, LOW);
digitalWrite(HUMIDIFIER_PIN, LOW);
}
void loop() {
unsigned long now = millis();
// ---- Read DHT sensor every 2s ----
static float humidity = NAN, temperature = NAN;
if (now - lastSensorRead >= sensorInterval) {
lastSensorRead = now;
humidity = dht.readHumidity();
temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
lcd.setCursor(0,1);
lcd.print("Sensor Error! ");
return;
}
// ---- Control outputs ----
fanState = (temperature > TEMP_HIGH || humidity > HUMIDITY_FAN);
heaterState = (temperature < TEMP_LOW);
humidifierState = (humidity < HUMIDITY_LOW);
if (humidity > HUMIDITY_HIGH) humidifierState = false;
digitalWrite(FAN_PIN, fanState);
digitalWrite(HEATER_PIN, heaterState);
digitalWrite(HUMIDIFIER_PIN, humidifierState);
// ---- Serial Debug ----
Serial.print("Temp: "); Serial.print(temperature);
Serial.print(" C Humidity: "); Serial.print(humidity);
Serial.print("% | Fan:"); Serial.print(fanState ? "ON " : "OFF");
Serial.print(" Heater:"); Serial.print(heaterState ? "ON " : "OFF");
Serial.print(" Humidifier:"); Serial.println(humidifierState ? "ON" : "OFF");
// ---- Build Row4 message safely ----
snprintf(row4Message, sizeof(row4Message),
" Fan:%s | Heat:%s | Humidifier:%s ",
fanState ? "ON" : "OFF",
heaterState ? "ON" : "OFF",
humidifierState ? "ON" : "OFF");
// ---- Update static rows ----
lcd.setCursor(0,1);
lcd.print("Temp: ");
lcd.print(temperature,1);
lcd.print((char)223);
lcd.print("C ");
lcd.setCursor(0,2);
lcd.print("Humidity: ");
lcd.print(humidity,0);
lcd.print("% ");
}
// ---- Row 1 scrolling banner ----
if (now - lastScroll1 >= scrollDelay) {
lastScroll1 = now;
int len1 = strlen(row1Message);
lcd.setCursor(0,0);
for (int i=0; i<20; i++) {
char c = row1Message[(scrollIndex1 + i) % len1];
lcd.print(c);
}
scrollIndex1 = (scrollIndex1 + 1) % len1;
}
// ---- Row 4 scrolling status ----
if (now - lastScroll4 >= scrollDelay) {
lastScroll4 = now;
int len4 = strlen(row4Message);
lcd.setCursor(0,3);
for (int i=0; i<20; i++) {
char c = row4Message[(scrollIndex4 + i) % len4];
lcd.print(c);
}
scrollIndex4 = (scrollIndex4 + 1) % len4;
}
}