Detecting a threshold position in movement
A limit switch, such as one of these:



They're cheap and plentiful. For a switch like that, you would connect the digital input pin of the microcontroller to the common pin of the switch. The NO and NC pins of the switch would be connected to power and ground. When the switch is relaxed, the digital input sees one voltage. When the object gets close enough to depress the switch, the digital input sees the other voltage. Your microcontroller then tells the motor to stop.

For a non-mechanical solution, you can use a magnetic proximity sensor. The simplest would be a reed switch. When a magnet mounted on the moving part gets close to the stationary reed switch, the switch activates and signals to the microcontroller similar to the limit switch. Reed switch are also very cheap.

Beyond that, there's a world of proximity sensors that represent the whole range of cost, complexity, and accuracy. Just type "proximity" into Digikey to get an idea.