-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobstacle_detection.ino
100 lines (84 loc) · 2.05 KB
/
obstacle_detection.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
/**
* Name: Andra Alazaroaie
*/
#define MAX 150
// sensor
int trigPin = PA9;
int echoPin = PB8;
// motors
int a1 = PB3;
int a2 = PA8;
int b1 = PB15;
int b2 = PB14;
unsigned long lastTrig = millis();
unsigned long trigDelay = 100;
volatile unsigned long startTime;
volatile unsigned long endTime;
volatile bool newDist = false;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(a1, OUTPUT);
pinMode(a2, OUTPUT);
pinMode(b1, OUTPUT);
pinMode(b2, OUTPUT);
// the pin on which we want the interrupt, the ISR, interrupt when there is a CHANGE on the pin
attachInterrupt(digitalPinToInterrupt(echoPin), echoPinInterrupt, CHANGE);
goFront();
}
void loop() {
// trigger only every trigDelay millis
unsigned long currentTime = millis();
if(currentTime - lastTrig > trigDelay) {
lastTrig += trigDelay;
trig();
}
// new distance detected by echo
if(newDist) {
newDist = false;
double dist = getDistance();
stopWhenClose(dist);
}
}
// interrupt service routine
void echoPinInterrupt() {
if(digitalRead(echoPin) == HIGH)
startTime = micros();
else {
endTime = micros();
newDist = true;
}
}
// distance = speed * time formula, we use the speed of sound
double getDistance() {
double time = endTime - startTime;
return time / 58.0;
}
// trigger function for the ultrasound, transmit a short ultrasonic pulse for the echo to listen to
void trig() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
}
// function to stop the motors when we detect a distance smaller than 15
void stopWhenClose(double distance) {
if(distance < 15.0) {
stop();
delay(10000);
} else
goFront();
}
void goFront() {
analogWrite(a1, 0);
analogWrite(a2, 185);
analogWrite(b1, 0);
analogWrite(b2, MAX);
}
void stop() {
analogWrite(a1, 0);
analogWrite(a2, 0);
analogWrite(b1, 0);
analogWrite(b2, 0);
}