forked from Suthar-Dev/LineFollowingRobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineFollower.ino
94 lines (77 loc) · 2.08 KB
/
LineFollower.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Project-1: //ARDUINO LINE FOLLOWING CAR - QUAD Robotics - A unit of Quad Store//
//www.quadstore.in
// YOU HAVE TO INSTALL THE AFMOTOR LIBRARY BEFORE UPLOAD THE CODE//
// GO TO SKETCH >> INCLUDE LIBRARY >> ADD .ZIP LIBRARY >> SELECT AF MOTOR ZIP FILE //
//including the libraries
#include <AFMotor.h>
//defining pins and variables
#define lefts A0
#define rights A2
//defining motors
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
void setup() {
//Setting the motor speed
motor1.setSpeed(180);
motor2.setSpeed(180);
motor3.setSpeed(180);
motor4.setSpeed(180);
//Declaring PIN input types
pinMode(lefts,INPUT);
pinMode(rights,INPUT);
//Begin serial communication
Serial.begin(9600);
}
void loop(){
//Printing values of the sensors to the serial monitor
Serial.println(digitalRead(lefts));
Serial.println(digitalRead(rights));
//line detected by both
if(digitalRead(lefts)==1 && digitalRead(rights)==1){
//Forward
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
//line detected by left sensor
else if(digitalRead(lefts)==0 && digitalRead(rights)==1){
//turn left
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
//line detected by right sensor
else if(digitalRead(lefts)==1 && digitalRead(rights)==0){
//turn right
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
//line detected by none
else if(digitalRead(lefts)==0 && digitalRead(rights)==0){
//stop
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
/*
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
*/
}