EasyManua.ls Logo

SunFounder 3in1 Kit - Page 251

Default Icon
351 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder 3in1 Kit
(continued from previous page)
void setup() {
...
//IR obstacle
pinMode(leftIR, INPUT);
pinMode(rightIR, INPUT);
}
2. Read the values of the left and right infrared modules and let the car to make the corresponding action.
void loop() {
int left = digitalRead(leftIR); // 0: Obstructed 1: Empty
int right = digitalRead(rightIR);
int speed = 150;
if (!left && right) {
backLeft(speed);
} else if (left && !right) {
backRight(speed);
} else if (!left && !right) {
moveBackward(speed);
} else {
moveForward(speed);
}
}
If the left IR module is 0 (obstacle detected) and the right IR module is 1, let the car back up to
the left.
If the right IR module is 0 (obstacle detected), let the car go back up to the right.
If 2 IR modules detect the obstacle at the same time, the car will go backward.
Otherwise the car will keep going forward.
3. About the backLeft() function.
When the right motor is turning counterclockwise and the left motor is not turning, the car will go
backward to the left.
void backLeft(int speed) {
analogWrite(A_1B, speed);
analogWrite(A_1A, 0);
analogWrite(B_1B, 0);
analogWrite(B_1A, 0);
}
4. About the backLeft() function.
When the left motor is turning clockwise and the right motor is not turning, the car will go backward
to the right.
void backRight(int speed) {
analogWrite(A_1B, 0);
(continues on next page)
5.6. 5. Play with Obstacle Avoidance Module 247

Related product manuals