-
Notifications
You must be signed in to change notification settings - Fork 2
/
HCSR04.cpp
62 lines (43 loc) · 1.24 KB
/
HCSR04.cpp
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
/*
HCSR04.cpp - Library for sensor HCSR04.
Thierry VIEIL - March, 2015
*/
#include <Arduino.h>
#include "HCSR04.h"
HCSR04::HCSR04(int Trigger,int Echo){
//We define pins function of the sensor
pinMode(Trigger, OUTPUT);
_Trigger = Trigger;
pinMode(Echo, INPUT);
_Echo = Echo;
}
float HCSR04::ping(int Divider,int shift)
{
// we wait 60 mS to
// prevent the capture
// of an echo due to reflection
// of the ultrasonic wave
//Delay 10 mS x 6
for (int i =0;i<6;i++) {
delayMicroseconds(10000);
}
// long : range from -2,147,483,648 to 2,147,483,647.
long BeginChrono,EndChrono,DeltaChrono =0;
BeginChrono = micros();
digitalWrite(_Trigger, HIGH);
delayMicroseconds(10);
digitalWrite(_Trigger, LOW);
//if Echo is never received we have
//to quit with -1 value
while(digitalRead(_Echo) != true) {
unsigned int test = micros();
if (test - BeginChrono> 60000) {return -1;} }
while(digitalRead(_Echo) == true) { //Echo begin wait until echo response end
EndChrono = micros();
DeltaChrono = EndChrono - BeginChrono;
if (DeltaChrono > 60000) {return -2;}
if (DeltaChrono < 0) {return -3;}
}
DeltaChrono = DeltaChrono-shift;
return float(DeltaChrono)/Divider;
}