What I can not get to operate is an external interrupt to trigger on the falling (or rising) edge of the echo pulse. The ISR is never called and that's a bit of a black box to me. I have the statements entered as I understand they need to be to create an interrupt on an external pin but nothing is occurring. Looking to see if there is anything I'm missing as far as how this is supposed to be done.
Defined a few globals just to detect the ISR running:
Code: Select all
#define PIN_SONAR0 13
#define PIN_SONAR1 16
#define PIN_SONAR2 17
Code: Select all
volatile uint sys_son0_isr_count;
volatile uint sys_son1_isr_count;
volatile uint sys_son2_isr_count;
Code: Select all
attachInterrupt(digitalPinToInterrupt(PIN_SONAR0), sonar0_isr, FALLING);
attachInterrupt(digitalPinToInterrupt(PIN_SONAR1), sonar1_isr, FALLING);
attachInterrupt(digitalPinToInterrupt(PIN_SONAR2), sonar2_isr, FALLING);
Code: Select all
void sonar0_isr()
{
sys_son0_isr_count++;
sys_sonar0.update_isr();
}
void sonar1_isr()
{
sys_son1_isr_count++;
//sys_sonar1.update_isr();
}
void sonar2_isr()
{
sys_son2_isr_count++;
//sys_sonar2.update_isr();
}
This is the code from the SRF05 object that triggers the sonar ranging cycle. In this the trigger pin is set to output and the trigger pulse is produced. Then the pin is switched back to input to listen for the start of the echo pulse and note the time that occurs.
Code: Select all
/** @brief Trigger the sonar sensor ranging cycle.
*
* @return: 0 on success, failure otherwise.
*/
inline int SRF05::trigger()
{
uint trigger_time;
uint delays = 200;
// Clear any previous echo time.
m_us_trigger = 0;
m_us_echo = 0;
if (!m_enable)
return SON_NOT_ENA;
// Send a trigger pulse of at least 20usecs
pinMode(m_tpin, OUTPUT);
digitalWrite(m_tpin, HIGH);
delayMicroseconds(20);
digitalWrite(m_tpin, LOW);
pinMode(m_tpin, INPUT);
// Wait for the echo input to show a positive pulse or timeout
while(digitalRead(m_epin) == LOW and delays)
{
delayMicroseconds(10);
delays--;
}
// If the echo line raised then record the trigger time and report
// success. Otherwise report failure.
if (delays)
{
m_us_trigger = micros();
return SON_OK;
}
else
return SON_NO_ECHS;
}
ETA: m_tpin and m_epin can be the same pin number, they are just separated to allow for sensors that have separate trigger and echo lines. In this case they are the same.