-
Notifications
You must be signed in to change notification settings - Fork 5
/
wall_following.ino
118 lines (97 loc) · 2.08 KB
/
wall_following.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#define echoPin_r 38
#define echoPin_f 42
#define echoPin_l 40
#define trigPin 36
int enR=7 ,enL=10,in1L=16,in2L=17,in1R=14,in2R=15;
long duration_f;
long distance_f;
long duration_l;
long distance_l;
long duration_r;
long distance_r;
void setup()
{
Serial.begin(9600);
pinMode(in1L,OUTPUT);
pinMode(in1R,OUTPUT);
pinMode(in2L,OUTPUT);
pinMode(in2R,OUTPUT);
pinMode(enR,OUTPUT);
pinMode(enL,OUTPUT);
//forward();
pinMode(echoPin_f,INPUT);
pinMode(echoPin_l,INPUT);
pinMode(echoPin_r,INPUT);
pinMode(trigPin,OUTPUT);
}
void loop() {
//ultrasonic setup
//flag=1;
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration_l=pulseIn(echoPin_l,HIGH);
distance_l=duration_l/58.2;
Serial.println(distance_l);
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration_r=pulseIn(echoPin_r,HIGH);
distance_r=duration_r/58.2;
Serial.println(distance_r);
if(distance_l<10 && distance_l!=0)
{
//flag=0;
right();
delay(125);
left();
//delay(200);
}
if(distance_r<10 && distance_r!=0 )
{
//flag=0;
left();
delay(125);
right();
//delay(200);
}
if((distance_l>=10 || distance_l==0) && (distance_r>=10 || distance_r==0))
{
forward();
}
}
void right()
{
analogWrite(enR,255);
analogWrite(enL,255);
digitalWrite(in2R, LOW);
digitalWrite(in1R, HIGH);
digitalWrite(in2L, LOW);
digitalWrite(in1L, HIGH);
Serial.println("RIGHT");
//delay(10);
}
void forward()
{
analogWrite(enR,255);
analogWrite(enL,255);
digitalWrite(in1L, LOW);
digitalWrite(in2L, HIGH);
digitalWrite(in2R, LOW);
digitalWrite(in1R, HIGH);
Serial.println("forward2");
}
void left()
{
analogWrite(enR,255);
analogWrite(enL,255);
digitalWrite(in2R, HIGH);
digitalWrite(in1R, LOW);
digitalWrite(in2L, HIGH);
digitalWrite(in1L, LOW);
Serial.println("LEFT");
}