-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot_v1.ino
63 lines (48 loc) · 1.07 KB
/
robot_v1.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
#include <Servo.h> //servo library
Servo myservo;
//for distance detection
int Echo = A4, Trig = A5;
String left = "left", right = "right", center = "center";
bool obstruction = false;
//motor control
int in1=6, in2=7, in3=8, in4=9;
// channel output pins
int ENA=5, ENB=11;
//distance parameters
int rightDistance, leftDistance, middleDistance;
void setup()
{
myservo.attach(3);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
}
void loop()
{
look(center);
if (obstruction == false){
smartForward();
}
else {
look(left,200);
leftDistance = checkDistance();
look(right,200);
rightDistance = checkDistance();
look(center);
middleDistance = checkDistance();
if (leftDistance > rightDistance){
rotateLeftFor(200);
smartForward();
}
else if (rightDistance > leftDistance){
rotateRightFor(200);
smartForward();
}
}
}