for a little background, I am new to Arduino but not to programming or microcontrollers. I’ve been working on embedded products for many years and thought I would take a look at Arduino out of curiosity.
With that out of the way, my question has to do with the Qduino mini and interrupts. I have some test code posted below that connects an IR phototransistor to D3 and measures the time of a positive pulse and then sends the time (in milliseconds) and a running pulse counter to the serial port. The signal input is verified on my oscilloscope to be nice and clean without any bounce.
The posted code works. The problem is, I don’t understand why it doesn’t work for the remaining INT pins for the Qduino(INT1, INT2 or INT3). The other pins trigger the ISR on any edge, multiple times. I should only get a printout when single positive pulse completes LOW to HIGH and back to LOW. With INT1, 2 and 3 the printout occurs on the rising edge, the falling edge and the pulse counter will jump many counts instead of incrementally.
Does anyone know why?
#include "Arduino.h"
#include "Wire.h"
#include "Qduino.h"
qduino q; // initialize the library
//IR detector pin on INT0, only interrupt that works correctly to detect edges
const int IR_pin = 3; //3 works
//problems using INT1, INT2 and INT3
// Variables will change:
volatile int shots = 0;
volatile unsigned long time_of_shot = 0;
volatile unsigned long timing_start = 0;
volatile int update_display = 1;
void setup()
{
q.setup();
pinMode(IR_pin, INPUT_PULLUP);
pinMode(13, OUTPUT); // Blue user LED for heart beat
pinMode(10, OUTPUT); // Red user LED for debug
digitalWrite(10, HIGH); //off
attachInterrupt(digitalPinToInterrupt(IR_pin), rising_isr, RISING);
Serial.begin(9600);
while(!Serial); // wait for Serial port to be opened
Serial.println("interrupt demo");
}
//////////////////////////////////////////////////////////////////////////////////
void rising_isr(){
//disable interrupts before reinitializing ints
detachInterrupt(digitalPinToInterrupt(IR_pin));
timing_start = millis();
attachInterrupt(digitalPinToInterrupt(IR_pin), falling_isr, FALLING);
}
void falling_isr(){
detachInterrupt(digitalPinToInterrupt(IR_pin));
time_of_shot = millis() - timing_start;
shots++;
update_display = 1;
attachInterrupt(digitalPinToInterrupt(IR_pin), rising_isr, RISING);
}
///////////////////////////////////////////////////////////////
void loop()
{
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
///////////////////////////////////////////////////////
if (update_display){
update_display = 0;
Serial.print("shots =");
Serial.println(shots);
Serial.println(time_of_shot);
}
}
Any insight is appreciated.
Thanks, Dan