SunFounder 3in1 Kit
2. Read the value of the Line Tracking module, if it is 1, then let the car go forward to the left; otherwise go forward
to the right. Also you can open the serial monitor by clicking the magnifying glass icon in the upper right corner
to see the change of the Line Tracking module value on the black and white line before unplugging the USB
cable.
void loop() {
int speed = 150;
int lineColor = digitalRead(lineTrack); // 0:white 1:black
Serial.println(lineColor);
if (lineColor) {
moveLeft(speed);
} else {
moveRight(speed);
}
}
3. About the moveLeft() and moveRight() functions.
Unlike the left-right turn function in project 2. Move by Code, only small left-right turns are needed
here, so you only need to adjust the value of A_1A or B_1B each time. For example, if you move to
the left front (moveLeft()), you only need to set the speed to A_1A and all others to 0, it will make
the right motor turn clockwise and the left motor not move.
void moveLeft(int speed) {
analogWrite(A_1B, 0);
analogWrite(A_1A, speed);
analogWrite(B_1B, 0);
analogWrite(B_1A, 0);
}
void moveRight(int speed) {
analogWrite(A_1B, 0);
analogWrite(A_1A, 0);
analogWrite(B_1B, speed);
analogWrite(B_1A, 0);
}
• Serial
Used for communication between the Arduino board and a computer or other devices
– Serial.begin(): Sets the data rate in bits per second (baud) for serial data transmission.
– Serial.println(): Prints data to the serial port as human-readable ASCII text followed by a
car return character (ASCII 13, or ‘r’) and a newline character (ASCII 10, or ‘n’).
• if else
The if else allows greater control over the flow of code than the basic if statement, by allowing
multiple tests to be grouped.
5.5. 4. Follow the line 243