Set target humidity and max temperature

This commit is contained in:
2026-05-03 21:26:02 +02:00
parent d3922c85e7
commit 507a874304

View File

@@ -7,26 +7,41 @@ constexpr unsigned int SENSOR_PIN {7U};
AM2302::AM2302_Sensor am2302{SENSOR_PIN}; AM2302::AM2302_Sensor am2302{SENSOR_PIN};
const int potPin = A1; const int potPin = A1;
const int buttonPin = 8;
// Timing
unsigned long lastLcdUpdate = 0;
unsigned long lastSensorRead = 0;
const unsigned long lcdInterval = 1000; // ---------------- STATE MACHINE ----------------
const unsigned long sensorInterval = 2000; enum State {
SET_MAX_TEMP,
SET_HUMIDITY,
CONFIRM,
VIEW
};
// State State state = SET_MAX_TEMP;
int lastMenuIndex = -1;
// ---------------- SENSOR VALUES ----------------
float temperature = NAN; float temperature = NAN;
float humidity = NAN; float humidity = NAN;
const int relayPin = 12; // ---------------- SETTINGS ----------------
float maxTemperature = 30.0;
float targetHumidity = 50.0;
unsigned long lastRelayToggle = 0; // ---------------- CONFIRM ----------------
const unsigned long relayInterval = 2000; bool confirmYes = false;
bool relayState = 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) { void readSensor(unsigned long now) {
if (now - lastSensorRead >= sensorInterval) { if (now - lastSensorRead >= sensorInterval) {
lastSensorRead = now; lastSensorRead = now;
@@ -38,71 +53,140 @@ void readSensor(unsigned long now) {
} }
} }
void updateRelay(unsigned long now) { // ---------------- BUTTON HANDLER ----------------
if (now - lastRelayToggle >= relayInterval) { void handleButton() {
lastRelayToggle = now; static bool lastReading = HIGH;
static unsigned long lastDebounce = 0;
const unsigned long debounceDelay = 50;
relayState = !relayState; // toggle state bool reading = digitalRead(buttonPin);
digitalWrite(relayPin, relayState ? HIGH : LOW);
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) { void updateLCD(unsigned long now) {
int potValue = analogRead(potPin); if (now - lastLcdUpdate < lcdInterval) return;
int menuIndex = (potValue < 512) ? 0 : 1;
if (menuIndex != lastMenuIndex || now - lastLcdUpdate >= lcdInterval) {
lastMenuIndex = menuIndex;
lastLcdUpdate = now; lastLcdUpdate = now;
lcd.clear(); lcd.clear();
if (menuIndex == 0) { int potValue = analogRead(potPin);
// ---------------- SET MAX TEMP ----------------
if (state == SET_MAX_TEMP) {
maxTemperature = map(potValue, 0, 1023, 20, 60);
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print("Humidity:"); lcd.print("Set Max Temp:");
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
if (!isnan(humidity)) { 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(humidity);
lcd.print("%"); lcd.print("%");
} else {
lcd.print("No data");
}
}
else {
lcd.setCursor(0, 0);
lcd.print("Temperature:");
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
if (!isnan(temperature)) { lcd.print("AT:");
lcd.print(temperature); lcd.print(temperature);
lcd.print(" C"); lcd.print("C");
} else {
lcd.print("No data"); // 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() { void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // start OFF
lcd.init(); lcd.init();
lcd.backlight(); lcd.backlight();
am2302.begin(); am2302.begin();
pinMode(buttonPin, INPUT_PULLUP);
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print("Starting..."); lcd.print("System Start");
} }
// ---------------- LOOP ----------------
void loop() { void loop() {
unsigned long now = millis(); unsigned long now = millis();
handleButton();
readSensor(now); readSensor(now);
updateLCD(now); updateLCD(now);
updateRelay(now);
} }