ADXL345 Interrupt

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
LukeeSVK
 
Posts: 7
Joined: Fri Jun 08, 2018 2:54 pm

ADXL345 Interrupt

Post by LukeeSVK »

Hallo, i quickly need help.
I bought ADXL345 from adafruit and sim800 from i dont know where.

I'm working on tracking system for my motocycle, but problem is, that when i'm controlling SMS and Accelerometer in same time, one measure cost 2 seconds of time. I need to make something like when someone shake with motorcycle accelometer will send interrupt and ..... It's possible to set somehow interrupt for low sensitivity ? I found this code, but whatever i change, still bad for me :/

Code: Select all

//Arduino 1.0+ Only!
//Arduino 1.0+ Only!

#include <Wire.h>
#include <ADXL345.h>


ADXL345 adxl; //variable adxl is an instance of the ADXL345 library

void setup(){
Serial.begin(9600);
adxl.powerOn();

//set activity/ inactivity thresholds (0-255)
adxl.setActivityThreshold(75); //62.5mg per increment
adxl.setInactivityThreshold(75); //62.5mg per increment
adxl.setTimeInactivity(10); // how many seconds of no activity is inactive?

//look of activity movement on this axes - 1 == on; 0 == off 
adxl.setActivityX(1);
adxl.setActivityY(1);
adxl.setActivityZ(1);

//look of inactivity movement on this axes - 1 == on; 0 == off
adxl.setInactivityX(1);
adxl.setInactivityY(1);
adxl.setInactivityZ(1);

//look of tap movement on this axes - 1 == on; 0 == off
adxl.setTapDetectionOnX(0);
adxl.setTapDetectionOnY(0);
adxl.setTapDetectionOnZ(1);

//set values for what is a tap, and what is a double tap (0-255)
adxl.setTapThreshold(50); //62.5mg per increment
adxl.setTapDuration(15); //625μs per increment
adxl.setDoubleTapLatency(80); //1.25ms per increment
adxl.setDoubleTapWindow(200); //1.25ms per increment

//set values for what is considered freefall (0-255)
adxl.setFreeFallThreshold(7); //(5 - 9) recommended - 62.5mg per increment
adxl.setFreeFallDuration(45); //(20 - 70) recommended - 5ms per increment

//setting all interupts to take place on int pin 1
//I had issues with int pin 2, was unable to reset it
adxl.setInterruptMapping( ADXL345_INT_SINGLE_TAP_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_DOUBLE_TAP_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_FREE_FALL_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_ACTIVITY_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_INACTIVITY_BIT, ADXL345_INT1_PIN );

//register interupt actions - 1 == on; 0 == off 
adxl.setInterrupt( ADXL345_INT_SINGLE_TAP_BIT, 1);
adxl.setInterrupt( ADXL345_INT_DOUBLE_TAP_BIT, 1);
adxl.setInterrupt( ADXL345_INT_FREE_FALL_BIT, 1);
adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT, 1);
adxl.setInterrupt( ADXL345_INT_INACTIVITY_BIT, 1);
}

void loop(){

//Boring accelerometer stuff 
int x,y,z; 
adxl.readAccel(&x, &y, &z); //read the accelerometer values and store them in variables x,y,z

// Output x,y,z values - Commented out
//Serial.print(x);
//Serial.print(y);
//Serial.println(z);


//Fun Stuff! 
//read interrupts source and look for triggerd actions

//getInterruptSource clears all triggered actions after returning value
//so do not call again until you need to recheck for triggered actions
byte interrupts = adxl.getInterruptSource();

// freefall
if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){
Serial.println("freefall");
//add code here to do when freefall is sensed
} 

//inactivity
if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){
Serial.println("inactivity");
//add code here to do when inactivity is sensed
}

//activity
if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){
Serial.println("activity"); 
//add code here to do when activity is sensed
}

//double tap
if(adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)){
Serial.println("double tap");
//add code here to do when a 2X tap is sensed
}

//tap
if(adxl.triggered(interrupts, ADXL345_SINGLE_TAP)){
Serial.println("tap");
//add code here to do when a tap is sensed
} 


}

User avatar
LukeeSVK
 
Posts: 7
Joined: Fri Jun 08, 2018 2:54 pm

Re: ADXL345 Interrupt

Post by LukeeSVK »

Anyone please ? or admin ? thx

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: ADXL345 Interrupt

Post by adafruit_support_bill »

Your code does not appear to be using the interrupt output of the ADXL345. You need to connect the interrupt output of the sensor to an interrupt pin on your processor. What processor are you using?

User avatar
LukeeSVK
 
Posts: 7
Joined: Fri Jun 08, 2018 2:54 pm

Re: ADXL345 Interrupt

Post by LukeeSVK »

Arduino Nano, INT1 connected to D2

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: ADXL345 Interrupt

Post by adafruit_support_bill »

