Hi everyone,
I'm working on a project to replace a failing "smart habitat" for my dart frogs. I have connected 3 types of sensors, 2 of each type, and I can read and use the values and averages thereof.
I can set and save values for timers I'm using to control the lights and sprinklers through a relay module. So far so good ![]()
Now I want to have the Arduino Mega 2560 control the climate by comparing the sensor data to user set values. I might move the whole project to a Nano 33 IoT to get easy app-access, but I think the Nano should be able to do this without issues.
While writing down my ideas I noticed some conditions can occur simultaniously, and I am unsure if this will cause problems.
The arduino can increase and decrease temperature with either a heating cable, or powering fans to drive the warm air out. To control the humidity I can use an ultrasonic fogger and the sprinklers to increase or the fans to decrease.
My initial idea is as follows:
void climateControl() {
float AirTempA , HumidityA; // A for average
static unsigned int maxAirTemp, minAirTemp; //set or changed by user
static unsigned int maxHumidity, minHumidity;
if (AirTempA > maxAirTemp) {
//turn on fans
if (AirTempA > maxAirTemp && HumidityA < minHumidity) {
//turn on sprinklers and wait a bit (endothermic evaporation), then turn on fans again. unlikely to happen.
}
if (AirTempA > maxAirTemp && HumidityA > maxHumidity) {
//turn on all the fans maximum power
}
}
if (AirTempA < minAirTemp) {
//turn on heating
if (AirTempA < minAirTemp && HumidityA < minHumidity) {
//turn on heating and ultrasonic fogger
}
if (AirTempA < minAirTemp && HumidityA > maxHumidity) {
//turn on heating & fans? maybe postpone or skip next programmed rain
}
}
if (HumidityA > maxHumidity) {
//turn on fans
if (HumidityA > maxHumidity && AirTempA < minAirTemp) {
//turn on fans and heating
}
if (HumidityA > maxHumidity && AirTempA > maxAirTemp) {
//turn on all fans maximum power
}
}
if (HumidityA < minHumidity) {
//turn on fogger
if (HumidityA < maxHumidity && AirTempA < minAirTemp) {
//turn on ultrasonic fogger and heating
}
if (HumidityA < maxHumidity && AirTempA > maxAirTemp) {
//turn on sprinklers and wait for a bit, then turn fans on again
}
}
}
As you can see there are double events where conditions are the same after an initial reading triggers and action.
I think I could take the doubles out and make them unique events, not preceded by another if() condition. However the initial condition to trigger the event would be the most important, the secondary && triger would likely be caused by the initial event/action.
Also as it stands now multiple different conditions could trigger the same event. I am unsure if this will cause a problem or not. Should I add flags so that if 1 event is active, others won't be able to trigger? I could add a condition to every event if ( != active), so it won't trigger if it's already active.
I'm a little worried something might cascade and cause extremes that are bad for frogs and plants. I do plan on adding alerts if certain values pass certain limits for that.