State
Learn about states and state manipulation

How to Control State?

We can use our knowledge of if statements (click here for more information about if statements) and variables (click here for more information about variables) to create modes or states for our robots. We can use states to set our robot into different modes, for example we could put it into fast mode, or autonomous mode, or remote control mode. To do this we will have a variable named state that will keep track of which mode we are in. But our variables can only hold numbers or letters, so how can we set it to fast mode? What we will do is assign each mode a value, for example fast could be 0, autonomous is 1, and remote control is 2. Then we will have several if statements to determine which states it is in.

But it can be hard to remember which one state 2 is. All that shows which number goes with each mode is the code itself or any comments in the code. To fix this we will use more variables to keep track of each mode. This way we just need to remember the name of the variable, but not the value with it.

So an example code would be like this:

This code will print out a different message and then wait a different amount of time depending on the state. You can either change the mode and re-download the program or you could also create a way to change the mode on the board. So you could try to have a system that if this button is pressed it will go to mode slow, and then this button is mode fast, and so forth.