Nano 33 IOT w/ LSM6DS3 not zeroing variable in loop

This is maybe not a Nano issue - but here goes:

  1. Messing around with a Nano 33 IOT and saw that it had the LSM6DS3 and decided to take a quick detour .... (first mistake??)
  2. Loaded up the fine Arduino_LSM6DS3 library 1.03 and Simple Accelerometer example. Noticed that the outputs were a little noisy and decided to do a little one-second averaging by averaging over a second based on the sample rate, devide the sum by the rate, print then then zero the accumulation variable and back into the loop.

ONLY - it doesn't work. It seems to increment the "z" access output UNLESS the z Accumulator is declared outside the "loop" i.e. as a global.

I am at wits end - so throwing it to the unwashed masses to see what they can figure out.

tried this on Ubuntu running the 2.3.1 IDE, and the web IDE as well

Here is output as it sits on my desk:

17:49:42.247 -> Acceleration in g's
17:49:42.247 -> X	Y	Z
17:49:43.240 -> 0.01	0.01	1.00
17:49:43.240 -> 0.00	0.00	0.00
17:49:44.275 -> 0.01	0.01	2.01
17:49:44.275 -> 0.00	0.00	0.00
17:49:45.273 -> 0.02	0.01	3.01
17:49:45.273 -> 0.00	0.00	0.00
17:49:46.309 -> 0.03	0.01	4.02
17:49:46.309 -> 0.00	0.00	0.00
17:49:47.306 -> 0.04	0.01	5.02
17:49:47.306 -> 0.00	0.00	0.00
17:49:48.336 -> 0.04	0.01	6.03
17:49:48.336 -> 0.00	0.00	0.00
17:49:47.306 -> 0.04	0.01	5.02
17:49:47.306 -> 0.00	0.00	0.00
17:49:48.336 -> 0.04	0.01	6.03
17:49:48.336 -> 0.00	0.00	0.00
17:49:49.367 -> 0.05	0.01	7.03
17:49:49.367 -> 0.00	0.00	0.00
17:49:50.366 -> 0.06	0.01	8.03
17:49:50.366 -> 0.00	0.00	0.00
17:49:51.394 -> 0.06	0.01	9.04
17:49:51.394 -> 0.00	0.00	0.00
17:49:52.395 -> 0.07	0.01	10.04
17:49:52.395 -> 0.00	0.00	0.00

Here is the "Code"

/*
  Arduino LSM6DS3 - Simple Accelerometer

  This example reads the acceleration values from the LSM6DS3
  sensor and continuously prints them to the Serial Monitor
  or Serial Plotter.

  The circuit:
  - Arduino Uno WiFi Rev 2 or Arduino Nano 33 IoT

  created 10 Jul 2019
  by Riccardo Rizzo

  This example code is in the public domain.
*/

#include <Arduino_LSM6DS3.h>
// float Accum_z;
int SampRate;

void setup() {
  Serial.begin(115200);
  while (!Serial);

  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");

    while (1);
  }

  Serial.print("Accelerometer sample rate = ");
  SampRate = IMU.accelerationSampleRate();
  Serial.print(SampRate);
  Serial.println(" Hz");
  Serial.println();
  Serial.println("Acceleration in g's");
  Serial.println("X\tY\tZ");
}

void loop() {

  float x;
  float y;
  float z;
  float Accum_x;
  float Accum_y;
  float Accum_z;
  
  for (int Count = 1; Count <= SampRate ; Count ++){
     while ( ! IMU.accelerationAvailable() ){};
      IMU.readAcceleration(x, y, z);
      Accum_z += z;
      Accum_y += y;
      Accum_x += x;
    
  }
    Serial.print(Accum_x/SampRate);
    Serial.print('\t');
    Serial.print(Accum_y/SampRate);
    Serial.print('\t');
    Serial.println(Accum_z/SampRate);
    Accum_x = 0 ;
    Accum_y = 0 ;
    Accum_z = 0 ;
    Serial.print(Accum_x);
    Serial.print('\t');
    Serial.print(Accum_y);
    Serial.print('\t');
    Serial.println(Accum_z);
}

It's a dead simple that works if the "float Accum_z;" is declared globaly, which I just do not get - makes no sense and I need a sanity check.

T_Roch

Just adding a few more datums (NOTE: the code was returned to normal except where indicated):

  1. The "x" channel has a similar issue
  2. tried setting to double types - no differenace
  3. tried setting "= 0.0;" vs. "= 0;" : no differance
  4. did a Serial.print(Accum_z); prior to the "Accum_z = 0.0;" and it corrected!
  5. put in a "delay" prior to zeroing - no change
  6. if the post zeroing print is entirely commented out, the "x" channel corrects (no growth)
  7. If the "Serial.print(Accum_x/SampRate);" is commented out, the "z" channel is correct(!)
  8. if the "Serial.print(Accum_y/SampRate);" line is commented out, the "z" channel is correct(!) and
    the "x" channel is correct
  9. Commented out "Serial.println(Accum_z/SampRate);" adding println to the prior line and the "x"
    channel coorected
  10. replaced "Serial.print(Accum_x/SampRate);" with
Accum_x /= SampRate;
    Serial.print(Accum_x);

" and the "x" channel was incorrect (accumulating)
11) set "Accum_z = 0.0;" to "Accum_z = Accum_y ;" : "z" channel still accumulating.
12) set Serial.print(Accum_z/SampRate); Serial.print('\t'); Serial.print(Accum_y/SampRate); Serial.print('\t'); Serial.println(Accum_z/SampRate);
and the "y" channel started accumulating!

  1. set prints to Serial.print(Accum_z/SampRate); Serial.print('\t'); Serial.print(Accum_y/SampRate); Serial.print('\t'); Serial.println(Accum_x/SampRate);
    and the "x" channel accumulated...
  2. uncommented the post zero prints and the "x" channel and "z" channel were accumulating.

Still seems dead simple that the zero should zero!

Note: useing Firmware 1.5 for the NANO 33 IoT, tried the 1.3.0 for giggles, and still bad - tried 1.0 as well so it doesn't look like a firmware injected issue.

This looks like a race condition or compiler bug, but I don't have the skill or knowlege to dig much deeper.

T_Roch

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