External interrupts on ArduFlyer won't work

Hello,
I’m trying to configure an external interrupt on an ArduFlyer, but can’t get it to work.

  • INT0 on Arduino pin 2 (corresponding to digital input 7 on the ArduFlyer board)
  • When I set input 7 to 0V, the interrupt service routine would change the value of a parameter from 0 to 10; and I would see that on the monitor.

I’ve tried everything I could and am really running out of idea.
Any idea why the isr_2 in the code below seems to never be called ?

Many thanks in advance.

Here is my code :

#include <AP_Common.h>
#include <AP_Math.h>
#include <AP_Param.h>
#include <AP_Progmem.h>
#include <AP_ADC.h>
#include <AP_InertialSensor.h>

#include <AP_HAL.h>
#include <AP_HAL_AVR.h>


#define HIGH 1
#define LOW  0
#define INT_NUM0   0

#define HAL_GPIO_INPUT  0
#define HAL_GPIO_OUTPUT 1
#define HAL_GPIO_ALT    2
#define HAL_GPIO_INTERRUPT_LOW 0
#define HAL_GPIO_INTERRUPT_HIGH 1
#define HAL_GPIO_INTERRUPT_FALLING 2
#define HAL_GPIO_INTERRUPT_RISING 3




// Arduino Mega 
#define digitalPinToInterrupt(p)  ( (p) == 2 ? 0 : ((p) == 3 ? 1 : ((p) >= 18 && (p) <= 21 ? 23 - (p) : -1)) ) 

const AP_HAL::HAL& hal = AP_HAL_AVR_APM2;  // Hardware abstraction layer



// Equivalence between Dig I/O Pin APM --> Arduino Pin pin number in the sotware
// Example : Pin 7 APM ==> number 2
const int apm_digio[9] = {12, 12, 11, 8, 7, 6, 3, 2, 5};

volatile int test_var = 0;

void isr_0 () {
  test_var = 10;
}
void isr_1 () {
  test_var = 11;
}
void isr_2 () {
  test_var = 12;
}
void isr_3 () {
  test_var = 13;
}
void isr_4 () {
  test_var = 14;
}
void isr_5 () {
  test_var = 15;
}

void setup() 
{
 for (int i=1;i<9;i++) {
     hal.gpio->pinMode (apm_digio[i], GPIO_INPUT);  
     hal.gpio->write(apm_digio[i], HIGH);  
   }
   
   hal.gpio->pinMode(LED, GPIO_OUTPUT);

   hal.gpio->attach_interrupt(0, isr_2, HAL_GPIO_INTERRUPT_FALLING);  // attach interrupt handler

}

void loop() 
{
 int Dig[12], i;
 

 
 for (i=1;i<9; i++) 
   Dig[i] = hal.gpio->read(apm_digio[i]);
  
 hal.console->printf_P(
           PSTR("Digital I/Os %d %d %d %d %d %d %d %d   Interruption: %d\n"),
           Dig[1], Dig[2], Dig[3], Dig[4], Dig[5], Dig[6], Dig[7], Dig[8], test_var);
 
 hal.scheduler->delay(1000);  //Wait 1000ms 
}

AP_HAL_MAIN();    // special macro that replace's one of Arduino's to setup the code (e.g. ensure loop() is called in a loop).

I'm trying to configure an external interrupt on an ArduFlyer

What is an ArduFlyer?

volatile int test_var = 0;

Maybe this needs to be a long long, to hold values up to 15.

 hal.scheduler->delay(1000);  //Wait 1000ms

Really? I'd have never guessed.

Array indices start at 0. Why are you ignoring the first element in all arrays?

ArduFlyer has a APM2.5 processor (Arduino Mega2560).
I don't need all indices in my example, just 1-8.

I would think an int variable would hold values from -16383 to 16384 ?

I don't need all indices in my example, just 1-8.

If you need 8 values in the array then it would be more normal to use index values of 0 to 7

I would think an int variable would hold values from -16383 to 16384 ?

That was the point that Paul was making. The maximum value of test_var appears to be 15, in which case it could be declared as a byte rather than an int.

I would think an int variable would hold values from -16383 to 16384 ?

You could guess again, or you could look at the Reference page, and see what the real limits for all the types are.