I am struggling with a sketch which has to do the following:
An infrared sensor is mounted under the track of a model railway.
If it is covered by a passing train it has to switch a signal (which is green in its original state) to yellow and after 2 seconds to red.
Once the sensor is no longer covered it has to switch back to the original state (green).
The one second delay is only to see what is happening,
The sketsch:
Great, you correctly posted your code. Thank you very much. You cannot image how refreshing it is going to be to not have 30 postings on just how to post the code.
I am struggling as well, and I hope this part does not take 30 postings to get right. What's the problem?
If the sensor has not been triggered then the code for the else will be run and the Green pin will be set HIGH then immediately set LOW again at the start of loop()
Do you want it HIGH or LOW when the sensor is not triggered ?
The problem is that once the sensor is covered the signal goes to yellow and after 2 seconds it goes to red.
However the signal keeps switching between red and yellow with the 2 seconds interval.
I want it to stay on red as long as the sensor is covered.
If the sensor is no longer covered the signal switches back to green as expected.
That doesn`t matter, it will always become HIGH after the first loop.
That does not change anything in repeatedly changing between Yellow and Red as long as the sensor is covered.