In the previous activity, you wrote a BlocklyProp program that turned lights on and off depending on the detection state of each QTI. To make the SumoBot avoid the white border, this activity will guide you through adding another switch...case block for maneuvers. It will use the same qtis variable with switch and the same case values, but the do (then break ✓) inputs will contain CR servo PIN speed and pause (ms) blocks for maneuver wheel speeds and run times.
This code example adds a switch...case block that performs maneuvers immediately after the one that decides which LED lights to turn on/off.
With each run function block, the program jumps to the function, executes the blocks contained by the function block, and then jumps back to continue at the next block below the run function block. Looking inside the repeat forever…block, the first block is run function "QTIs Front". Even though the function QTIs Front block is collapsed, the blocks it contains still get run. So, it checks the QTI sensors and sets binary 1s and 0s in the qtis variable. When it’s done, the program picks up where it left off below the run function "QTIs Front" block.
Note that a disabled block does not run. So the run function "QTIs Front Terminal" block gets skipped, and the next block is run function "QTI LED Indicators". The program jumps to that function block and goes through the switch...case block to turn the LED lights on/off. When that’s done, the program jumps back to the next block in the repeat forever loop, which is the switch...case block for the maneuvers you added.
The maneuvers switch...case block executes a different set of blocks depending on the “case” of the qtis variable. In the case where qtis is 11, it means both QTIs are over a black surface, so the SumoBot goes forward with no pauses. After that, the (then break ✓) leaves the switch block. At that point, the code reaches the end of the repeat forever block, and it starts over at the beginning. So, the QTIs get rechecked again, the LED lights get updated, and then the program comes back to this switch...case block in a few thousandths of a second. That allows the program to respond immediately to a case where one of the binary digits in the qtis variable is 0, indicating the white sumo ring border is detected. For example, if the right QTI detects white, the case might be 10. The blocks in that case make the SumoBot back up for 600 ms, then turn left for 600 ms before breaking from the switch...case and repeating the loop.