5.6 Red and Blue Sorting Car
5.6.1 Overview
In this project, we build an interesting color block sorting car by the AI vision module. We first need to mount the module on the soccer robot car to enable its recognition function. If a red block is detected, the car will convey it to the left side and return to its original position along the same path. If a blue one is detected, it will convey it to the right and come back.
5.6.2 Mount the AI module to the soccer robot car
Note: Please install the "Football Robot" first according to the robot car tutorial, and then follow the installation steps below.
Required Parts

Step 1

Step 2

Step 3

Step 4

Step 5

Step 6
AI vision module |
Car interface |
|---|---|
T/C (yellow) |
SCL |
R/D (white) |
SDA |
V/+ (red) |
5V |
G/- (black) |
G |

Completed

5.6.3 Code Flow

5.6.4 Test Code
If you want to build the code blocks by yourself, please load the functions of KS5002 robot car. Since the AI vision module needs to interact with this car, its functions will be used. After loading, we can directly drag its code blocks without adding them one by one.

Set the communication mode of the AI vision module to
I2C, and set it to run inColormode. Set the position x, y (default) of the recognition box for color recognition. The minimum width and height are both 20. Set the servo to rotate 160 degrees.

The if block determines the number of detections. Only when the number of detections is greater than 0 will the color block data be output. Note to choose
Color.Add code for stopping the car under the ‘if’ code block

determines whether the detected color block is red.If yes, the car will (1) control the servo to rotate to 180° so that the clip will grab the red block with a delay of 1000ms; (2) turn left for 0.3 seconds, move forward for 0.3s and stop; (3) control the servo to rotate to 160° so that the clip will put the red block down; (4) move back for 0.3s, turn right for 0.3s.
After the car completes these actions, the red block is moved to the left.

determines whether the detected color block is blue. Here we need to modify “red” in it to “blue”.If a blue block is detected, the car will (1) control the servo to rotate to 180° so that the clip will grab the blue block with a delay of 1000ms; (2) turn right for 0.3 seconds, move forward for 0.3s and stop; (3) control the servo to rotate to 160° to put the blue block down; (4) move back for 0.3s, turn left for 0.3s.
After the car completes these actions, the blue block is moved to the right.

Complete code:

5.6.5 Test Result
After uploading the code, the AI vision module will enable the “Color” mode to recognize the captured image to determine red or blue. If a red block is detected, the car will hold the red block and send it to the left side, and then it will return to its original position along the same path. If a blue one is detected, the car will convey it to the right side and come back. (There may be a slight deviation from the original position after the car moves back because it was set based on the car driving time.)
Note: The code sets the minimum size of the color block. If the size of your block is smaller than this, it will not be recognized. These values can be modified in the following code blocks according to your needs.
