Optical Sensing#
To make Optical Sensor blocks appear in VEXcode V5, an Optical Sensor must be configured in the Devices window.
For more information, refer to these articles:
Set Optical Mode#
The Set Optical Mode block is used to set an Optical Sensor to either detect colors or gestures.
set [Optical7 v] to [color v] mode
By default, an Optical Sensor is set to always detect colors. Before using any Optical Sensor gesture blocks, the Optical Sensor must be set to detect gestures.
Choose which Optical Sensor to use.
Choose whether you want to set the mode of the Optical Sensor to either detect colors or gestures.
In this example, the Optical Sensor is set to detect gestures before waiting until a left gesture is detected to print a message.
when started :: hat events
[Set Optical sensor to detect gestures.]
set [optical7 v] to [gesture v] mode
[Don't print the message until a left gesture is detected.]
wait until <[Optical7 v] gesture [left v] detected?>
print [Left gesture detected.] on [Brain v] ◀ and set cursor to next row
Set Optical Light#
The Set Optical Light block is used to set the light on the Optical Sensor to on or off. The light lets the Optical Sensor see objects if it is looking at an object in a dark area.
set [Optical7 v] light [on v]
Choose which Optical Sensor to use.
Choose whether to turn the light on or off.
In this example, the Optical Sensor will turn its light on for two seconds before turning it off.
when started :: hat events
set [Optical7 v] light [on v]
wait (2) seconds
set [Optical7 v] light [off v]
Set Optical Light Power#
The Set Optical Light Power block is used to set the light power of Optical sensor
set [Optical7 v] light power to (50) %
The The Set Optical Light Power block block accepts a range of 0% to 100%. This will change the brightness of the light on the Optical Sensor. If the light is off, this block will turn the light on.
Choose which Optical Sensor to use.
In this example, the Optical Sensor’s power light is set to 75% before it waits to detect an object to print a message.
when started :: hat events
[Set the Optical Sensor's light power to 75%]
set [Optical7 v] light power to (75) %
[Don't print the message until an object is detected.]
wait until <[Optical7 v] found an object?>
print [Object detected.]on [Brain v] ◀ and set cursor to next row
Optical Sensor Found Object#
The Optical Sensor Found Object block is used to report if the Optical Sensor detects an object close to it.
<[Optical7 v] found an object?>
The Optical Sensor Found Object block reports True
when the Optical Sensor detects an object close to it.
The Optical Sensor Found Object block reports False
when an object is not within range of the Optical Sensor.
Choose which Optical Sensor to use.
In this example, the Optical Sensor’s power light is set to 75% before it waits to detect an object to print a message.
when started :: hat events
[Set the Optical Sensor's light power to 75%.]
set [Optical7 v] light power to (75) %
[Don't print the message until an object is detected.]
wait until <[Optical7 v] found an object?>
print [Object detected.] on [Brain v] ◀ and set cursor to next row
Optical Sensor Detects Color#
The Optical Sensor Detects Color block is used to report if the Optical Sensor detects the specified color.
<[Optical7 v] detects [red v] ?>
The Optical Sensor Detects Color block reports True
when the Optical Sensor detects the specified color.
The Optical Sensor Detects Color block reports False
when the Optical Sensor doesn’t detect the specified color.
Choose which Optical Sensor to use.
Choose which color the Optical Sensor will check for.
In this example, the Optical Sensor’s will wait until it detects a blue object before printing a message.
when started :: hat events
[Don't print the message until the color blue is detected.]
wait until <[Optical7 v] detects [blue v] ?>
print [Color blue detected.] on [Brain v] ◀ and set cursor to next row
Optical Brightness#
The Optical Brightness block is used to report the amount of light detected by the Optical Sensor.
([Optical7 v] brightness in %)
The Optical Brightness block reports a number value from 0% to 100%.
A large amount of light detected will report a high brightness value.
A small amount of light detected will report a low brightness value.
Choose which Optical Sensor to use.
In this example, the Optical Sensor will print the current brightness value to the Brain’s screen.
when started :: hat events
print ([Optical7 v] brightness in %) on [Brain v] ◀ and set cursor to next row
Optical Hue#
The Optical Hue block is used to report the hue of the color of an object.
([Optical7 v] hue in degrees)
The Optical Hue block reports a number value that is the hue of the color of an object. It returns a number between 0 and 359.
The value can be thought of as the location of the color on a color wheel in degrees.
Choose which Optical Sensor to use.
In this example, the Optical Sensor will print the currently seen hue to the Brain’s screen.
when started :: hat events
print ([Optical7 v] hue in degrees) on [Brain v] ◀ and set cursor to next row
Optical Sensor Detects Gesture#
The Optical Sensor Detects Gesture block is used to report whether an Optical Sensor has detected the specified gesture.
<[Optical7 v] gesture [up v] detected?>
Important: The Optical Sensor must first be set to detect gestures using the Set Optical Mode block, otherwise it will not detect any gestures.
The Optical Sensor Detects Gesture block reports True
when the Optical Sensor detects the specified gesture.
The Optical Sensor Detects Gesture block reports False
when the Optical Sensor doesn’t detect the specified gesture.
Choose which Optical Sensor to use.
Choose which gesture the Optical Sensor will check for.
In this example, the Optical Sensor is set to detect gestures before waiting until a left gesture is detected to print a message.
when started :: hat events
[Set Optical Sensor to detect gestures.]
set [Optical7 v] to [gesture v] mode
[Don't print the message until a left gesture is detected.]
wait until <[Optical7 v] gesture [left v] detected?>
print [Left gesture detected.] on [Brain v] ◀ and set cursor to next row