Color Detection

Sometimes it is handy to be able to detect various colors, and have your robot make decisions based on the color seen.  Imagine you had a line following course where there is a three way intersection, then imagine that each ‘leg’ of this intersection had a different color box next to it as shown in the following picture.

Your robot when reaching this switch could now determine which is the preferred way by for example detecting ‘red’ and ‘green’ using a color sensor and then decide that only the ‘green’ identified leg of the road is the appropriate path to follow.

More complex examples could be a color sorter, where a robot detects 4 or 5 different colors and takes actions based on the color detected.

In order for your robot program to make these decisions, based on a particular color detected, you can use a switch statement, in a some what more advanced configuration then you maybe used to.

You may know that the switch statement besides using a ‘true’ / ‘false’ logic switch mode — the default method when using one of the sensors directly to switch – can use number or text input on one the data wire connector to switch.  When changing the ‘control’ from “sensor’ to ‘Value’ you are then also able to select one of three modes which drive the switch: Logic (Boolean), Number or Text.  When selecting either Number or Text you can build a list in the conditions box based on the expected input on the data wire.

So lets look at the how we would switch based on the color sensor data.  In this case rather then setting the switch to control = ‘Sensor’ we will set it to Control = ‘Number’ (Or in our below example ‘text’ so that we can also display the value from the sensor on the NXT LCD display).  Then we can build in the Conditions box a switch list. WE know the following about the color sensor, when using the ‘Detected Color’ data port as shown below:

The following Color Values will be transmitted on the Detected Color data port:

1 – Black

2 – Blue

3 – Green

4 – Yellow

5 – Red

6 – White

These Numeric values can then be used to create switch conditions of our switch and allow us to execute different code based on which color we have detected.  So one could imagine that when we saw the red box next to the line, we would turn in the opposite direction, until we detected green. Or if we detected green next to the line, we know that we are turning the right way.  Other examples where we may be able to sue this detection method is in playing RoboCup Soccer – where if we are on the green field we are o.k, and if we detect the red ‘box’ we are in our opponents scoring area, while if we detect ‘yellow’ we are in our own scoring area.  Likewise if we detect white, we have left our field altogether!  Other examples are detecting various types of victims, where say a red color identifies an adult, and a yellow color a baby, while a green color may be a pet.  In each case we can make decisions based on the detected color.

Here is an example of how we can use a combination of a Color Sensor and the Switch statement to execute conditional code based on which color we may have detected.  In this example, we use Control = ‘Text’ in that we take the numeric Detected Color Value from the color sensor, convert it into a Text value so that we can display it on the NXT lcd panel, and then use the text value to create switching conditions.

You can imagine the different ways you can use this example and how you can further evolve it to create color based logic path in your robot code.