Complete rewrite of Application

This commit is contained in:
2026-05-04 22:30:50 +02:00
parent 507a874304
commit 1f2a0b253e
9 changed files with 360 additions and 180 deletions

View File

@@ -1,192 +1,58 @@
#include <AM2302-Sensor.h>
#include <LiquidCrystal_I2C.h>
#include "DisplayManager.h"
#include "SensorManager.h"
#include "Button.h"
#include "Menu.h"
LiquidCrystal_I2C lcd(0x27, 16, 2);
DisplayManager display;
SensorManager sensor(7);
constexpr unsigned int SENSOR_PIN {7U};
AM2302::AM2302_Sensor am2302{SENSOR_PIN};
Button downButton(8);
Button upButton(9);
Button okButton(10);
Button backButton(11);
const int potPin = A1;
const int buttonPin = 8;
Menu menu;
// ---------------- 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();
display.begin();
sensor.begin();
am2302.begin();
pinMode(buttonPin, INPUT_PULLUP);
lcd.setCursor(0, 0);
lcd.print("System Start");
upButton.begin();
downButton.begin();
okButton.begin();
backButton.begin();
}
// ---------------- LOOP ----------------
void loop() {
unsigned long now = millis();
handleButton();
readSensor(now);
updateLCD(now);
upButton.update(now);
downButton.update(now);
okButton.update(now);
backButton.update(now);
sensor.update(now);
if (upButton.pressed()) {
menu.prev();
}
if (downButton.pressed()) {
menu.next();
}
if (okButton.pressed()) {
menu.enter();
}
if(backButton.pressed()) {
menu.back();
}
if (display.ready(now)) {
display.showText(
String("> ") + menu.getCurrent(),
String(" ") + menu.getNext()
);
}
}