-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHC_SR04.cpp
81 lines (71 loc) · 1.56 KB
/
HC_SR04.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Driver pour le sonar à ultrasons
* HC_SR04.cpp
*/
#include "Arduino.h"
#include "HC_SR04.h"
// PUBLIC :
// @brief : constructor
HC_SR04::HC_SR04(int _trig, int _echo){
// Initialisation du ECHO
pinMode(_echo,INPUT);
echo = _echo;
// Initialisation du TRIG
pinMode(_trig,OUTPUT);
trig = _trig;
}
// @brief : retourne le pin TRIG
int HC_SR04::getTrig(void){
return trig;
}
// @brief : retourne le pin ECHO
int HC_SR04::getEcho(void){
return echo;
}
// @brief : retourne la valeur de la distance jusqu'à l'objet le plus proche
// 0 si il y a un probleme
unsigned long HC_SR04::getTime(void){
unsigned long time_var;
digitalWrite(trig,HIGH);
delayMicroseconds(15);
digitalWrite(trig,LOW);
time_var = micros();
while(digitalRead(echo) == LOW){
if((micros()-time_var)>50000){
return 0;
}
}
time_var = micros();
while(digitalRead(echo) == HIGH){
if((micros()-time_var)>50000){
return 0;
}
}
time_var = micros() - time_var;
return time_var;
}
// @brief : outil de diagnostique automatisé
// 0 -> OK ; -1 -> not responding ; 1 -> get stuck ; 2 -> value not in range.
int HC_SR04::diagnose(void){
unsigned long time_var;
digitalWrite(trig,HIGH);
delayMicroseconds(15);
digitalWrite(trig,LOW);
time_var = micros();
while(digitalRead(echo) == LOW){
if((micros()-time_var)>50000){
return -1;
}
}
time_var = micros();
while(digitalRead(echo) == HIGH){
if((micros()-time_var)>50000){
return 1;
}
}
time_var = micros() - time_var;
if( (time_var>40000) || (time_var<200) ){
return 2;
}
return 0;
}