Model railway Arduino control
I'd suggest using a State Machine approach - it's a very powerful technique you'll benefit from learning. Although this is a fairly simple problem and can be solved relatively easily without it, it starts to get very messy and hard to understand and maintain, especially if you want to extend it to control other parts of the railway.

In this case, implement a variable that represents four states:


Running left to right
Waiting at right end
Running right to left
Waiting at left end


In the first state, you keep looking for the right hand sensor to become active. When it does, stop the train and go into the next state. In that state,  wait for the desired time limit to expire, start the train in the opposite direction and go into the next state, and so on.

It's easy to see how you can extend this approach to solve far more complex problems.

In terms of coding, the easiest way is normally to use the state variable in a switch/case statement inside your loop to execute the appropriate section of code depending on the current state. And don't forget to carry out appropriate initialisation before starting the loop.