Here comes some code:
#define F_CPU 16000000
#define ARDUINO 100
#define DEVICE (0x1D) // ADXL345 I2C device address
#define TO_READ (6) // num of bytes we are going to read each time (two bytes for each axis)
#define pbIN 0 // Arduino pin which is connected to INT1 from the ADXL345
#define ledOut 8
#include "Arduino.h"
#include "Wire.h"
#include "Wire.cpp"
#include "twi.h"
#include "twi.c"
#include "binary_const.h" // with this we can use something B8(01010101) that it will convert to 85 at compile time
#include <avr/power.h>
#include <avr/sleep.h>
#define R_DEVID 0
#define R_THRESH_TAP 29
#define R_OFSX 30
#define R_OFSY 31
#define R_OFSZ 32
#define R_DUR 33
#define R_LATENT 34
#define R_WINDOW 35
#define R_THRESH_ACT 36
#define R_THRESH_INACT 37
#define R_TIME_INACT 38
#define R_ACT_INACT_CTL 39
#define R_THRESH_FF 40
#define R_TIME_FF 41
#define R_TAP_AXES 42
#define R_ACT_TAP_STATUS 43
#define R_BW_RATE 44
#define R_POWER_CTL 45
#define R_INT_ENABLE 46
#define R_INT_MAP 47
#define R_INT_SOURCE 48
#define R_DATA_FORMAT 49
#define R_DATAX0 50
#define R_DATAX1 51
#define R_DATAY0 52
#define R_DATAY1 53
#define R_DATAZ0 54
#define R_DATAZ1 55
#define R_FIFO_CTL 56
#define R_FIFO_STATUS 57
byte buff[TO_READ]; //6 bytes buffer for saving data read from the device
char str[40]; //string buffer to transform data before sending it to the serial port
boolean inspected = 0;
volatile int state = 1; // The input state toggle
// Declarations
void writeTo(int device, byte address, byte val);
void readFrom(int device, byte address, int num, byte buff[]);
byte readByte(int device, byte address);
void stateChange();
// Arduino compliance
void setup();
void loop();
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(57600); // start serial for output
// ----------------------------------------------------------------------------
// Low power - 45 uA, 50 Hz
//writeTo(DEVICE, R_BW_RATE, B8(00011001));
// Low power - 34 uA, 12.5 Hz
writeTo(DEVICE, R_BW_RATE, B8(00010111));
// interrupts setup; DATA_READY, Activity, Inactivity
writeTo(DEVICE, R_INT_MAP, B8(01100111)); // send all interrupts to ADXL345's INT1 pin
writeTo(DEVICE, R_INT_ENABLE, B8(10011000)); // enable DATA_READY, Activity, and Inactivity
// inactivity configuration
writeTo(DEVICE, R_TIME_INACT, 10); // 1s / LSB
writeTo(DEVICE, R_THRESH_INACT, 3); // 62.5mg / LSB
// also working good with high movements: R_TIME_INACT=5, R_THRESH_INACT=16, R_ACT_INACT_CTL=B8(00000111)
// but unusable for a quite slow movements
// activity configuration
writeTo(DEVICE, R_THRESH_ACT, 8); // 62.5mg / LSB
// activity and inactivity control
writeTo(DEVICE, R_ACT_INACT_CTL, B8(11111111)); // enable activity and inactivity detection on x,y,z using ac
// Bypass mode, INT1 enabled (later INT2 maybe!!??)
writeTo(DEVICE, R_FIFO_CTL, B8(00000000));
// DATA_FORMAT, 16G, Full resolution
writeTo(DEVICE, R_DATA_FORMAT, B8(00001011));
// Enable measurement mode; +++++ LAST STEP +++++
// set the ADXL345 in measurement and sleep Mode: this will save power while while we will still be able to detect activity
// set the Link bit to 1 so that the activity and inactivity functions aren't concurrent but alternatively activated
// set the AUTO_SLEEP bit to 1 so that the device automatically goes to sleep when it detects inactivity
writeTo(DEVICE, R_POWER_CTL, B8(00111100));
// -----------------------------------------------------------------------------------
pinMode(ledOut, OUTPUT);
attachInterrupt(pbIN, stateChange, LOW);
}
void loop()
{
Serial.print(state,DEC);
delay(100);
//sleepNow();
}
// Definitions
void stateChange()
{
state = 0;
digitalWrite(ledOut, HIGH); // Test if it triggers, if Serial might fool me!!
}
// ---------------- Functions ------------------
//Writes val to address register on device
void writeTo(int device, byte address, byte val) {
Wire.beginTransmission(device); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}
//reads num bytes starting from address register on device in to buff array
void readFrom(int device, byte address, int num, byte buff[]) {
Wire.beginTransmission(device); // start transmission to device
Wire.write(address); // sends address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(device); // start transmission to device
Wire.requestFrom(device, num); // request 6 bytes from device
int i = 0;
while(Wire.available()) // device may send less than requested (abnormal)
{
buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); // end transmission
}
// read a single bite and returns the readed value
byte readByte(int device, byte address) {
Wire.beginTransmission(device); // start transmission to device
Wire.write(address); // sends address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(device); // start transmission to device
Wire.requestFrom(device, 1); // request 1 byte from device
int readed = 0;
if(Wire.available())
{
readed = Wire.read(); // receive a byte
}
Wire.endTransmission(); // end transmission
return readed;
}