Skip to main content

Lesson 3: Using [If then] blocks

In the Disk Maze Challenge, the VR Robot is checking the condition of the Front Eye Sensor to see what color is being reported. Each of the colors matches to a different behavior the VR Robot should perform.

VEXcode VR If Then C block with a boolean parameter of 'Front Eye detects red?'.
  • Start a new project and name it Unit7Lesson3.

    VEXcode VR Toolbar with the 'Project Name' button highlighted in a red box, to the left of the Select Playground button. The project name is set to Unit 7 Lesson 3.
  • Drag an [If then] block into the workspace and attach it to the [When started] block.

    VEXcode VR blocks program with a When Started block and an empty If Then C block.

    For Your Information

    The [If then] block is a C block that accepts Boolean reporter blocks as input. Using the patterns observed while solving the Disk Maze in Lesson 2, a project can be created where the VR Robot makes a decision if the condition reports as TRUE or FALSE.

    VEXcode VR If Then C block with a red box highlighting the boolean parameter space.

    The [If then] block will run one time in a project. In this example, the Front Eye Sensor will report TRUE if a green object is detected, and execute the command inside the C block - turn right for 90 degrees. If a green object is not detected, the project will move to the next command in the stack.

    VEXcode VR blocks project that turns right if the Front Eye is detecting the color green. The project reads: When started, if front eye detects green, then turn right for 90 degrees. Outside of the if statement is a stop driving block.

    Using Switch Blocks 


    The image below shows the Switch [If then] block statement for this Lesson's example project. 

    The same VEXcode VR blocks project but with switch blocks included, that turns right if the Front Eye is detecting the color green. The project begins with a When started block and is followed by a switch C block version of an If Then block. The Python code reads 'if front_eye.detect(GREEN): drivetrain.turn_for(RIGHT, 90, DEGREES)'. Outside of the if statement is a stop driving block.

    The project below includes the same commands converted into one Switch block.  

    The first line of code, if front_eye.detect(GREEN): is the conditional loop that checks whether the Front Eye Sensor detects the color green. Note that there should be a colon (:) at the end of this line. 

    The second line of code, drivetrain.turn_for(RIGHT, 90, DEGREES) tells the robot to turn right for 90 degrees when the Front Eye Sensor detects the color green. This line is indented at a default of 4 spaces because this is the specific behavior that will be performed if the Front Eye Sensor detects green. 

    The same VEXcode VR blocks project but with switch blocks included, that turns right if the Front Eye is detecting the color green. The project begins with a When started block and is followed by a switch C block version of an If Then block. The Python code reads 'if front_eye.detect(GREEN): drivetrain.turn_for(RIGHT, 90, DEGREES)'. Outside of the if statement is a stop driving block.

  • Drag a <Color sensing> block into the workspace into the hexagonal input of the [If then] block.

    VEXcode VR blocks project that uses an if statement to sense the color of a disk. It begins with a When Started block and then a comment block reading 'if Front Eye detects green then turn right'. Lastly there is an empty If Then block with a boolean parameter of a Color Sensing block that reads 'Front Eye detects red?'.
  • Change the parameter of the <Color sensing> block to ‘green.’

    VEXcode VR blocks project that uses an if statement to sense a green disk. It begins with a When Started block and then a comment block reading 'if Front Eye detects green then turn right'. Lastly there is an empty If Then block with a boolean parameter of a Color Sensing block that reads 'Front Eye detects green?'. The color dropdown menu is open to change the color from red to green.
  • When the Front Eye Sensor detects a green disk, the VR Robot will need to turn right 90 degrees to begin solving the Disk Maze. Drag a [Turn for] block into the [If then] block.

    VEXcode VR blocks project that uses an if statement to sense a green disk and then turn right. It begins with a When Started block and then a comment block reading 'if Front Eye detects green then turn right'. Lastly there is an If Then block with a boolean parameter of a Color Sensing block that reads 'Front Eye detects green?' and a Turn right for 90 degrees block inside the If container that will trigger if the boolean is true.
  • Follow these same steps to add another [If then] block for all three colors in the Disk Maze (green, blue, and red). Each color is associated with a different behavior. The blocks inside the [If then] block will need to match that intended behavior. Add the following blocks to the project. When the VR Robot detects the color ‘blue,’ the VR Robot will turn left 90 degrees.

    The same VEXcode VR blocks project from before but with an added if statement to turn left if there is a blue disk. It begins with a When Started block and then there is an If Then block with a boolean parameter of a Color Sensing block that reads 'Front Eye detects green?' and a Turn right for 90 degrees block inside the If container. Lastly, there is the same if block and turn block stack but changed to turn left for 90 degrees if the color blue is detected.
  • When the VR Robot reaches the end of the maze, the VR Robot will detect the color ‘red.’

    VR robot in the VR Disk Maze playground, the robot is stopped and looking towards a red disk.
  • To stop the VR Robot, another [If then] block needs to be added with instructions for what to do when the VR Robot detects ‘red.’ Add the following blocks to the project.

    The same VEXcode VR blocks project from before but with an added if statement to stop driving if there is a red disk. It begins with a When Started block and then there is an If Then block with a boolean parameter of a Color Sensing block that reads 'Front Eye detects green?' and a Turn right for 90 degrees block inside the If container. Next there is the same if block and turn block stack but changed to turn left for 90 degrees if the color blue is detected. Lastly, the same if block and turn block stack is used but changed to Stop Driving if the color red is detected.
  • The VR Robot can also make a decision if it does not see a color by using the ‘none’ value. At the starting point of the Disk Maze, the VR Robot does not detect any colors.

    The VR playground view with the dashboard opened and a red box highlighting the robot's Front Eye sensing values. The current 'Object' value is false, and the current 'Color' value is none.
  • Because the Front Eye Sensor does not detect a color at the beginning of the Disk Maze, the VR Robot will need to drive forward until it detects the green disk. Another [If then] block can be added to instruct the VR Robot for what to do when no color is detected.

    VEXcode VR Color Sensing block, that reads 'Front Eye detects none?'. There is a dropdown menu open to show how the user can change the target color to none.
  • Add the following blocks to the project. When the VR Robot does not detect any color, the VR Robot will drive forward.

    The same VEXcode VR blocks project from before but with an added if statement to drive forward if there is no disk detected. It begins with a When Started block and then there is an If Then block with a boolean parameter of a Color Sensing block that reads 'Front Eye detects green?' and a Turn right for 90 degrees block inside the If container. Next there is the same if block and turn block stack but changed to turn left for 90 degrees if the color blue is detected. Next, the same if block and turn block stack is used but changed to Stop Driving if the color red is detected. Lastly, the same if block and turn block stack is used but changed to Drive Forward if no color is detected.
  • The VR Robot now has instructions for each color detected by the Eye Sensor.
    • Front Eye Sensor detects ‘green?’
      • Turn right 90 degrees
    • Front Eye Sensor detects ‘blue?’
      • Turn left 90 degrees
    • Front Eye Sensor detects ‘red?’
      • Stop driving
    • Front Eye Sensor detects ‘none?’
      • Drive forward
  • Open the Disk Maze Playground and run the project.

Select the Next button to continue with the rest of this Lesson.