How to stop a motor based on resisters in series

I have a dc motor and h-bridge which I'd like to control by an analogRead along a series of ten 200 ohm resistors. Picture a model train traveling down a track and every 2 inches it reads a value from the resistors in series. I can read the voltage and divide by 100 to determine the resistor "number".

I want to use the sensed number to advance the train to the next position; analogRead +1.

The problem is that analogRead creates a infinite loop. I read resistor 2 and add 1, but the sensor reads 3 and adds 1, then 4...

Is there a way to stop a motor after it has advanced one resistor?

Thanks.

you need to upload the schematic & code here.

Are you actually talking about a model railway layout? If so, say so. If not, tell us what you are actually doing.

I like model trains but I can’t relate your resistors to train detection. As @AMPS-N says, upload your schematic and code.

…R

You need a state machine. Look around these forums for examples.

Think of the states - train is on resistor 1, so it should stay there until it's commanded to move. Once it's received that command, then it is in the "moving 1-2" state, where it drives the motors until the analogRead() detects position 2. Once it's detected that it's in position 2, then it transitions to the "station 2" state which stops the motor and waits for another command.