Skip to main content

Lesson 4: Coding the Object Sensor

Lesson 4: Coding the Object Sensor

In the last Lesson, you learned about the Object Sensor and collected the reflectivity percentages of Disks and Cubes. Now, you will use those values to build a project in VEXcode to stop the Exit Conveyor when a Disk is detected by the Object Sensor.

In this Lesson, you will:

  • Configure the Object Sensor in VEXcode.
  • Learn how to use comparison operators in VEXcode.
  • Create a project to move a Disk from the Entry Conveyor to the Exit Conveyor using feedback from the Object Sensor.

By the end of this Lesson, you will have built on your project from the previous Lesson to stop the Exit Conveyor when the Disk is detected by the Object Sensor.

Green disk on the Exit Conveyor and underneath the Object Sensor.

Editing the Project

You will begin by editing your project from the Unit 4 Lesson 2 Activity to prepare for adding the commands related to the Object Sensor.

Open your Unit 4 Lesson 2 Activity project in VEXcode EXP, or recreate the project shown here.

Rename the project Unit 4 Lesson 4.

Three block stacks that are from the Unit 4 Lesson 2 project. The first stack reads When started, spin entry conveyor 1 inbound, wait 7 seconds, and then stop entry conveyor 1. Next, spin transport conveyor 2 forward, wait 4 seconds, and then stop transport conveyor 2. Lastly, spin exit conveyor 4 outbound, wait 2 seconds, and then stop exit conveyor 4. The next stack reads When signal tower 6 bumper pressed, set arm 10 to control stopped. The last project reads When arm 10 control stopped, set signal tower 6 to green and off and then set signal tower 6 to red and blinking. Lastly, stop entry conveyor 1, stop transport conveyor 2, and then stop exit conveyor 4.

Configure the Object Sensor in VEXcode by opening the Devices Window and selecting Add a Device.

Devices Window is open and the Add a Device button is highlighted.

Select 3-Wire to open the list of 3-wire devices.

Device options in the Devices Window, with the 3 Wire icon called out in red.

Then, select the Object Sensor.

3 Wire Device options in the Devices Window, with the Object Sensor icon called out in red.

Select port A, then Done to complete the configuration.

Note: Be sure the Object Sensor is plugged into port A on the Brain.

Devices Window with Port A highlighted for the Object Sensor.

Remove the last two blocks in the project. This includes the Wait block and the Stop block for the Exit Conveyor, as shown in this video.

Video file

Add a Wait until block to the project.

The Wait until block is a block that waits for a Boolean condition to report as true before moving on to the next block in the stack.

Continuation of a VEXcode EXP stack with an empty Wait until block added to the end of it. The stack reads When started, spin entry conveyor 1 inbound, wait 7 seconds, and then stop entry conveyor 1. Next, spin transport conveyor 2 forward, wait 5 seconds, and then stop transport conveyor 2. Lastly, spin exit conveyor 4 outbound and then Wait until.

Comparison Operators

Now that the Wait until block is added to the project, the condition to be checked by the block needs to be added. 

Previously, you learned about the reflectivity percentage reported by the Object Sensor and recorded those values in your engineering notebook. In this project, you need to the Exit Conveyor to run until that value is above a certain threshold. A threshold is something used to determine if a value is above, below, or within an acceptable range. Comparison operators can be used to check if the reflectivity is above a certain value, or above that threshold.

Add a Greater than block to the hexagonal space of the Wait until block.

The Wait Until block that was just added, with a new boolean parameter of 0 greater than 50.

Add a Reflectivity block in the first opening of the Greater than block.

The Wait Until block now with an Object Sensor Reflectivity block added into the greater than block. The whole block together now reads Wait until object sensor A reflectivity in % is greater than 50.

The Greater than block compares the first value to the second. The Greater than block currently in the project is comparing the reflectivity percentage reported by the Object Sensor to the parameter '50' before declaring the Boolean as True or False

The Wait Until block with the Greater than value highlighted. The whole block together now reads Wait until object sensor A reflectivity in % is greater than 50.

Think about the goals of this project. The Exit Conveyor needs to spin until the Object Sensor detects an object, then the conveyor needs to stop. The values collected in the previous Lesson can be used with this Greater than block to determine the threshold that needs to be exceeded before the project moves to the next block in the stack.

Change the second parameter of the Greater than block to '9.' 

