Hi, I am trying to write code that would achieve the following:
The arduino is in deep sleep with interrupt 1 from ADXL345 attached to D3. ADXL345 records values in its FIFO buffer until the FIFO buffer's watermark level is reached. At this point the ADXL345 will generate an interrupt that should wake up the Arduino, the Arduino will now burst read values from the FIFO buffer and store them in an array. It will then go back to sleep.
This cycle will keep on repeating.
Here's my code:
#include <Wire.h>
#include <ADXL345.h>
#include <LowPower.h>
ADXL345 adxl;
const byte interruptPin = 3;
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
adxl.powerOn();
adxl.setLowPower(1);
//look of activity movement on this axes - 1 == on; 0 == off
adxl.setActivityX(1);
adxl.setActivityY(1);
adxl.setActivityZ(1);
//setting all interrupts to take place on int pin 1
adxl.setInterruptMapping(ADXL345_INT_DATA_READY_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping(ADXL345_INT_WATERMARK_BIT, ADXL345_INT1_PIN);
//register interrupt actions - 1 == on; 0 == off
adxl.setInterrupt( ADXL345_INT_DATA_READY_BIT, 1 );
adxl.setInterrupt( ADXL345_INT_WATERMARK_BIT, 1);
//setting lowest sampling rate
adxl.setRate(12.5);
//setting device into FIFO mode
adxl.setMode(ADXL345_MODE_FIFO);
//set watermark for Watermark interrupt
adxl.setWatermark(25);
}
void wakeUp()
{
Serial.println("I just woke up...");
}
void loop() {
// put your main code here, to run repeatedly:
adxl.getInterruptSource(); // reading interrupt status flags
Serial.println("Going to sleep...");
attachInterrupt(digitalPinToInterrupt(interruptPin), wakeUp, CHANGE);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
detachInterrupt(digitalPinToInterrupt(interruptPin));
int x[25], y[25], z[25];
byte fifoentries, intEvent;
fifoentries = adxl.getFifoEntries();
intEvent = adxl.getInterruptSource(); // reading interrupt status flags
if (adxl.triggered(intEvent, ADXL345_WATERMARK) ) // if watermark interrupt occured
{
Serial.println("Watermark interrupt triggered. Fetching data now." );
if (fifoentries != 0) {
// burst read will cause the fifo to empty
adxl.burstReadXYZ(&x[0], &y[0], &z[0], fifoentries); // reading all samples of FIFO
for (int i = 0; i < fifoentries; i = i + 5) //Printing only every 5th sample to prevent spam on console
{
Serial.print("FIFO data sample: ");
Serial.print(i);
Serial.print(", x: ");
Serial.print(x[i]);
Serial.print(", y: ");
Serial.print(y[i]);
Serial.print(", z: ");
Serial.println(z[i]);
}
}
}
}
The wakeUp() function is never called with this code, only "Goi?" is printed on the Serial Monitor. I am using the Seeed Studio ADXL345 library and the RocketScream LowPower library for deep sleep. Without the attachInterrupt and the powerDown parts the code works flawlessly with accelerometer values being printed whenever the WATERMARK INTERRUPT is triggered.
Any help would be appreciated. Thanks