This is maybe not a Nano issue - but here goes:
- Messing around with a Nano 33 IOT and saw that it had the LSM6DS3 and decided to take a quick detour .... (first mistake??)
- 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