1

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 issue is if the device goes into sleep the array sum value is not = 0 for the next iteration. Not sure how to fix this. Should I make it a volatile global variable and change upon wake up in the ISR?

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

Zhelyazko Grudov
  • 367
  • 1
  • 11

0 Answers0