// https://wokwi.com/projects/354478377898627073
// https://forum.arduino.cc/t/humidity-temp-ventilation-control-for-frog-paludarium-2nd-3rd-or-even-4th-pair-of-eyes-on-whether-im-on-the-right-track/1078600
# define buttonPin 6
# define fanLED 7 // fan running lamp/relay
unsigned long fanTimer;
void setup() {
Serial.begin(115200);
Serial.println("paludarium thing\n");
pinMode(buttonPin, INPUT_PULLUP);
pinMode(fanLED, OUTPUT);
turnOnFan();
delay(777);
turnOffFan();
}
unsigned long now; // time for all non-blocked functions
unsigned long counter;
void loop()
{
counter++; // just count the loops as they go by
now = millis();
setFan(); // turn on for random time maybe
handleFan(); // turn off fan maybe
report(); // whassup?
}
int fanOnTime;
bool fanIsOn;
void setFan()
{
if (!digitalRead(buttonPin)) {
fanOnTime = random(3000, 7000);
Serial.print(" fans starting for ");
Serial.println(fanOnTime);
turnOnFan();
fanTimer = now;
// crude button denouncing:
do {
delay(20);
} while (!digitalRead(buttonPin));
delay(20);
}
}
void handleFan()
{
if (now - fanTimer > fanOnTime) {
turnOffFan();
}
}
void turnOnFan()
{
if (!fanIsOn) {
fanIsOn = true;
Serial.println(" turn fan ON");
digitalWrite(fanLED, HIGH); // and do whatever else needs be done
}
}
void turnOffFan()
{
if (fanIsOn) {
fanIsOn = false;
Serial.println(" turn fan OFF");
digitalWrite(fanLED, LOW); // and do whatever else needs be done
}
}
// reporting only - adjust for taste
void report()
{
static unsigned long lastPrint;
if (now - lastPrint > 1777) {
Serial.print(counter); counter++;
Serial.print(" the fan is ");
Serial.println(fanIsOn ? "running..." : "not running.");
lastPrint = now;
}
}