D2 on the Nano is external interrupt 0. You need to attach to that interrupt and write an interrupt handler for it. See the documentation here for details:
https://www.arduino.cc/reference/en/lan ... interrupt/

User avatar
LukeeSVK
 
Posts: 7
Joined: Fri Jun 08, 2018 2:54 pm

Re: ADXL345 Interrupt

Post by LukeeSVK »

and how that interrupt handler looks like ? cause i tried to looking everywhere, writting, but i cant find way to interrupt when i shake with that

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: ADXL345 Interrupt

Post by adafruit_support_bill »

See the example code in the documentation: https://www.arduino.cc/reference/en/lan ... interrupt/

The 'blink' function is the interrupt handler:

Code: Select all

const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}

User avatar
LukeeSVK
 
Posts: 7
Joined: Fri Jun 08, 2018 2:54 pm

Re: ADXL345 Interrupt

Post by LukeeSVK »

I made code, but I tested INT1 on adxl345 connect to D2, D3 with changing in the code, but serial is clear with NOTHING txt, more in code...

Code: Select all

#include <Adafruit_ADXL345_U.h>

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

const byte interruptPin = 3;
volatile byte state = LOW;

void setup() {
  Serial.begin(9600);
  accel.begin();
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  Serial.println("NOTHING");
  delay(10000);
}

void blink() {
  sensors_event_t accelEvent; 
  accel.getEvent(&accelEvent);

  Serial.println(1);
  Serial.println(accelEvent.acceleration.x);
    
  state = !state;
}

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: ADXL345 Interrupt

Post by adafruit_support_bill »

Interrupts are disabled upon entry to the interrupt handler. So it is not possible to do things that require interrupts such as serial or i2c communication. In general, you need to minmize the time you spend in an interrupt handler, so serial output is not a good idea anyway.

It is possible to re-enable interrupts: https://www.arduino.cc/reference/en/lan ... nterrupts/
You could also wire your sensor for software SPI operation. That will still work with the interrupts disabled.

User avatar
LukeeSVK
 
Posts: 7
Joined: Fri Jun 08, 2018 2:54 pm

Re: ADXL345 Interrupt

Post by LukeeSVK »

I found this on Sparkfun's github:

Code: Select all

/*  ********************************************* 
 *  SparkFun_ADXL345_Example
 *  Triple Axis Accelerometer Breakout - ADXL345 
 *  Hook Up Guide Example 
 *  
 *  Utilizing Sparkfun's ADXL345 Library
 *  Bildr ADXL345 source file modified to support 
 *  both I2C and SPI Communication
 *  
 *  E.Robert @ SparkFun Electronics
 *  Created: Jul 13, 2016
 *  Updated: Sep 06, 2016
 *  
 *  Development Environment Specifics:
 *  Arduino 1.6.11
 *  
 *  Hardware Specifications:
 *  SparkFun ADXL345
 *  Arduino Uno
 *  *********************************************/

#include <SparkFun_ADXL345.h>         // SparkFun ADXL345 Library

/*********** COMMUNICATION SELECTION ***********/
/*    Comment Out The One You Are Not Using    */
ADXL345 adxl = ADXL345();             // USE FOR I2C COMMUNICATION

/****************** INTERRUPT ******************/
/*      Uncomment If Attaching Interrupt       */
int interruptPin = 2;                 // Setup pin 2 to be the interrupt pin (for most Arduino Boards)


/******************** SETUP ********************/
/*          Configure ADXL345 Settings         */
void setup(){
  
  Serial.begin(9600);                 // Start the serial terminal
  Serial.println("SparkFun ADXL345 Accelerometer Hook Up Guide Example");
  Serial.println();
  
  adxl.powerOn();                     // Power on the ADXL345

  adxl.setRangeSetting(16);           // Give the range settings
                                      // Accepted values are 2g, 4g, 8g or 16g
                                      // Higher Values = Wider Measurement Range
                                      // Lower Values = Greater Sensitivity

  adxl.setSpiBit(0);                  // Configure the device to be in 4 wire SPI mode when set to '0' or 3 wire SPI mode when set to 1
                                      // Default: Set to 1
                                      // SPI pins on the ATMega328: 11, 12 and 13 as reference in SPI Library 
   
  adxl.setActivityXYZ(1, 0, 0);       // Set to activate movement detection in the axes "adxl.setActivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
  adxl.setActivityThreshold(75);      // 62.5mg per increment   // Set activity   // Inactivity thresholds (0-255)
 
  adxl.setInactivityXYZ(1, 0, 0);     // Set to detect inactivity in all the axes "adxl.setInactivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
  adxl.setInactivityThreshold(75);    // 62.5mg per increment   // Set inactivity // Inactivity thresholds (0-255)
  adxl.setTimeInactivity(10);         // How many seconds of no activity is inactive?

  adxl.setTapDetectionOnXYZ(0, 0, 1); // Detect taps in the directions turned ON "adxl.setTapDetectionOnX(X, Y, Z);" (1 == ON, 0 == OFF)
 
  // Set values for what is considered a TAP and what is a DOUBLE TAP (0-255)
  adxl.setTapThreshold(50);           // 62.5 mg per increment
  adxl.setTapDuration(15);            // 625 μs per increment
  adxl.setDoubleTapLatency(80);       // 1.25 ms per increment
  adxl.setDoubleTapWindow(200);       // 1.25 ms per increment
 
  // Set values for what is considered FREE FALL (0-255)
  adxl.setFreeFallThreshold(7);       // (5 - 9) recommended - 62.5mg per increment
  adxl.setFreeFallDuration(30);       // (20 - 70) recommended - 5ms per increment
 
  // Setting all interupts to take place on INT1 pin
  //adxl.setImportantInterruptMapping(1, 1, 1, 1, 1);     // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);" 
                                                        // Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts.
                                                        // This library may have a problem using INT2 pin. Default to INT1 pin.
  
  // Turn on Interrupts for each mode (1 == ON, 0 == OFF)
  adxl.InactivityINT(1);
  adxl.ActivityINT(1);
  adxl.FreeFallINT(1);
  adxl.doubleTapINT(1);
  adxl.singleTapINT(1);
  
attachInterrupt(1, ADXL_ISR, RISING);   // Attach Interrupt

}

