Handle echo pulse edges in correct order.

only emit a new distance value if echo pulse edges trigger in the right order. Fixes #1
This commit is contained in:
Michael Mandl 2017-10-15 13:11:46 +02:00 committed by mandlm
parent 94611f2381
commit 4811ab8239
2 changed files with 7 additions and 6 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
build*/
*.user
.*.swp

View file

@ -2,7 +2,7 @@
#include <wiringPi.h>
volatile unsigned int timestampHigh = 0;
volatile unsigned int timestampLow = 0;
volatile unsigned int pulseLength = 0;
DistanceThread::DistanceThread()
{
@ -19,9 +19,10 @@ void DistanceThread::run()
{
timestampHigh = micros();
}
else
else if (timestampHigh != 0)
{
timestampLow = micros();
pulseLength = micros() - timestampHigh;
timestampHigh = 0;
}
});
@ -32,10 +33,9 @@ void DistanceThread::run()
digitalWrite(m_triggerPin, LOW);
delayMicroseconds(40);
unsigned int delayUS = timestampLow - timestampHigh;
if (delayUS < 25e3)
if (pulseLength < 25e3)
{
emit distanceUpdated(delayUS / 0.58);
emit distanceUpdated(pulseLength / 0.58);
}
delay(100);