Putting samd-based board into sleep mode

I'm using a SAMD21G18A based board - the arduino zero, for a battery-powered wearable project (a type of computer mouse). I'm trying to figure out how to put the device to sleep, when it hasn't been moved for a period of time. I'm using data from a connected gyroscope, which is stored into an array. If the sum of the data in the array is less than some threshold, then I put the device into sleep using the sleep function from the arduino low-power library. I'm waking the device with an interrupt from the sensor.

void nomotion_sleep(float x_value) {
  static float sleep_threshold = 15;
  static float sleep_array[15];
  static byte sa_size = 15;
  static float asum; // array sum
  static byte i = 0;
  static uint32_t array_timer;

  if (timer1(array_timer, 60000)) { // a timer f, checks every min
    sleep_array[i] = abs(x_value); //x_value is from the gyro
    asum += sleep_array[i];
    i += 1;
    if (i == sa_size - 1) {
      if (asum < sleep_threshold) {
        //sleep
        LowPower.sleep();
      }
      for (byte ii = 0; ii < sa_size; ii += 1) {
        sleep_array[i] = 0;
      }
      asum = 0;
      i = 0;
    }
  }
}

The above code is my attempt to do this, but I'm not sure if I need to erase the asum value somehow, if the device goes to sleep. And not sure how to do this. Maybe upon wake up in the function called by the interrupt?

Is there a better way to do this? Thank you for trying to help.

Memory contents should not be affected by sleep/wake cycles.

Do You have a strategy how to wake the controller up from the sleep?

Thank you @jremington . So that means I need to delete the array sum variable upon waking the device up?

@Railroader yes, with an interrupt from the sensor, thanks for pointing it out.

Sorry, I did not try to understand your code, and I don't understand your question. You can't delete a variable.

No worries, thanks for helping anyways.

Fair enough so far. A digital sensor?

yes a bmx160; but not sure how to write the code to put the device to sleep, since I'm using the sensor interrupts to wake up.

Check the datasheets for that!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.