Arduino + accelerometer interrupt based on activity

Hi,
I'm trying to use a duemilanove and a ADXL345 accelerometer for a tracking project i'm working.
The arduino needs to be sleeping and only waking up on an external interrupt. This I've managed using the LowPower library, without the adxl345 it works fine, i mean, if I tie pin2 (interrupt 0 ) to 5V it wakes the arduino and goes sleep after.
Merging this lowpower library with the adxl345 is where the problems start.
I can put the arduino to sleep and wake up after adxl senses motion but then its int1 pin (the interrupt pin on adxl) never goes to low again. Noticed that if I tap the adxl accel very careful it does make interrupt go high and then low (as it's supposed) but if I move it fast then it goes high and never goes low (this doesnt make any sense to me).

link to ADXL345 library

#include "LowPower.h"
#include <Wire.h>
#include <ADXL345.h>

ADXL345 adxl; //variable adxl is an instance of the ADXL345 library
boolean state = false;

void setup() {
  Serial.begin(115200);
  pinMode(2, INPUT);

  adxl.powerOn();
  //set activity/ inactivity thresholds (0-255)
  adxl.setActivityThreshold(4); //62.5mg per increment

  //look of activity movement on this axes - 1 == on; 0 == off
  adxl.setActivityX(1);
  adxl.setActivityY(1);
  adxl.setActivityZ(1);

  //setting all interupts to take place on int pin 1
  adxl.setInterruptMapping(ADXL345_INT_ACTIVITY_BIT, ADXL345_INT1_PIN);

  adxl.setInterruptLevelBit(0);
  adxl.printAllRegister();
}

void loop() {
  if (state)
  {
    Serial.println("WAKING UP");
    Serial.flush();
    state = false;
  }


  delay(100);
  adxl.getInterruptSource();
  attachInterrupt(0, interrupt0, RISING);
  adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT,   1);
  Serial.println("Ill go to sleep now...");
  Serial.flush();
  LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);

  detachInterrupt(0);

  //register interupt actions - 1 == on; 0 == off
  adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT,   0);
}


void interrupt0()
{
  state = true;
}

But if I use the sketch below (putting arduino to sleep but without low power library) it works good, and I can wake up arduino when adxl interrupt pin goes HIGH and then it somehow resets the adxl pin to go low again until it senses motion again.

#include <Wire.h>
#include <ADXL345.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>

ADXL345 adxl; //variable adxl is an instance of the ADXL345 library
boolean state = false;
void setup() {
  Serial.begin(115200);
  pinMode(13, OUTPUT);
  pinMode(2, INPUT);      // Make digital 2 an input
  adxl.powerOn();
  //setting all interupts to take place on int pin 1
  //set activity/ activity thresholds (0-255)
  adxl.setActivityThreshold(4); //62.5mg per increment

  //look of activity movement on this axes - 1 == on; 0 == off
  adxl.setActivityX(1);
  adxl.setActivityY(1);
  adxl.setActivityZ(1);
  adxl.setInterruptMapping( ADXL345_INT_ACTIVITY_BIT, ADXL345_INT1_PIN );
  adxl.setInterruptLevelBit(0);
  adxl.printAllRegister();

}


void wakeUp()
{
state = true;
}

void loop() {

    if (state)
  {
    Serial.println("WAKING UP");
    Serial.flush();
    state=false;
  }
  
  delay(100);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
  sleep_enable();
  adxl.getInterruptSource();
  attachInterrupt(0, wakeUp, RISING);
  adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT,   1);
  sleep_mode();
  sleep_disable();         // first thing after waking from sleep:
  detachInterrupt(0);

  //register interupt actions - 1 == on; 0 == off
  adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT,   0);
}

I've put a multimeter in order to watch the state of the adxl interrupt pin and can clearly see that on the first sketch it after it senses motion for the first time, it stays at 3.3v and never goes back to 0 again, which on the second sketch it does go back to 0 after sensing the interrupt.

for sure i'm missing something on the sketch using lowpower lib. but what is missing in order to make the adxl345 reset its interrupt pin to low (like it's doing on the second sketch) ?

First there are a lot of differences in what you are doing the the sensor after you awake between the two sketches - start looking for the problem there.

Second you use attachInterrupt only in setup you should never do the attach/detach thing.

Third your program is arse about face.

It should look more like this

void loop(){
      put arduino to sleep


      // arduino wakes up here!
      do the things you want when it wakes up

}

Ditch "state" it's not needed and just leave the ISP empty

Mark

Thanks for your time.
are there ? Because i've put some debug prints and they show exactly in the same order on the two sketches.
So I dont need to detach / attach ? I've read that in order for the interrupt handler to not be triggered while still the input is high, i would need to detach. I'll remove it then, thanks.

Your sketch seems fine if i dont need to do anything before putting the arduino to sleep, but in my case I do need.
You're saying that there's no need for the state boolean ? Then how would i know if the arduino woke from that specific interrupt ? (the sketch i provided is just a basic sketch to show the problem, this is part of a more complicated sketch, thus the need for knowing if the arduino woke from that specific interrupt)