I've been struggling for days to come up with the logic I need to program the following:
I'm making a wind sensor that will shut off my outdoor water fountain when it gets windy. I have a reed switch that's triggered by a magnet which is embeded in a rotating wind sensor. I plan to program a relay to shut-off the power to the fountain when the reed switch triggers "x" number of times over a time period "p".
It would be a simple program if it was just a matter of counting "x" and when that count gets to a certain number, the relay goes low (pump shuts off) for a set amount of time. The problem is that I need to count "x" over a period of time "p" otherwise I could get a few wind gusts here and there and eventually, perhaps days later, the pump will finally go off. I want it to shut off only during sustained, significant winds, not little gusts over a long period of time.
The pump also needs to remain off for a set amount of time "t" before turning back on. If I just reset the counts "x" to zero (0) at the same time the pump turns back on, and it's still windy, the pump will turn on when "t" is reached but then go right back off a few moments later. What I need to do is continue to measure the wind in the background, even after the pump turns off. If it's still windy, the pump never turns back on until the wind subsides.
As I'm typing this, I suddenly realized one thing I might be able to do is to create another variable that includes the count "x" divided by the time period for counting "p". If this ratio (x/p) exceeds a certain threshold, the pump turns off for time period "t". If the ratio continues to exceed the threshold, the pump never turns on. If the ratio goes below the threshold, I delay (yes, I'll use a millis delay) the pump turning on for awhile longer to make sure the wind is really dying (x/p stays low.)
I thought about deleting this post now that I have a possible solution but I'll leave it up in case someone has a better solution or feels like my ramblings might be of use for some other project.