/****************** MAIN CODE ******************/
/*     Accelerometer Readings and Interrupt    */
void loop(){
  
  // Accelerometer Readings
  int x,y,z;   
  adxl.readAccel(&x, &y, &z);         // Read the accelerometer values and store them in variables declared above x,y,z

  // Output Results to Serial
  /* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */  
  //Serial.print(x);
  //Serial.print(", ");
  //Serial.print(y);
  //Serial.print(", ");
  //Serial.println(z); 
  
  ADXL_ISR();
  // You may also choose to avoid using interrupts and simply run the functions within ADXL_ISR(); 
  //  and place it within the loop instead.  
  // This may come in handy when it doesn't matter when the action occurs. 

}

/********************* ISR *********************/
/* Look for Interrupts and Triggered Action    */
void ADXL_ISR() {
  
  // getInterruptSource clears all triggered actions after returning value
  // Do not call again until you need to recheck for triggered actions
  byte interrupts = adxl.getInterruptSource();
  
  // Free Fall Detection
  if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){
    Serial.println("*** FREE FALL ***");
    //add code here to do when free fall is sensed
  } 
  
  // Inactivity
  if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){
    Serial.println("*** INACTIVITY ***");
     //add code here to do when inactivity is sensed
  }
  
  // Activity
  if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){
    Serial.println("*** ACTIVITY ***"); 
     //add code here to do when activity is sensed
  }
  
  // Double Tap Detection
  if(adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)){
    Serial.println("*** DOUBLE TAP ***");
     //add code here to do when a 2X tap is sensed
  }
  
  // Tap Detection
  if(adxl.triggered(interrupts, ADXL345_SINGLE_TAP)){
    Serial.println("*** TAP ***");
     //add code here to do when a tap is sensed
  } 
}
There is some configuration in their library, so there is my problem.... when i used that code, it cant know what i wanted ...
But i still dont know how can i configure it in Adafruit library... even i cant configure in sparkfun shake detection. What now ?

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: ADXL345 Interrupt

Post by adafruit_support_bill »

But i still dont know how can i configure it in Adafruit library... even i cant configure in sparkfun shake detection. What now ?
The Adafruit library follows the Unified Sensor model - which does not have support for interrupts. If you want to use interrupts with this sensor, there are many ADXL345 libraries such as the Sparkfun version that have interrupt support.
There is some configuration in their library, so there is my problem.... when i used that code, it cant know what i wanted
No device or library is going to read your mind. You need to define precisely what you want. You also need to read the device data sheet to understand the capabilities of the device. http://www.analog.com/media/en/technica ... DXL345.pdf
...even i cant configure in sparkfun shake detection. What now ?
The Sparkfun library supports all of the configuration options in the ADXL345. But there is nothing called "shake detection" in that sensor. You can configure it to generate interrupts for taps, free-falls or activity above a configured threshold. These are defined on page 20 of the data sheet.

If your concept of 'shake detection' can be defined in terms of acceleration thresholds that the sensor understands, then that is what you need to configure in the sensor.

User avatar
LukeeSVK
 
Posts: 7
Joined: Fri Jun 08, 2018 2:54 pm

Re: ADXL345 Interrupt

Post by LukeeSVK »

Thanks for helping, shake detection its ... I mean, when you take it to the hand or shake with that, maybe just change slope

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: ADXL345 Interrupt

Post by adafruit_support_bill »

Sounds like what you want is an interrupt on 'activity'. Try enabling interrupts for activity and experiment with the activity threshold to find a level that works for you.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”