The following code works with the busy wait, but if I replace the while loop with a call to wiringPiISR() it always returns 0 for the distance. I also tried calling wiringPiISR() before raising the trigger pin thinking that the echo pin might have already been raised HIGH before the callback was registered to handle it, but that didn't make a difference. Any idea what might be the problem?
#include <iostream>
#include <wiringPi.h>
#define GPIO22 3
#define GPIO23 4
using namespace std;
static int trigger = GPIO22;
static int echo = GPIO23;
static volatile long startTimeUsec;
static volatile long endTimeUsec;
void recordPulseLength (void) {
startTimeUsec = micros();
while ( digitalRead(echo) == HIGH );
endTimeUsec = micros();
}
/**
* Uses the HC SR04 ultrasonic sensor to measure distance. The HC SR04
* provides 2cm to 400cm range measurement.
*/
int main()
{
double speedOfSoundMetersPerSecond = 340.29;
wiringPiSetup();
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
// Initialize the sensor's trigger pin to low. If we don't pause
// after setting it to low, sometimes the sensor doesn't work right.
digitalWrite(trigger, LOW);
delay(500); // .5 seconds
// Triggering the sensor for 10 microseconds will cause it to send out
// 8 ultrasonic (40Khz) bursts and listen for the echos.
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
// The sensor will raise the echo pin high for the length of time that it took
// the ultrasonic bursts to travel round trip.
// Doesn't work; endTimeUsec and startTimeUsec are always the same.
//wiringPiISR(echo, INT_EDGE_RISING, &recordPulseLength);
while ( digitalRead(echo) == LOW);
recordPulseLength();
long travelTimeUsec = endTimeUsec - startTimeUsec;
double distanceMeters = ((travelTimeUsec/1000000.0)*speedOfSoundMetersPerSecond)/2;
cout << "Distance is " << distanceMeters*100 << " cm." << endl;
return 0;
}