-
Notifications
You must be signed in to change notification settings - Fork 0
/
obstacle_avoidance_sketch.ino
97 lines (78 loc) · 2.1 KB
/
obstacle_avoidance_sketch.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
#define ledPin 8 //this is the led pin
#define ledPin2 9
#define ledPin3 10
class Sonar{
public:
int triggerPin;
int echoPin;
long distance, duration;
void sonar(int trig, int echo){ //custom initializer
triggerPin = trig;
echoPin = echo;
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}
};
class Testing{
public:
static bool isObstaclePresent(Sonar s){ //checking an obstacle is within less than or equal five centimeters
digitalWrite(s.triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(s.triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(s.triggerPin, LOW);
s.duration = pulseIn(s.echoPin, HIGH);
s.duration /= 29; //for converting miliseconds to centimeters
s.duration /= 2; //for getting the actual distance
s.distance = s.duration;
if(s.distance <= 5){ //checking if the distance is less or equal five
Serial.print("this is for sonar: ");
Serial.println(s.distance);
return true;
}
else{
Serial.print("this is for sonar: ");
Serial.println(s.distance);
return false;
}
}
};
Sonar sonar1;
Sonar sonar2;
Sonar sonar3;
void setup() {
// put your setup code here, to run once:
sonar1.sonar(2, 3); // sonar(trig, eho);
sonar2.sonar(4, 5);
sonar3.sonar(6, 7);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if((Testing::isObstaclePresent(sonar1) == true) ){ //checking the obstacle for sonar1
pinMode(ledPin,OUTPUT);
digitalWrite(ledPin,HIGH);
delay(40);
// Serial.println(sonar1.distance);
//Serial.println(sonar2.distance);
// Serial.println(sonar3.distance);
//delay(10);
}
else digitalWrite(ledPin,LOW);
if((Testing::isObstaclePresent(sonar2) == true)){
pinMode(ledPin2,OUTPUT);
digitalWrite(ledPin2,HIGH);
delay(40);
}
else digitalWrite(ledPin2,LOW);
if((Testing::isObstaclePresent(sonar3) == true)){
pinMode(ledPin3,OUTPUT);
digitalWrite(ledPin3,HIGH);
delay(40); //delay after every checking
// Serial.println(sonar1.distance);
//Serial.println(sonar2.distance);
// Serial.println(sonar3.distance);
// delay(10);
}
else digitalWrite(ledPin3,LOW);
}