Interrupts with Arduino Pro Mini

Hy there

I'm currently writing a program to read some motion sensors, or I'm mainly trying it. I would use the interrupt, but I didn't manage to make it working.

Here an extract of my code:

#define BMA_INT 0                     // INT0 on digital pin 2
#define ITG_INT 1                     // INT1 on digital pin 3

void setup() {
  Serial.begin(9600);                 // create the serial connection at 9600bit/s baudrate
  Wire.begin();                       // initilize I2C communication with the Arduino as master

  Serial.println("Hello Arduino");

  attachInterrupt(BMA_INT, bmaISR, RISING);         // configure interrupt for the BMA on rising edge
  attachInterrupt(ITG_INT, itgISR, RISING);         // configure interrupt for the ITG

  Serial.println("Setup successfully");
}        // end setup-function

void bmaISR(void) {
  xAcc = readY_BMA();                 // read the three accelerations from the sensor, over I2C
  yAcc = -readX_BMA();                // the axis of the BMA have to be adapted to the axis of the ITG !!!
  zAcc = readZ_BMA();
}        // end BMA ISR

void itgISR(void) {
  xRate = readX_ITG();                // read the three angular rates from the gyro
  yRate = readY_ITG();
  zRate = readZ_ITG();
}        // end ITG ISR

I'm using a Arduino Pro Mini (3.3V, 8MHz) / ATmega328. Is there anyone with some experience with external interrupts? Thank a lot for your answers!

An interrupt service routine needs to do something really quickly and get out again. Your service routines are doing some things which take quite a bit of time. Try changing your code to do something like this:

volatile int read_BMA = 0;
volatile int read_ITG = 0;
void loop()
{
  if(read_BMA) {
    xAcc = readY_BMA();                 // read the three accelerations from the sensor, over I2C
    yAcc = -readX_BMA();                // the axis of the BMA have to be adapted to the axis of the ITG !!!
    zAcc = readZ_BMA();
    read_BMA = 0;
  }
  if(read_ITG) {
    xRate = readX_ITG();                // read the three angular rates from the gyro
    yRate = readY_ITG();
    zRate = readZ_ITG();
    read_ITG = 0;
  } 
}

void bmaISR(void) {
  read_BMA = 1;
}        // end BMA ISR

void itgISR(void) {
  read_ITG = 1;
}        // end ITG ISR

The service routines only set a flag. The loop() does nothing until one of the flags is set and then reads the required info.

Pete

Hi Buchc3

El supremo got in there while I was writing this, and he's right.

Why are you trying to use interrupts? and what is going to pull your pin high to trigger it? (I prefer a pull up resistor and triggering on the falling when it gets pulled low)

Interrupt processing should be as minimal as possible, increment a counter or set a flag so your main loop knows that the interrupt was triggered and then call a routine to handle the event. Calling spi/i2c routines is definitely discouraged if not out right impossible since other processor functions are suspended during the call to the interrupt handler.

Interrupts are typically used with motion sensors only for predefined triggers that the sensor can generate such as free-fall detection or exceeding some limit. Regular polling of the sensors should be handled in or by a procedure called from the main loop.