Skip to content

Commit 7f0c992

Browse files
authored
Merge pull request #3 from NdK73/master
pulseIn works better in noInterrupts() thank to NdK73
2 parents c7a594c + 97f360f commit 7f0c992

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/HCSR04.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ float HCSR04::dist(int n) const
2424
digitalWrite(this->out, HIGH);
2525
delayMicroseconds(10);
2626
digitalWrite(this->out, LOW);
27-
return pulseIn(this->echo[n], HIGH) / 58.0;
27+
noInterrupts();
28+
float d=pulseIn(this->echo[n], HIGH);
29+
interrupts();
30+
return d / 58.0;
2831
}
2932
float HCSR04::dist() const{return this->dist(0);}

src/HCSR04.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class HCSR04
66
HCSR04(int out,int echo[],int n); //initialisation class HCSR04 (trig pin , echo pin)
77
~HCSR04(); //destructor
88
float dist() const; //return curent distance of element 0
9-
float dist(int n) const; //return curent distance of element 0
9+
float dist(int n) const; //return curent distance of element n
1010

1111
private:
1212
void init(int out,int echo[],int n); //for constructor

0 commit comments

Comments
 (0)