- :-

Topics Covered

  • Color Sensor
  • If Statements
  • Else Statements
  • Check Your Understanding:
    1. 1. When using an if statement, the program will run what is "inside" of the curly braces of the if statement:
    2. If the condition of the if statement is true
      If the condition of the if statement is false
      The program will always run what is inside of the curly braces, no matter if the condition of the if statement is true or false
    3. 2. True / False: The command "getColorName" asks the color sensor what color it sees by name.
    4. True
      False
    5. 3. In the second lesson video, if the robot was placed on a blue color tile, instead of a red one, and the
      same program was used, what would the robot do if the program was run?
    6. The robot will turn to the right
      The robot will turn to the left
      The robot will not move
      The robot will move backwards
    7. 4. When using an if statement, the program will run what is "inside" of the curly braces:
    8. If the condition is true
      If the condition is false
      The program will always run what is inside of the curly braces
    Try It!
    Try it! 1

    Color Sensor Values

    The VEX IQ Color sensor is capable of several different modes:
    Color Name, Grayscale, Hue, and Proximity. The readings from the color sensor are extremely sensitive to “ambient” light in the robots environment.

    Download and run this program to output the values from the sensor in each of the modes to the VEX IQ Screen.

    Try it!

    Real Robot users

    • After downloading the program, move the robot by hand to differently colored objects and surfaces, keeping the sensor 2-3 centimeters from the object.
    • Notice how different hue values correspond to different color names, and how changes in the ambient light of your environment affect the sensor.

    Virtual Robot users

    • After downloading the program, in ROBOTC go to Robot > Debugger Window > VEX IQ Remote Screen to view the values from your computer screen.
    • Use the W, A, S, and D keys on the keyboard to maneuver your robot around the virtual table, pointing the color sensor at the differently colored objects.
    • Notice how different hue values correspond to different color names.
    What happens?
    As the color sensor is pointed at differently colored objects, the values on the screen update. Certain hue values correspond with certain color names. If the lighting in the environment varies, so do the sensor readings.
    Try it! 2

    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.



    What if we wanted to add some more functionality to the program? For example, what if we wanted the robot to turn right only if it detected blue?

    With our current program, we cannot add a condition to the else. Currently, the else just means that the if is not true. We want to instead be able to say that the robot should turn right only if the color sensor detects blue.



    With ROBOTC, we can do this. We can add an "if" to the "else" statement and create an "else/if"



    Make sure you leave a space after the else and write "if(getColorName(colorDetector) == colorBlue"

    Now the robot will turn left if the color is red and right if the color is blue.

    What happens if the robot does not detect a red or blue color?

    Try it!
    What Happens?
    With the updated program, the robot would first detect for red. If it doesn't detect red, it would then detect for blue. If it doesn't detect blue, it would then move to the end of the program, thus doing absolutely nothing.



    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?

    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.

    They are not actionable commands, so they do not end with semicolons.

    Actionable items such as forward, backward, turnLeft, turnRight, and waitUntil commands are known as statements in the “ROBOTC” programming language and end in semicolons.

    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.

    In this example when getColorName(colorSensor) is equal to red the program will continue.

    An "if" statement like the one in this program, on the other hand, says:

    "If... the condition is true right now... then complete the code within the curly braces. Otherwise, skip this section of code."

    Color Sensor (Conditionals) - Mini Challenges

    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.

    If the color tile is Red, the robot should travel to room A.
    If the color tile is Green, the robot should travel to Room B.
    If the color tile is Blue, the robot should travel to Room C.
    • Recreate the table below on any surface. Have the robot move from the start (1) to the center. Depending on the center, the robot will then move to the room (A), (B), or (C), corresponding with the color of the center.

    Mini Challenge PDF[ Signal Reader Challenge.pdf ]

    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.

    • Recreate the table below on any surface. Space out lines even and randomize it either red or green. Program the robot to go from one end of a table to the other side, using its color sensor.