Detecting Color
Last updated
Last updated
We've asked our robot to gather a lot of data with the color sensor. Now let's have it use that information to output an actual color name rather than just a value!
Recall when we learned about using if/else
statements while working with the touch sensor.
Let's first set up the skeleton of our if/else statement for determining different colors:
Last we can add our telemetry for our robot to read out information to the Driver Hub based on what it detects:
Each check will be for a certain color that is within the specified range. "Key" can be changed to "Color" on the telemetry blocks. We can add the colors in first:
Next we will add our values for the hue ranges. For example, a color that's hue is between 90-149 should appear as green.
The exact hue values may need to be adjusted slightly, but those used above are based on the default conversion of HSV to RGB when using hue to identify color.
You'll notice that "red" is detected for values under 30 and above 350. This is intentional as red is the beginning and end of the RGB spectrum!
Let's snap our If/Else statement into our loop below the "Alpha" telemetry call.
Save your OpMode and give it a try! You can adjust the values as you need to better reflect the colors available or changes due to lighting in the room.
Once we've added our block we'll click the gear to add the needed "else if" pieces to have enough for all our colors. To each we will add a block from the Logic menu. Next we can add our variable and a block to each side of this logic statement. We want for each if/else our robot to check if the read hue is LESS THAN a set number!