Need some order in programming!

I’ve run into a bit of a pickle with my coding… When writing programs for stuff like joysticks etc, it’s fine if the program runs every time the microprocessor refreshes(actually that’s a good thing!). However, what if you don’t want that to happen? An example in autonomous mode: you want to take a reference bearing with a digital compass at the beginning of the game. How do you prevent the robot from getting a new “first bearing” every time the processor loops? Or how about ordered events? If I want the robot to drive forward for a certain amount of time and then extend the arm, how do I prevent it from driving forward if it’s already done so (when the processor refreshes)?

Is there some way to carefully control when pieces of code are carried out? Perhaps static integers or “do while” loops (Initialize integer an integer at 0. If you carry out the enclosed piece of code, mark the integer as a 1-so it won’t repeat…)?

Help! :ahh:

Do a search on “state machine” and “state variable” for your answer.

-Kevin