Hysteresis for 3 Control Functions Operating Same Relay

Hi Again... here is the next installment of my garden controller project. The newest problem is getting the fan_relay to behave as expected when the code for all 3 functions is included in the sketch and turned on. The code for each function works properly isolated from the other two but having any two functions working at the same time and the relay hysteresis will not work as expected. I do see why but don't know how to resolve it.

The three functions are Fan Cycle Timer, Humidity Controlled Fan, and Temperature Controlled Fan.
the cycle timer is user defined to set the cycle every x hours to run the fan for x minutes. The humidity and temperature have user defined set points that go on at set point and off at -3% or -2C respectively... again, it's clear to see why they won't work together but I just can't wrap my head around how to make them work together.

Some code is turned off to show some of my failed attempts and for other obvious reasons.

this is working code with the exception of this latest bug and, oh... is huge so I've attached it.

Any help will be greatly appreciated - thank you in advance!

Cycle_Timer_nano_V3.ino (25.8 KB)

What appears to be missing from your problem description, and I'm guessing from your code too, is the priority that is to be assigned to the inputs. The fan should be on when the time says it should be on. It should be off when the time says it should be off.

It should also be on when the temperature sensor says "Man, it's hot in here". It should not be on if it's cold.

It should be on when the humidity sensor says "Man, I'm sweating like a pig". It should be off when the humidity sensor is happy.

Now, obviously, on any given pass through loop(), the fan may need to be on. Or, it may need to be off.

Should it be on if it is time to be on, if it is hot, or if it is humid? Should it be off only if it is time to be off, it is not too hot, and it is not too humid?

You need to be able to clearly state the conditions that should cause the fan to be on, and to be off. Implementing that in code is then trivial.