This number is based on the values collected in the previous Lesson's activity. The reflectivity was reported as 10% when the green Disk was under the Object Sensor, so anything greater than 9% should report as True.

The Wait Until block with the Greater than value highlighted and changed from 50 to 9. The whole block together now reads Wait until object sensor A reflectivity in % is greater than 9.

Add a Stop block to the end of the project. Be sure the parameter is set to 'ExitConveyor4.'

Continuation of the VEXcode EXP stack with a Stop Motor button added to the end that reads Stop exit conveyor 4. The stack reads When started, spin entry conveyor 1 inbound, wait 7 seconds, and then stop entry conveyor 1. Next, spin transport conveyor 2 forward, wait 5 seconds, and then stop transport conveyor 2. Lastly, spin exit conveyor 4 inbound, Wait until object sensor A reflectivity in % is greater than 9, and then stop exit conveyor 4.

Be sure the Brain is connected to VEXcode and download the project.

VEXcode EXP Toolbar with the Download icon called out in a red box, in between the Brain and Run icons.

Read the code and predict what the conveyors will do when the project is run. 

Record your prediction in your engineering notebook.

The VEXcode EXP stack that the Stop Motor block was just added to. The whole stack reads When started, spin entry conveyor 1 inbound, wait 7 seconds, and then stop entry conveyor 1. Next, spin transport conveyor 2 forward, wait 5 seconds, and then stop transport conveyor 2. Lastly, spin exit conveyor 4 outbound, Wait until object sensor A reflectivity in % is greater than 9, and then stop exit conveyor 4.

Be sure a green Disk is placed on the Entry Conveyor. Press the Check button on the Brain to run the project to test it.

Observe the behavior of the conveyors as the project runs.

EXP Brain with the diamond Check button called out in a red box to start the Unit 4 Lesson 4 project.

Press the X button on the Brain to stop the project when the conveyors have finished moving. Record your observations in your engineering notebook.

Did the conveyors move as you predicted? Did the green Disk get moved to the Exit Conveyor as intended? Why or why not?

EXP Brain with the diamond X button called out in a red box to end the project.

Reviewing the Project

Let's review the project flow in this project that stopped the Exit Conveyor when the green Disk was detected by the Object Sensor. The Wait until block paused the project from moving forward until the condition within is reported as TRUE. 

Diagram that visualized the flow of logic in the VEXcode EXP project. An arrow indicates the Wait until block starting, and then a cyclical arrow indicates that the block waits until its condition is true. Only when the object sensor A reflectivity in % is greater than 9 will the next block run.

Because the Spin block is a non-waiting block, it allows the CTE Workcell to keep spinning the conveyor motor while checking the values from the Object Sensor and the condition of the reflectivity being greater than 9%. After the condition in the Wait until block reports as TRUE, the project moves to the next block in the stack, stopping the Exit Conveyor motor.

For Your Information

Another combination of blocks can be used in a project to stop the Exit Conveyor when an object is under the Object Sensor. The Set Detection Threshold block can be used at the beginning of a project to set the threshold, just as the Greater than block did in the project that was built above. 

VEXcode EXP Set Detection Threshold block that reads 'Set object sensor A detection threshold to 50%.

Once the threshold is set, the Boolean Detects object block can be used in any blocks with hexagonal spaces. This block will report TRUE when the detection threshold defined by the Set detection threshold block is met, or FALSE when the reflectivity percentage is below that threshold.

VEXcode EXP Detects Object block that reads 'Object sensor A detects object?'.

Because the Detects object block is a Boolean block, it can fit directly into the Wait until block.

Alternate version of the VEXcode EXP stack, with the Wait Until boolean block changed to a Detection Threshold system. The whole stack now reads When started, set object sensor A detection threshold to 9%. Next, spin entry conveyor 1 inbound, wait 7 seconds, and then stop entry conveyor 1. Next, spin transport conveyor 2 forward, wait 5 seconds, and then stop transport conveyor 2. Next, spin exit conveyor 4 inbound, wait until object sensor A detects object, and then stop exit conveyor 4.

Check Your Understanding

Before moving on to the next Lesson, ensure that you understand the concepts in this Lesson by answering the following questions in your engineering notebook.

Check Your Understanding questions > (Google Doc / .docx / .pdf )


Select Next > to move on to the Putting It All Together Activity.