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