-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObstacleAvoider.ino
More file actions
80 lines (73 loc) · 1.29 KB
/
ObstacleAvoider.ino
File metadata and controls
80 lines (73 loc) · 1.29 KB
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
const int vcc=A3;
const int trig=A2;
const int echo=A1;
const int gnd=A0;
int pin1=3;
int pin2=5;
int pin3=6;
int pin4=9;
long duration;
int distance;
void setup()
{
pinMode(trig,OUTPUT);
pinMode(vcc,OUTPUT);
pinMode(echo,INPUT);
pinMode(gnd,OUTPUT);
pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);
pinMode(pin3,OUTPUT);
pinMode(pin4,OUTPUT);
digitalWrite(vcc,HIGH);
digitalWrite(gnd,LOW);
Serial.begin(9600);
}
void loop()
{
while(Serial.available()==0)
{
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
duration=pulseIn(echo,HIGH);
distance=duration*0.034/2;
if(distance<15)
{
right();
Serial.print("Too Close!! distance:");Serial.println(distance);
}
else
{
forward();
}
char val=Serial.read();
if(val==' ')
{
Serial.println("Car stopped, Press Reset to Continue");
finish();
}
}
}
void forward()
{
digitalWrite(pin1,HIGH);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
digitalWrite(pin4,HIGH);
}
void right()
{
digitalWrite(pin1,HIGH);
digitalWrite(pin2,LOW);
digitalWrite(pin3,HIGH);
digitalWrite(pin4,LOW);
}
void finish()
{
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
digitalWrite(pin4,LOW);
}