Using Nano33BLE, LSM9DS1 Library accelerometer function which works fine in main loop. Now I want to save one-time reading on program power up to use as base for subsequent readings in loop. All variables (sensor readings) declared as global (top of pgm). Two sensor initiations set up, one in pgm set up to be read once, one in main loop to read repetitively.
Problem: the one in set up does not return values.
Please show the code;
/*
Arduino LSM9DS1 - Simple Accelerometer
This example reads the acceleration values from the LSM9DS1
sensor and continuously prints them to the Serial Monitor
or Serial Plotter.
The circuit:
- Arduino Nano 33 BLE Sense
created 10 Jul 2019
by Riccardo Rizzo
This example code is in the public domain.
*/
#include <Arduino_LSM9DS1.h>
float x, y, z;
float q;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Started");
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
if (IMU.accelerationAvailable())
{
IMU.readAcceleration(x, y, z);
}
q = y;
}
void loop() {
delay(500);
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
Serial.print(q);
Serial.print('\t');
Serial.print(y);
Serial.print('\t');
Serial.println(y - q);
}
}
try this:
while ( ! IMU.accelerationAvailable());
if (IMU.accelerationAvailable())
{
IMU.readAcceleration(x, y, z);
}
Fantasticl! My undying gratitude. I don't quite understand why this works, but it does. Ralph
Please mark the thread solved
The reason this works is because it makes the sketch "wait" until the first valid result is available.
The logic of the LSM9DS1 is slow compared to the Arduino. It was designed for low power and needs low noise to make some good measurements. It takes a few milliseconds for the LSM9DS1 to start and create valid values. That is a long time for the processor even when using a serial interface to check for new values.
Try to add a counter to the while loop and see how often the code needs to check for a valid value. Then add some delay just before the while loop. Increase the delay until the counter returns zero. Then you measured the time it takes the LSM9DS1 to return valid valid after initialization.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.