// https://forum.arduino.cc/t/pwm-fan-control-nano/1063695
// https://wokwi.com/projects/350670924205261395
# define signal A0 // input: reading the pot
# define output 7 // output: controlling the fan, motor, whatever
# define UPPER 777 // too high, turn on
# define LOWER 222 // too low, turn off
// raw indication
# define hotLED A1
# define coldLED A2
bool running;
void setup() {
Serial.begin(115200);
Serial.print("hysteresis demo\n\n");
pinMode(coldLED, OUTPUT);
pinMode(hotLED, OUTPUT);
}
unsigned long loopCounter;
void loop() {
int currentReading = analogRead(signal);
if (currentReading > UPPER) {
if (!running) {
Serial.print(loopCounter);
Serial.println(" too <hot> turn on <fan>\n");
}
running = true; // may have already been
digitalWrite(output, HIGH);
}
if (currentReading < LOWER) {
if (running) {
Serial.print(loopCounter);
Serial.println(" <cold> enough turn off <fan>\n");
}
running = false; // may have already been
digitalWrite(output, LOW);
}
// full speed loop available here.
loopCounter++;
digitalWrite(hotLED, currentReading > UPPER ? HIGH : LOW);
digitalWrite(coldLED, currentReading < LOWER ? HIGH : LOW);
}