Implementing a delay in the sketch renders the sensor values unusable

Hi,

I am using an Arduino Nano 33 IoT and I am trying to get Roll and Pitch values from the built-in IMU and send these to a Google Cloud IoT Core Gateway running on a Raspberry Pi. To collect the Roll and Pitch values I am using the following library: Arduino_LSM6DS3/RollPitchYaw.ino at master · owennewo/Arduino_LSM6DS3 · GitHub

The library works well and is collecting the values I want. The issue I am encountering is that to send these sensor values to Google Cloud IoT Core Gateway, I need to send them via UDP. With the help of an example I found I am able to do this however I am unable to implement any delays into the code I currently have and many packets containing sensor information I want are dropped when sent to the gateway due to the volume. As soon as I input any delays I am unable to get any usable sensor information, for example the raw values show as “nan” and “inf” when printed to serial monitor.

I’ve attached the sketch I’m using below, the delays I tried to use are on lines 148 and 178 respectively. As soon as I uncomment these out the sketch stops reading the sensor values correctly. I’m just trying to get a better understanding of why this is happening.

Does anyone have any recommendations on how I can implement a delay while maintaining the sensor values so the sensor data is collected every 3-5 seconds or so?

Or would it take a completely different approach to achieve this?

YawPitchRoll6.ino (8.1 KB)

check out the BlinkWithoutDelay example (File->Examples->02.Digital->BlinkWithoutDelay) to see how to implement a wait time without blocking your code. Basically, you want to continue to read the IMU but only transmit the data to the cloud every X seconds.

Also, your currentTime and lastTime variables should be unsigned long [not that you actually use them...]

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R