Exercise: The Brightness Beeper
Building the Brightness Beeper
Programming the Brightness Beeper
While the level of light measured by the Color Sensor can be viewed in the Robot Inventor app, such as in Figure 9-3, this data can also be put to use. As an example, the light level can be made proportional to the pitch of a tone. When the light level changes, by moving the reflector liftarm stack, the pitch will change. Coding examples can be found as follows.
The Word Blocks Code
Step 3: The play beep block produces a sound on the Hub’s speaker. The Color Sensor’s reading, represented as a percentage, is converted into the MIDI note to play. A higher MIDI value corresponds to a higher pitch, so the beep will get higher when more light is reflected into the Color Sensor. The default duration setting of 0.2 seconds is a good choice to produce a quick beep that will rapidly update as the loop restarts.
The Python Code
An object is needed to represent the Color Sensor, given the variable name color in this case 1. A function associated with color is get_reflected_light(), used to measure the brightness of the light reflected from the sensor’s built-in light source. The measurement result from this function is assigned to a variable named brightness 2. However, this brightness value will be between 0 and 100 percent, which won’t work well to represent a musical MIDI note, as these notes range from 43 to 123. So a mathematical manipulation converts the possible brightness values to corresponding values within the desired MIDI range. This new value is assigned to a variable named note 3. The speaker.beep() function plays this note for a duration of 0.2 seconds 4. After the note is played, the loop runs through again to play another note.
A good start on using the Brightness Beeper is to place the stack of liftarms a couple centimeters away from the Color Sensor. A beep should result that quickly repeats. Moving the liftarm stack closer or farther away will make the beep change in pitch. But if the stack is placed very close, such that the stack almost touches the Color Sensor, the tone goes to a low pitch. This low pitch indicates that not much light is being reflected into the Color Sensor, which happens because the direction of the reflected light is not well aligned to hit the light-sensitive part of the Color Sensor.
Project: The Color Sorter
Building the Color Sorter
The Color Sorter has four separate components to build: the baseplate, the chute, the pusher, and the basket. Building instructions for each of these four components are l presented as follows. The final assembly step then combines the chute, pusher, and basket onto the baseplate.
The Baseplate
The Chute
The Pusher
An illustration displays how a pusher connects to a platform of rectangular frames to build a color sorter.
The Basket
The basket holds the sorted liftarms, with the selected color on one side and other colors on the opposite side. A motor spins the basket to change the side of the basket in which the liftarms are dropped. The two halves of the basket are separated by a wall.
Final Assembly
Programming the Color Sorter
Load a liftarm onto the Color Sensor by moving the pusher motor. Wait for one second to allow the Color Sensor to take a measurement. If the measured color is the one preselected (red in this example), set the basket motor’s angle position to one side of the basket. For all other colors, orient the basket motor to the other side of the basket. Push the liftarm into the basket by moving the pusher motor. Repeat these steps for all six liftarms loaded into the chute.
- 1.
Start the program.
- 2.
Set the basket motor speed to 25 percent.
- 3.
Set the pusher motor speed to 100 percent.
- 4.
Create a loop that repeats six times.
- 5.
Move the pusher motor clockwise to the 180-degree position.
- 6.
Delay for one second.
- 7.
Determine whether the liftarm color is red. If yes, go to step 8. If no, go to step 9.
- 8.
Move the basket motor to the 180-degree position.
- 9.
Move the basket motor to the zero-degree position.
- 10.
Move the pusher motor counterclockwise to the zero-degree position.
- 11.
Stop the program.
The Word Blocks Code
Step 2: The set speed to block assigns the speed of the basket motor. Without this block, a motor will assume a default speed of 75 percent, which could cause jerky motions for the basket. A slower speed will move the basket smoothly.
Step 3: Another set speed to block assigns the pusher motor’s speed. Here, the maximum speed is desired to give the liftarms a firm shove into the basket.
Step 4: The repeat block sets up a loop that repeats six times, then stops. Loops have been used throughout the programs of this book that run indefinitely, but this loop only runs through a specified number of repetitions. Six repetitions are used here, because the Color Sorter holds six liftarms to be sorted.
Step 5: The go to position block sets the pusher motor’s angle to the position specified. This motion retracts the axle connected to the eccentric so that a liftarm drops from the chute onto the Color Sensor.
Step 7: The if then else block presents a condition—it checks if the liftarm seen by the Color Sensor is red. If the red condition is met, then the blocks within the first set of yellow bars will run. Otherwise, the blocks of the else section within the second yellow bars will run.
Step 8: The go to position block sets the basket motor’s angle to the position specified to collect liftarms that correspond to the selected color, red in this case. The basket has two halves, with one dedicated to the selected color and the other half for all other colors.
Step 9: Another go to position block sets the basket motor’s angle to the position specified to collect liftarms that are not the selected color. In other words, any liftarm that is not red will go into the side of the basket that corresponds to this motor position.
Step 10: The next go to position block works with the pusher motor, as opposed to the basket motor of the prior two blocks. This action on the pusher motor shoves the liftarm into the basket.
The Python Code
There are three objects to name in the program: the basket motor given the variable name basket 1, the Color Sensor given the variable name color 2, and the pusher motor given the variable name pusher 3. Loops commonly used in this book kept running indefinitely, but here a loop is set up that only runs six times, using a for loop 4. Six repetitions are used here because the Color Sorter holds six liftarms at the beginning of the program. The sorting routine, described as follows, thus gets run six times.
A set of statements runs inside the repeated loop. These load a liftarm onto the Color Sensor, take a color measurement, and decide to which side of the basket the liftarm should go. To load a liftarm, the pusher is retracted with the run_to_position() function 5. After a pause, the Color Sensor measures the liftarm’s color and assigns the result to a variable named measurement 6. An if conditional selects one of two options depending on the detected color. If measurement matches the value of red, the basket motor goes to a position of 180 degrees using the run_to_position() function 7. The second option, in the else block, occurs if the measurement isn’t red. It presents the other half of the basket. After the basket has been positioned, a motor function shoves the liftarm into the basket.
Summary
This chapter explored the use of the Color Sensor, which can measure the color of an object placed in front of the sensor. In addition to measuring color, the Color Sensor can also detect the brightness of light reflected back into the sensor from a light source built into the sensor. Color Sensor data can be viewed in the Robot Inventor app to give a quick look at measurements. An exercise built a device that beeps a tone on the Hub, with the tone changing depending on the brightness of light reflected back into the sensor. Then a more complex machine was built using color detection that sorts liftarms by their color. This project also involved motors that moved liftarms through the machine to sort them. The program for the Color Sorter involved the use of a loop that runs through a specified number of iterations, rather than running indefinitely.