This is an example program that demonstrates how to use a color sensor to select a set of commands.
There are several parts to this program. The first is a main loop. In this example it goes on infinitely.
The second part is the first switch block. It tells the color sensor in port 3 to look for the color red.
The true part means the color sensor in port 3 found the color red, so it should do the first code branch. In the example, this is to make a barking sound and pivot to the right.
The false part means the color sensors did not find red, so go on to the next check of the color sensor.
At the second check of the color sensor, it looks for the color yellow. If it finds the color yellow, then it starts the second code branch. If it does not find the color yellow, then it exits both of the switch statements with doing nothing. Because it is still in the main loop, it will keep repeating the process to search for the chosen colors.
If you wanted to add more choices, add more switch blocks and follow the pattern.
if sensor3 equals red then:
<Do code branch #1>
if sensor3 equals yellow then:
<Do code branch #2>
} while (true)
Post a Comment