Hi everyone, ı am trying to use ADXL345. I want to detect inactivity, activity and if new data available.
here is my full code:
#include <Wire.h> // Wire library - used for I2C communication
#define DEVICE 0x53 // ADXL345 device address
#define ADXL345_READ_ERROR 1 // problem reading accel
#define ADXL345_ERROR 0 // indicates error is predent
#define BW_RATE 0x2C
#define POWER_CTL 0x2D
#define THRESH_ACT 0x24 //-> activty threshold
#define THRESH_INACT 0x25 // -> inactivty threshold
#define TIME_INACT 0x26
#define ACT_INACTL_CTL 0x27
#define INT_ENABLE 0x2E
#define INT_MAP 0x2F
#define INT_SOURCE 0x30
#define DATA_FORMAT 0x31 // Data format control (2g,4g,8g,16g)
#define data 0x32
bool status; // set when error occurs
byte buff[6];
float X_out, Y_out, Z_out; // Outputs
byte nom[1];
byte error_code; // Initial state
boolean sa = false;
volatile int sensor_update = 0, active_mode = 0;
void setup() {
Serial.begin(115200); // Initiate serial communication for printing the results on the Serial monitor
while (!Serial);
writeTo(POWER_CTL, 0x08);
writeTo(BW_RATE,0x18);
writeTo(DATA_FORMAT, 0x01);
setActivityAxes(ACT_INACTL_CTL, 0xFF);
setActivityThreshold(5);
setInActivityThreshold(5);
setTimeInactivity(10);
writeTo(INT_MAP, 0x67);
writeTo(INT_ENABLE, 0x98);
writeTo(POWER_CTL, 0x38);
delay(10);
}
void loop() {
readFromAct(INT_SOURCE, 1, nom);
}
void setTimeInactivity(int timeInactivity) {
timeInactivity = constrain(timeInactivity, 0, 255);
byte _b = byte (timeInactivity);
writeTo(TIME_INACT, _b);
}
void setInActivityThreshold(int inActivityThreshold) {
inActivityThreshold = min(max(inActivityThreshold, 0), 255);
byte _b = byte (inActivityThreshold);
writeTo(THRESH_INACT, _b);
}
void setActivityAxes(byte address, byte val)
{
writeTo(address, val);
}
void setActivityThreshold(int activityThreshold) {
activityThreshold = min(max(activityThreshold, 0), 255);
byte _b = byte (activityThreshold);
writeTo(THRESH_ACT, _b);
}
void readFromAct(byte address, int num, byte _buff[]) {
Wire.beginTransmission(DEVICE); // start transmission to device
Wire.write(address); // writes 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++;
}
if (i != num) {
status = ADXL345_ERROR;
error_code = ADXL345_READ_ERROR;
}
if (_buff[0] & (1 << 4))
{
Serial.println("activty detected") ;
}
Wire.endTransmission(); // end transmission
if (_buff[0] & (1 << 3))
{
Serial.println(" Inactivty Detected , No activty detected") ;
}
if ((_buff[0] & (1 << 7) && _buff[0] & (1 << 1)))
{
Serial.println(_buff[0]);
readFrom(data, 6, buff);
}
}
void readFrom(byte address, int num, byte _buff[]) {
Wire.beginTransmission(DEVICE); // start transmission to device
Wire.write(address); // writes 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++;
}
if (i != num) {
status = ADXL345_ERROR;
error_code = ADXL345_READ_ERROR;
}
X_out = ((_buff[1] << 8) | _buff[0]);
Y_out = ((_buff[3] << 8) | _buff[2]);
Z_out = ((_buff[5] << 8) | _buff[4]);
Serial.print(X_out);
Serial.print(", ");
Serial.print(Y_out);
Serial.print(", ");
Serial.print(Z_out);
Serial.println("");
}
void writeTo(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
}
}
while this code, I can detect inactivity, activity and if new data useable. If inactivity detected, it use auto sleep too.
I detect these with this lines.
if (_buff[0] & (1 << 4))
{
Serial.println("activty detected") ;
}
Wire.endTransmission(); // end transmission
if (_buff[0] & (1 << 3))
{
Serial.println(" Inactivty Detected , No activty detected") ;
}
if ((_buff[0] & (1 << 7) && _buff[0] & (1 << 1)))
{
Serial.println(_buff[0]);
readFrom(data, 6, buff);
}
So I want to use interrupt. ADXL345 has 2 interrupt INT1 and INT2.
0x2F—INT_MAP Any bits set to 0 in this register send their respective interrupts to the INT1 pin.
So I use it like this. writeTo(INT_MAP, 0x67);
0x2E—INT_ENABLE Setting bits in this register to a value of 1 enables their respective functions to generate interrupts, whereas a value of 0 prevents the functions from generating interrupts.
So I use it like this. writeTo(INT_ENABLE, 0x98);
When I connect int1 pin to arduino's pin2, adxl345 is just not working.
I added this line on setup :
void setup()
{
.
.
.
.
attachInterrupt(digitalPinToInterrupt(int2Pin), inactivityISR, RISING);
.
.
.
}
here is my new loop
void inactivityISR(void) {
Serial.println("hi");
sensor_update = 1; //DATA_READY each 20ms
}
void loop()
{
if(sensor_update==1){
readFrom(data, 6, buff);
sensor_update=0;//reset
}
}
I just couldn't get this work with interrupt. Why it is not working ? I have been trying to get this thing work a lot but I couldn't success.