// https://wokwi.com/projects/353115104055753729
// 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 THRESHOLD 5 // five wind events
# define WINDOW 2000 // within 2 seconds
# define LOCKOUT 15000 // pump disabled for 15 seconds
unsigned long pumpLockoutTimer;
bool pumpRunning;
bool windFlag;
void setup() {
Serial.begin(115200);
Serial.println("wind jammer demo\n");
pinMode(windPin, INPUT_PULLUP);
pinMode(pumpLED, OUTPUT);
turnOnPump();
}
unsigned long now; // time for all non-blocked functioning
void loop()
{
windJammer();
}
void windJammer()
{
now = millis();
if (tooMuchWindQ()) {
Serial.println(" enough!");
turnOffPump();
pumpLockoutTimer = now;
}
if (now - pumpLockoutTimer > LOCKOUT) {
turnOnPump();
}
}
bool pumpIsOn;
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
}
}
bool tooMuchWindQ()
{
static unsigned long timer;
static unsigned int counter;
if (now - timer > WINDOW) { // all quiet, reset counter
if (counter)
Serial.println(" wind reset");
counter = 0;
timer = now;
}
windSensorCheck();
if (windFlag) { // handle the windFlag
counter++;
Serial.println(" wind up!");
timer = now;
windFlag = false;
}
if (counter >= THRESHOLD) {
counter = 0;
return true;
}
else return false;
}
// here just a debounced button - replace with something that catches the wind
void windSensorCheck()
{
static byte lastReading;
static unsigned long lastTime;
if (now - lastTime < 20)
return;
byte windReading = !digitalRead(windPin); // LOW = wind
if (windReading != lastReading) {
if (windReading) {
windFlag = 1;
Serial.println("see the wind");
}
lastTime = now;
}
lastReading = windReading;
}
// never mind this stuff
/*
void loop1() {
now = millis();
tooMuchWindQ();
}
void loop0() {
now = millis();
windSensorCheck();
if (windFlag) {
static int lineCount;
Serial.print(lineCount); // just to differentiate output lines
lineCount++;
Serial.println(" WIND!");
windFlag = false;
}
}
void windSensorCheck0()
{
static byte lastReading;
static unsigned long lastTime;
if (now - lastTime < 20)
return;
byte windReading = !digitalRead(windPin); // LOW = wind
if (windReading != lastReading) {
if (windReading)
windFlag = 1;
lastTime = now;
}
lastReading = windReading;
}
*/