This is a simplified version.
#include <Wire.h>
#include <ADXL345.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
ADXL345 adxl;
//WATCHdog interrupt
ISR(WDT_vect)
{
wdt_disable(); //disable watchdog
}
void setup(void)
{
Serial.begin(115200);
pinMode(3, INPUT);
if (!adxl.begin())
{
Serial.println("Something is WRONG!");
delay(1000);
}
else
{
Serial.println("ALL GOOD!");
adxl.setRange(ADXL345_RANGE_16G);
adxl.setDataRate(ADXL345_DATARATE_800HZ);
adxl.setLowPower(0);
//set activity/ inactivity thresholds (0-255)
adxl.setActivityThreshold(20); //62.5mg per increment
//look of activity movement on this axes - 1 == on; 0 == off
adxl.setXActivity(1);
adxl.setXActivity(1);
adxl.setXActivity(1);
attachInterrupt (digitalPinToInterrupt(3), wakeNow, CHANGE);
adxl.getInterruptSource();
//setting all interupts to take place on int pin 1
adxl.setInterruptMapping( ADXL345_ACTIVITY, ADXL345_INT1);
//register interupt actions - 1 == on; 0 == off
adxl.setInterrupt( ADXL345_ACTIVITY, 1);
}
}
void loop(void)
{
sleepNow();
/* DO STUFF */
delay(2000);
}
void sleepNow()
{
Serial.println("Start sleep");
delay(50);
// Do not interrupt before we go to sleep, or the
// ISR will detach interrupts and we won't wake.
noInterrupts ();
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset
WDTCSR = bit (WDCE) | bit (WDE);
// set interrupt mode and an interval
WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0); // set WDIE, and 1 second delay
wdt_reset(); // pat the dog
byte old_ADCSRA = ADCSRA;
ADCSRA = 0;
ADMUX = 0; //turn of internal VREF
// Digital Input Disable Register on analogue pins
DIDR0 = bit (ADC0D) | bit (ADC1D) | bit (ADC2D) | bit (ADC3D) | bit (ADC4D) | bit (ADC5D);
DIDR1 = bit (AIN1D) | bit (AIN0D);
// attachInterrupt (digitalPinToInterrupt(1), wakeNow, CHANGE);
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
EIFR = bit (INTF1);
// turn off brown-out enable in software
// BODS must be set to one and BODSE must be set to zero within four clock cycles
MCUCR = bit (BODS) | bit (BODSE);
// The BODS bit is automatically cleared after three clock cycles
MCUCR = bit (BODS);
// We are guaranteed that the sleep_cpu call will be done
// as the processor executes the next instruction after
// interrupts are turned on.
interrupts (); // one cycle
sleep_cpu (); // one cycle
// cancel sleep as a precaution
sleep_disable();
ADCSRA = old_ADCSRA;
}
void wakeNow()
{
sleep_disable();
}