Hello everyone
I am currently trying to make use of an ADXL345 sensor to detect activity and I am also making use of the I2Cdev library for the device. I want the interrupt to take place on INT1. Below is some code:
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#include "Wire.h"
// I2Cdev and ADXL345 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "ADXL345.h"
// class default I2C address is 0x53
// specific I2C addresses may be passed as a parameter here
// ALT low = 0x53 (default for SparkFun 6DOF board)
// ALT high = 0x1D
ADXL345 accel;
int16_t ax, ay, az;
#define LED_PIN 13 // (Arduino is 13, Teensy is 6)
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
Wire.begin();
// initialize serial communication
Serial.begin(9600);
// initialize device
Serial.println("Initializing I2C devices...");
accel.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(accel.testConnection() ? "ADXL345 connection successful" : "ADXL345 connection failed");
// configure LED for output
pinMode(LED_PIN, OUTPUT);
//Create an interrupt that will trigger when an activity is detected.
attachInterrupt(0, activity, RISING);
accel.setActivityAC(true); // AC-Coupled
accel.setInterruptMode(1); // Set interrupts to active low
accel.setActivityThreshold(20); // Set the threshold
accel.setIntActivityEnabled(true); // Set Interrupt Activity as enabled
accel.setActivityXEnabled(true);
accel.setActivityYEnabled(true);
accel.setActivityZEnabled(true);
accel.setIntActivityPin(0); // Create Activity interrupt on INT1
}
void activity(void){
// Run this code if Activity Interrupt was triggered
if(accel.getIntActivitySource() == 16){ // 16 is 0001 0000 which is D4 of INT_SOURCE Register
digitalWrite(LED_PIN, HIGH);
}
}
void loop() {
// read raw accel measurements from device
accel.getAcceleration(&ax, &ay, &az);
// display tab-separated accel x/y/z values
Serial.print("accel:\t");
Serial.print(ax); Serial.print("\t");
Serial.print(ay); Serial.print("\t");
Serial.println(az);
}
For some reason the Arduino freezes (when serial monitor open, can see it freeze up when you tap it or move it). Hope someone can point out what I am doing wrong.
Thank you