// https://wokwi.com/projects/353163986319942657
// https://forum.arduino.cc/t/logic-to-trigger-relay-based-on-sensor-counts-over-a-unit-of-time/1073844
# define windPin 6
# define pumpLED 7 // pump running lamp
# define LOCKOUT 15000 // pump disabled for 15 seconds
unsigned long pumpLockoutTimer;
void setup() {
Serial.begin(115200);
Serial.println("wind jammer demo\n");
pinMode(windPin, INPUT_PULLUP);
pinMode(pumpLED, OUTPUT);
turnOffPump();
turnOnPump();
}
unsigned long now; // time for all non-blocked functions
void loop()
{
now = millis();
windSpeedPumpControl(); // calculate wind speed and turn on/off/lockout fan
report();
}
void windSpeedPumpControl()
{
static unsigned long lastPrint;
static unsigned long counter;
if (windSensorCheck()) {
turnOffPump();
pumpLockoutTimer = now;
}
if (now - pumpLockoutTimer > LOCKOUT) {
turnOnPump();
}
}
float theWindSpeed;
# define RESET 2000 // may not need to, but if no activity, throw out old data.
# define REPORT 100 // throttle flagging the high wind condition
# define WINDY 7.0 // worked for button pressing wildly
// maintains windSpeed with a low pass filter on debounced sensor clicks
bool windSensorCheck()
{
static byte lastReading;
static unsigned long lastTime;
static unsigned long lastEventTime;
static float windPeriod;
// debounce
if (now - lastTime < 10) { // limits usefulness to 100 Hz
return theWindSpeed > WINDY;
}
if (now - lastTime > RESET) {
windPeriod = 999.9;
theWindSpeed = 0.0;
lastTime = now;
}
byte windReading = !digitalRead(windPin); // LOW = wind
if (lastReading != windReading) {
if (windReading) {
windPeriod = 0.9 * windPeriod + 0.1 * (now - lastTime);
theWindSpeed = 1000.0 / windPeriod;
}
lastReading = windReading;
lastTime = now;
}
return theWindSpeed > WINDY;
}
bool pumpIsOn; // just so the pump is only switched if it is in the wrong state
void turnOnPump()
{
if (!pumpIsOn) {
pumpIsOn = true;
Serial.println("turn pump ON");
digitalWrite(pumpLED, HIGH); // and do whatever else needs be done
}
}
void turnOffPump()
{
if (pumpIsOn) {
pumpIsOn = false;
Serial.println("turn pump OFF");
digitalWrite(pumpLED, LOW); // and do whatever else needs be done
}
}
// reporting only - adjust for taste
void report()
{
static unsigned long lastPrint;
static unsigned long counter;
if (now - lastPrint > 777) {
Serial.print(counter); counter++;
Serial.print(" ");
Serial.println(theWindSpeed);
lastPrint = now;
}
}