running something once...

hey guys, lets say i wanted to run something only once when something happens… like if a ping)))) US detector references an object at a certain range infront of it… say 1 foot… then what would i use to make a servo move a certain amount, then stop… and then continue on with other code

so i was thinkign an if statement, but how would i let it run only once - e.g. i roll a “car” towards a wall with the setup described above, and the car’s “brakes” must kick in when the sensor finds an object 10 cm away from it?

by “brakes” i mean have a servo turn 1/4 a turn, for simplicity sake…

pls help… need to know :-?

You’ll need to have an integer variable that represents the “state” of the car. Then, changes in state can be triggered by events, e.g. from “RUN” state to “STOP” state when an obstacle is detected, or from “STOP” to “REVERSE” after a certain time has elapsed. The state variable will then determine what the motors, servos, etc. do at any given time.