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).
#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) ?