0

Adalogger External Interrupt +IMU not returning to main loop
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adalogger External Interrupt +IMU not returning to main loop

by ECSydney on Fri Jun 14, 2019 9:51 am

Hi everyone,

Im currently making a device which uses the BNO080 IMU to detect motion (eventually I will be recording data from three other devices but I'm trying to iron out the kinks in the interrupt aspect of the code before I include the logging aspects of the code). If there isn't motion the code enters a countdown section that will then put the feather to sleep until an external interrupt, (a vibration switch) activated the interrupt and ideally runs the code from the start.

Currently the code counts down and turn the feather off, one I activate the interrupt the light turns on, but it doesn't return to the void loop after the wake up.

Any advice on how to fix this problem would be greatly appreciated.

Code: Select all | TOGGLE FULL SIZE
 

#include <Wire.h>
#include "ArduinoLowPower.h"
#include "SparkFun_BNO080_Arduino_Library.h"
#include <math.h>
#define LED_BUILDIN 13
#define interruptPin 6



BNO080 myIMU;
float z;
float y;
float x;
float Total;
volatile float mag;
volatile int Counter = 3000;


void Going_To_Sleep() {
  LowPower.attachInterruptWakeup(digitalPinToInterrupt(interruptPin), wakeUp, LOW);

  digitalWrite(LED_BUILDIN, LOW);
  Serial.println("AHHHH");
  delay(100);
  LowPower.sleep();
}

void wakeUp() {

  digitalWrite(LED_BUILTIN, HIGH);
  Counter = 3000;

}



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);


  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  digitalWrite(LED_BUILTIN, HIGH);
  attachInterrupt(digitalPinToInterrupt(interruptPin), wakeUp, LOW);
 


  Wire.begin();
  myIMU.begin();
  myIMU.enableAccelerometer(50); //Send data update every 50ms
  Serial.println(F("Accelerometer enabled"));
  Serial.println(F("Output in form x, y, z, in m/s^2"));


}

void loop() {

  myIMU.dataAvailable();

  z = myIMU.getAccelZ(); //should be recording and flushing this data.
  y = myIMU.getAccelY(); //should be recording and flushing this data.
  x = myIMU.getAccelX(); //should be recording and flushing this data.


  mag = sqrt(pow(z, 2) + pow(y, 2) + pow(x, 2));
  //Serial.println("Magnitude is:");
  //  Serial.print (mag);
  //  Serial.println();


  if (mag > 9.9 && mag < 10.5) {
    Counter--;
    Serial.println(Counter);

  }
  else if ( (mag > 10.5) || (mag < 9.0)) {
    Counter = 3000;

  }


 
  if (Counter <= 0) {
    //    LowPower.attachInterruptWakeup(6, wakeUp, CHANGE);
    Serial.println("ENTERSLEEPMODE");
    Going_To_Sleep();
  }
}

ECSydney
 
Posts: 1
Joined: Fri Jun 14, 2019 9:44 am

Re: Adalogger External Interrupt +IMU not returning to main

by XRAD on Fri Jun 14, 2019 5:50 pm

There are some rules that apply to ISR's, like using delays, and millis(). As an alternative, did you try something like (NOT tested):

Code: Select all | TOGGLE FULL SIZE
void Going_To_Sleep() {  // u r in this void until broken
  LowPower.attachInterruptWakeup(digitalPinToInterrupt(interruptPin), wakeUp, LOW);

  digitalWrite(LED_BUILDIN, LOW);
  Serial.println("AHHHH");
  delay(100);
  LowPower.sleep();

if(something == true){
  wake_up(); /// or maybe call to loop();
 }
else if (something == false){
Going_To_Sleep();
 }
}


it's in the logic assembly; I have called back to the the loop() (like any other function) on many functions() and usually works for me......

if you call to wake_up(); you need to send it back to the loop, too..

Code: Select all | TOGGLE FULL SIZE
void wakeUp() {

  digitalWrite(LED_BUILTIN, HIGH);
  Counter = 3000;

loop();

}





once you get the logic down, you can edit out lines and combine code to make a shorter sketch, but it really won't effect the speed of the sketch...

XRAD
 
Posts: 471
Joined: Sat Nov 19, 2016 3:28 pm

Please be positive and constructive with your questions and comments.