Topics Covered
Color Sensor Values
The EV3 can detect seven different colors plus the absence of color. Each of these eight colors is labeled with a different number.
The color sensor provides a value for each of the seven different colors and the absense of color that it sees.
![]() |
Another Else Statement
Right now, the program tells the robot to turn left if the color sensor detects red and to turn right if it detects any other color.
With the updated program, the robot would do nothing if neither blue nor red is detected.![]() We can now just add another else statement to the program. This else would run when the if statement is false and the else if is also false. In other words, when the robot does not detect either red or blue. If the robot detects any other color, let's just tell it to move forward. Save the program and run it. The robot should now turn left if the color is red, right if the color is blue and move straight if the color sensor detects any other color. Keep in mind, we can add more than one else if to our programs. |
Did you know?
Concept of Whitespace![]() |
White space is used by programmers to keep their code clean and organized. White space is ignored the robot or compiler. White space includes indentation, spaces, and new lines within a program. Indentation is often used to signify which programming structures (tasks, conditionals, loops, etc.) and commands are embedded within other programming structures. Blank lines are often used to separate different behaviors within a long program. |
Did you know?
Semicolon vs. No Semicolon![]() |
Up until now, you may have noticed that certain commands end with semicolons, and some do not. |
|
![]() |
Loops and conditional statements like “if/else” create structure and decision making within a program. |
Did you know?
Conditional Structures![]() |
If statements control the flow of the program by containing blocks of code. |
|
![]() |
Code "inside" of an if statement is denoted by a set of curly braces. |
Did you know?
If Statements vs. Wait Untils![]() |
A "waitUntil" command waits – it does nothing – until the condition within the parenthesis is true and then it continues on with the program.
|
|
![]() |
An "if" statement like the one in this program, on the other hand, says: |
Mini Challenge 1: Signal Reader
Program the robot so that it travels to the middle of the challenge table where there will be a color tile. The robot then makes a decision about its next movement based upon the color of the color tile.
|
Mini Challenge 2: Ladder Drill
Program the robot to make from one end of a table to the other side, using its color sensor.
If the robot detects red, have it move backwards 1 rotation.
If the robot detects green, have it move forward 4 rotations.
|