I got a LED indicator to toggle two LEDs when you open the door (ultrasonic sensor measure a lower distance). The problem to solve is that sometimes people leave the door open (distance < 30), and in that case the LEDs should become green after 5 seconds, and after the door is closed (distance >30) the LEDs should be RED and the indicator should work normally. Now if the door is leaved open, the LEDs toggle from red to green at every 4 seconds.
Have you considered what would happen if the door is left ajar? What if the distance is exactly 30 cm? What if the draught makes it oscillate just around the 30-cm mark?
It never happened, It's sliding, without hinges.
I just have to find a way to make it work in as much as possible situations without adding an PIR sensor or any other extra sensors. The most often situation is when you left door open. Maybe if I make the LED switch when you close the door, and not when you open it could help a little.
Sorry, it was a typo on post writing when I copied the code, it’s 1 in my code, the toggle function works, but I don’t know how to make it show green when door is open for more than 10 seconds and then work normally again when you close the door, starting with Red.
Can you fully explain the behaviour you're looking for? So two LEDs, red and green. What are the various states/behaviours and at what distances do they occur?
Two RGB leds that have to show the same color in the same time (for 2 different rooms).
When distance from sensor is < the leds toggle, from red to green and viceversa, but when you leave the door open, I want the leds to be green. And then red when you close it and so on.
With this code the Red LED is always lit when you close the door,so if you leave with the door closed, it will show busy (RED). The green LED is on when door is open for longer than 4 seconds, but the toggle between lights when you close and open the door does not work anymore.
Yes, but the toggle code should still be there. I think the logic is this, but I don't know how to do it:
The actual code is some kind of "normalMode", when the toggle LED code works.
Setup -> bool normalMode = true;
And when the door is open for more then, let's say 10 seconds,
the LEDs should turn green, like the room is free, in case you leave the sliding door open,
for ex. if distance < 30 && more then 10 seconds elapsed, normalMode = false.
if Normal mode = false {
analogWrite(green_LED, 225);
and then when you close the door (distance > 30), it should get back to the "normalMode" starting with LEDs being RED (room is busy).
I still don't think you've fully explained the problem you're trying to solve here and all of the scenarios inside. I think you're (understandably) trying to explain it using limited programming knowledge.
Can you try explaining the problem your project is trying to solve and your proposed solution, in as great detail as possible using little to no programming terms?
I've tried to read and amalgamate all of your previous hints at what you would like here. Because the more I pry the more info we're getting.
It seems to me like the setup is like an 'occupied' notifier for something like a meeting room, is that right?
So if the door is closed (meeting in session), you want the red LED to come on, to tell people that the room is busy and not to disturb.
If the door is open, you want the red LED to go off.
If the door has been left open for more than 5 seconds you want the green LED to come on so that people can see that the room is free to be used again?