Hi. I’m trying to do a basic thing from Arduino that I’m getting some complications here.
As you can see in the picture, I have a tank with a reed switch on a floater to know when the water is on that level so it will activate a water pump x seconds.
There are multiple problems with this logic.
1- If the reed switch is pressed, and stay pressed, the action is not done until I “unpress” it.
2- If the water still above the switch limit and the time (50s) is over, it will not do the action again.
I was trying the while, if, wait until, etc, without success
Thank you for any help.
I’m going to assume that you simply have typos when writing this out here. There seems to be an errant ! after gpio and a set of parentheses where there shouldn’t be.
What is the functionality you want to happen? Turn on when pin high and turn off when pin low? It is unclear what your ideal operation is.
It could be as simple as this if you want it to be on when high and off when low.
After reading this again, I think you are trying to have the pump run for 50s, then check if the float is still physically high then run again if it is and repeat.
I would use a script that first turns the pump on then has a while loop that runs until the binary sensor is off. Inside the while loop, it pauses for 50s, or whatever you want. After the while loop finishes, it turns the pump off. Then you execute the script in your on_press, but first check if the script is already running first. You don’t want the script to turn on again if it is already running.
Other than the misaligned delay after the then this looks like it should work. But beware that any sensor bouncing around low and high will call this sequence to restart. This is why I recommended putting this all in a script and check if it is already running before calling it.
I was looking for scripts examples but there are not much thing
I was able to do it (not 100%) but it works
The only problem is:
If the thank is already full and the ESP is powered ON, the signal is inverted…
Later I will try something on ON_BOOT that I’ve seen some where.
Ok, I made a script but its not working 100%
On PRESS it will turn on the switch and delay the 10 seconds.
The problems:
1- if it is always on, when release the button, the 2nd delay will be the remaining time of the delay and not the 10s
2- If Powered ON with the switch pressed, it will not activate de relay until I release and press again.
For point 1) you can add another delay after the while and before the turn off. An even more elegant approach is to only turn on the relay for on_press and then only turn it off after 10s on_release. No while loop needed. Many methods here, but it comes down to your preference and what works best.
For point 2, use an on_boot automation to turn on the relay if the pin is on
It will not work 100% for the same reason/problem I have since the beginning.
On Arduino when I make a delay, the code stops until the time is over. Then it will execute the next code.
Here the remaining code stills running and it will complicate this.
For example, if I keep pressing the switch (reed switch) for more than 10s and than release it, depending of the moment I release it, the pump will turn off after 1 to 10s. Not OK.
What I want is to release it and the pump keeps running more 10s.
Thins code is for a toilet sink.
This is what can happen:
The electricity can fail, someone can flush the water and the tank is full. When the electricity gets back, the pump should start and just stop 10s after the level sensor (reed switch) are reached.
When use it normally, when the the level sensor are reached )on) the pump starts. If the water come faster then the pump can push, when the water stops and the level sensor are reached (off), it have to keep running for 10s.
MatthewFlamm, thank you very much for the patience to teach me and trying to show me the way.
If you need control of whether code snippets are running use scripts like I suggested. Although I still think that one option is equivalent to the Arduino code you posted. It may turn off only 1s after going low.
If you want it to always run for 10s after going low, don’t use a while loop and just turn on the pump on_press then delay 10s before turning pump off in on_release. Why doesn’t this work?
The problem with this code is:
If I press the switch the pump starts.
Then, I release the switch and the time starts.
If I press again the switch, for example, at second 6 and release it again, the time will not start again and will switch off the pump in the next 4s.
If so, you need to use script with the delay and switch turn off. Execute the script if the pin goes low, but first check if it is already running. Maybe you want to cancel any running scripts and restart?
Then turn the script off when the pump turns on. This way the script is running fresh when it goes low.