it is not really clear to me what the sensors do in relation to tain is entering block and leaving block. I try to describe in my own words what I'm guessing:
train enters block IR-Sensor1 changes from HIGH to LOW (What is the level of IR-Sensor2 in this moment? LOW or HIGH)
train stops in front of a signal. What output-levels do IR-Sensor1 / IR-Senor2 have in this moment?
You press a button to change signal from red to green ==> train passes signal
train starts to leaves block. If the locomotive of the train reaches IR-Sensor2 IR-sensor2 changes from HIGH to LOW
As long as wagons pass by IR-sensor2 IR-sensor2 stays LOW
When the last wagon of the train has passed by IR-sensor2 IR-sensor2 output-level goes HIGH and that is the moment where the signal shall change back to the state they were in the beginning?
Is this a correct description?
It would be helpful if you could draw a basic plan with free hands
that shows where is the signal and where are the IR-sensors 1 and IR-sensor 2
make two or three of these planes and add train entering
train is in block
train is leaving block
train has left th eblock and describe for each case how the signals and out-put-levels of the IR-sensors are.
So all different conditions can be seen easily. Me too I think a state-machine is the right program-structure to solve this control-problem
best regards Stefan
any newbee can apply the most professional habit from the first line of code they write on their own:
add only ONE thing at a time. Test/debug that ONE thing until that ONE thing works reliable - repeat.
The sad thing is: only the REAL professionals write code this way.