Looking to code in hysteresis with a time delay


I have a running program reading two pressures and two actuated ball valves with a pump and an air compressor. I’m looking to add hysteresis in terms of alarming. One of the pressure transmitters is a 0-5v 1000psi sensor, so I have quite a bit of fluctuation in pressure readings. I want to add some protection for the pump so that if for some reason the suction pressure of the pump goes low, it will shut the pump off. My issue is that I don’t want it to shut the pump off when I first start it and increase the pressure. I have written code with blynk timers to only read pressure every 10 seconds, as well as smoothing the readings. My issue is that I want it to shut off the pump if the pressure is high for 30 consecutive seconds. I don’t want it to trip the pump off if there is a momentary pressure spike, but rather a prolonged pressure increase.

I’ve searched the forum and all I could find were examples of hysteresis that used blynk timers to look every x amount of seconds but not look consistently.

Any chance someone could point me in the right direction?

I think I’d take a different approach.
For the over-pressure warning, let’s say you sample the pressure every 1 second (to keep the explanation simple). I’d use an variable, and increment it by 1 every time I take a reading and the pressure is over my upper threshold.
I’d also reset the variable to zero if my pressure is below the upper threshold.
After taking a pressure reading I’d check if this variable is >= 30
If it is, then I’d know that the pressure has been over my upper threshold value for 30 consecutive readings, and I should shut the pump down.

As far as the start-up process is concerned, I’d probably look at whether the pressure is below the lower threshold, but greater than it was last time I took a reading. If it is then I know the pressure is climbing, and therefore okay.
You could probably build more logic into this, by also counting the amount of time the pressure is below the lower limit, in the same way as described earlier.



Thank you for the response, very helpful.

How does this look?

As a side note, whenever my program loses power or connection, the arduino uno wifi rev 2 sets the pins high on startup. I have to cycle the valves and relays to get them into a normal state… ive tried swapping high to low and it still does the same thing. Any idea why on this?

Also, at the beginning of my code, I declare other variables, and I had put the variable and =0 after it. Is this necessary or can I just declare the variable and then put a line of code with Blynk.syncVirtual(V9); in order to pull the most recent number from the server?

Thanks again

//declare variable as z at beginning of code

float z = 0

void protection(){
if ((digitalRead(6) == LOW) && (pressureValue1 > 800))//set up so it reads when the pump is on

z= z + 1


if ((digitalRead(6) == LOW) && (pressureValue1 < 800))

if z >= 30
{ digitalWrite(6, HIGH) }// high will shut off the pump

void setup() {

  timer.setInterval(1000L, protection);

It look okay, as far as I can see from a snippet of code.

Impossible to say without knowing what the rest of your sketch looks like, whether your relays are active HIGH or LOW and how your datastreams are configured (if you’re using Blynk IoT).