-
Notifications
You must be signed in to change notification settings - Fork 0
/
Black_line_follower_Test_1.ino
71 lines (62 loc) · 1.76 KB
/
Black_line_follower_Test_1.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Import The redbot h file
#include <RedBot.h>
// Define a macro for speed to simplify changing
// the speed of the robot wihin the code
#define SPEED 120
// Define a macro for the value of the proximity sensor reading when
// it detects a white trace
#define WHITE 10
// intiate IR Sensors
RedBotSensor Right_IR = RedBotSensor(A6);
RedBotSensor Left_IR = RedBotSensor(A7);
// intiate Motors
RedBotMotors motors;
void setup() {
}
void loop() {
// Check the reading of the 2 proximity sensors
// if both read black road.
// Move forward
if(Right_IR.read() > WHITE && Left_IR.read() > WHITE)
{
forward();
}
// if the right sensor detects white trace and left sensor
// detects black road.
// Turn left
else if(Right_IR.read() < WHITE && Left_IR.read() > WHITE)
{
rotate_left();
}
// if the right sensor detects black road and left sensor
// detects white trace.
// Turn right
else if (Right_IR.read() > WHITE && Left_IR.read() < WHITE)
{
rotate_right();
}
// if both sensors detect white trace it means that the robot is
// outside of the road.
// Stop the robot
else if(Right_IR.read() < WHITE && Left_IR.read() < WHITE)
{
motors.stop();
}
}
// This function moves the robot a step forward then stops
void forward(){
motors.rightMotor(SPEED);
motors.leftMotor(-SPEED);
delay(50);
motors.stop();
}
// This function uses the pivot command from the redbot library to
// rotate the robot to the left
void rotate_left(){
motors.pivot(-SPEED);
}
// This function uses the pivot command from the redbot library to
// rotate the robot to the right
void rotate_right(){
motors.pivot(SPEED);
}