State Machine

We now have all the programming tools at our disposal to implement a fairly sophisticated program structure, called a state machine. State machines are programs where you break the desired robot actions into a series of modes, or statesTake a moment to look at the strange version of a flowchart shown below. This flowchart is a version of a state diagram.B8 State Diagram

State Diagrams, just like the standard flowcharts we’ve been using, use arrows to indicate flow between states, also known as transitions. When there is a decision to be made, they have multiple arrows flowing out, with a label used to indicate which decision flows to which state. Another interesting concept in this exercise is that we can stay in a state until something happens. Take a look at the Idle state. What happens if there is no sound and there is no touch? In order to name our states, we will need to use string constants. String constants can output text and numbers, which means that the name of our state can match the action being performed. This makes your code much easier to follow.

Left Arrow Right Arrow