-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHC_SR04.cpp
42 lines (35 loc) · 931 Bytes
/
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
#include "HC_SR04.h"
//HC_SR04 *HC_SR04::_instance=NULL;
HC_SR04 *HC_SR04::_instance(NULL);
HC_SR04::HC_SR04(int trigger, int echo, int interrupt, int max_dist)
: _trigger(trigger), _echo(echo), _int(interrupt), _max(max_dist), _finished(false)
{
if(_instance==0) _instance=this;
}
void HC_SR04::begin(){
pinMode(_trigger, OUTPUT);
digitalWrite(_trigger, LOW);
pinMode(_echo, INPUT);
attachInterrupt(_int, _echo_isr, CHANGE);
}
void HC_SR04::start(){
_finished=false;
digitalWrite(_trigger, HIGH);
delayMicroseconds(10);
digitalWrite(_trigger, LOW);
}
unsigned int HC_SR04::getRange(bool units){
return (_end-_start)/((units)?58:148);
}
void HC_SR04::_echo_isr(){
HC_SR04* _this=HC_SR04::instance();
switch(digitalRead(_this->_echo)){
case HIGH:
_this->_start=micros();
break;
case LOW:
_this->_end=micros();
_this->_finished=true;
break;
}
}