It's corn maze season right now, so here is a little program to help you solve any maze...eventually. The main body of the code is on the left and a function, or a sub program, is on the right. The function HeadingColor isn't necessary to solve the maze, but adds visual and audio output that is nice for the user to have. You can see the whole program below and the breakdown of it below that.
We start out the program with some output to let the user know the program has started. Then we setup initial values for the variables numHeading and numSpeed.
We move into setting up a forever loop, as we want part of this program to happen repeatedly. At this point, Sphero executes the HeadingColor function (passing the numHeading variable as a parameter) and then rolls away. The function is just another group of commands that happens as its own little program within a program. We could have put all of those code blocks in the main code instead of packaging it as a function, but it makes the program cleaner and easier to understand the flow of the program this way.
Here we ask a question, "What happens when Sphero's speed is zero?" The If...Then statement allows Sphero to ask a question and do something based on the answer to that question. In this case, the code checks the speed sensor data to see if it is 0, or that it isn't moving. If Sphero isn't moving it must be against a wall or in a corner and that isn't the exit, so it executes the code inside of the If...Then statement. In this case, it sets the numHeading variable to some new random number. It then exists the If...Then statement and loops back up to Main Code - part 2. Because the roll command in part 2 has a duration set to 1 second, Sphero checks its speed every second to see whether it needs to change directions.
Just like the main code, this starts out with some audio output. Having received the current heading as a parameter, the speak command lets the user know what direction the Sphero is headed. They we have an If...Then...Else statement that asks a question, in this case is the heading greater than or equal to zero and less than 90. And if it is then set the mainLED to green. If it isn't 0-89, then do something else.
The goal of the function is to set the color of the mainLED to a different value depending on which quadrant it is headed towards. To do this with Sphero we use nested If...Then...Else statements. Essentially we ask if something is true and if it isn't true, then we ask if something else is true. We do this until we don't have anymore questions to ask. In this example, we don't have to ask if the heading is >=270 or <360, because that is the only quadrant left after we have asked about the first 3.