#include #include LiquidCrystal_I2C lcd(0x27, 16, 2); constexpr unsigned int SENSOR_PIN {7U}; AM2302::AM2302_Sensor am2302{SENSOR_PIN}; const int potPin = A1; const int buttonPin = 8; // ---------------- STATE MACHINE ---------------- enum State { SET_MAX_TEMP, SET_HUMIDITY, CONFIRM, VIEW }; State state = SET_MAX_TEMP; // ---------------- SENSOR VALUES ---------------- float temperature = NAN; float humidity = NAN; // ---------------- SETTINGS ---------------- float maxTemperature = 30.0; float targetHumidity = 50.0; // ---------------- CONFIRM ---------------- bool confirmYes = false; // ---------------- TIMING ---------------- unsigned long lastLcdUpdate = 0; unsigned long lastSensorRead = 0; const unsigned long lcdInterval = 500; const unsigned long sensorInterval = 2000; // ---------------- BUTTON ---------------- bool lastButtonState = HIGH; // ---------------- SENSOR ---------------- void readSensor(unsigned long now) { if (now - lastSensorRead >= sensorInterval) { lastSensorRead = now; if (am2302.read() == AM2302::AM2302_READ_OK) { humidity = am2302.get_Humidity(); temperature = am2302.get_Temperature(); } } } // ---------------- BUTTON HANDLER ---------------- void handleButton() { static bool lastReading = HIGH; static unsigned long lastDebounce = 0; const unsigned long debounceDelay = 50; bool reading = digitalRead(buttonPin); if (lastReading == HIGH && reading == LOW) { // CONFIRM LOGIC if (state == CONFIRM) { if (confirmYes) { state = VIEW; // accept settings } else { state = SET_MAX_TEMP; // restart setup } } else { // NORMAL FLOW if (state == SET_MAX_TEMP) { state = SET_HUMIDITY; } else if (state == SET_HUMIDITY) { state = CONFIRM; } else if (state == VIEW) { state = SET_MAX_TEMP; } } } lastReading = reading; } // ---------------- LCD ---------------- void updateLCD(unsigned long now) { if (now - lastLcdUpdate < lcdInterval) return; lastLcdUpdate = now; lcd.clear(); int potValue = analogRead(potPin); // ---------------- SET MAX TEMP ---------------- if (state == SET_MAX_TEMP) { maxTemperature = map(potValue, 0, 1023, 20, 60); lcd.setCursor(0, 0); lcd.print("Set Max Temp:"); lcd.setCursor(0, 1); lcd.print(maxTemperature); lcd.print(" C"); return; } // ---------------- SET HUMIDITY ---------------- if (state == SET_HUMIDITY) { targetHumidity = map(potValue, 0, 1023, 0, 80); lcd.setCursor(0, 0); lcd.print("Set Humidity:"); lcd.setCursor(0, 1); lcd.print(targetHumidity); lcd.print("%"); return; } // ---------------- CONFIRM ---------------- if (state == CONFIRM) { confirmYes = (potValue < 512); lcd.setCursor(0, 0); lcd.print("Confirm Settings"); lcd.setCursor(0, 1); if (confirmYes) { lcd.print("> YES NO"); } else { lcd.print(" YES > NO"); } return; } // ---------------- VIEW MODE ---------------- lcd.setCursor(0, 0); lcd.print("AH:"); lcd.print(humidity); lcd.print("%"); lcd.setCursor(0, 1); lcd.print("AT:"); lcd.print(temperature); lcd.print("C"); // show target humidity // show target lcd.setCursor(10, 1); lcd.print("TH:"); lcd.print(targetHumidity); // show target lcd.setCursor(10, 0); lcd.print("TT:"); lcd.print(maxTemperature); } // ---------------- SETUP ---------------- void setup() { lcd.init(); lcd.backlight(); am2302.begin(); pinMode(buttonPin, INPUT_PULLUP); lcd.setCursor(0, 0); lcd.print("System Start"); } // ---------------- LOOP ---------------- void loop() { unsigned long now = millis(); handleButton(); readSensor(now); updateLCD(now); }