Three or More State Programs Pro Preview

In more advanced robotics programming, your robot may need to respond in three or more different ways depending on sensor input. Up to this point, we've created programs with only two possible outcomes using a switch block: a condition is checked, and the robot performs one action or the other based on the result.

But what if the robot needs more than just two responses? To handle this, we can use multiple conditions—often implemented with nested switch blocks or structured decision chains. In this section, we’ll explore how to build programs that go beyond binary choices and enable your robot to react intelligently to more complex environments.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #893
  • 30 Mar 2018

Let’s revisit the 3-state line-following example using the following setup:

Threshold Diagram

Suppose we use a single switch block that checks whether the value read by the color sensor is less than 27:

Initial Switch Block

In this case, the robot knows to turn right when the value is less than 27. However, it has no instructions for what to do when the value is greater than or equal to 27. At that point, the robot needs to choose between going straight or turning left. If the value is less than 37, it should go straight. If it's greater, it should turn left. So how can we add this decision-making?

The same way we handled the first condition—by using another switch block. If we place a second switch inside the "false" branch of the first one, comparing the value to 37, we can handle all three cases. Since the second switch is only evaluated if the first condition is false, we already know the value is at least 27 when it runs.

Nested Switch Diagram

This approach—placing a switch block inside another—is known as a nested switch.

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for Instructors Remote Training
  • 183
  • 280:11
  • 156
Image for Competition programming
  • 7
  • 0
  • 8
  • 3d_rotation 1
Image for Level D2. "Seafaring". Robotics with LEGO
  • 24
  • 2:49
  • 107
Image for Lesson 6 - Catching the wind
  • 3
  • 5
  • 9
  • 3d_rotation 1
Image for Level C1. "Transportation". Robotics with LEGO
  • 33
  • 1:03
  • 110
Image for Robotics with LEGO - Level 2.0 - Robots in Factories
  • 44
  • 15:01
  • 129
Image for Robotics with LEGO - Level 2.0 - Robots in Factories
  • 44
  • 15:01
  • 129
Image for Lesson 5 - Robo Car
  • 6
  • 4
  • 11
  • 3d_rotation 1
Image for Robotics with LEGO - Level 2.5 - Maritime Journey
  • 42
  • 3:52
  • 133
Image for Robotics with LEGO - Level 2.5 - Maritime Journey
  • 42
  • 3:52
  • 133
Image for Lesson 6 - Catching the wind
  • 4
  • 5
  • 11
  • 3d_rotation 1
Image for Build, Code, Play: Your First Adventures with SPIKE Prime
  • 4
  • 0:33
  • 27