Open Haoyu-R opened 3 years ago
I got the same problem.
void Sensors::Loop(SystemState &state) {
if(DEBUG) Serial.println("BEGIN Sensors::Loop()");
long currentMillis = millis();
if(currentMillis - previousMillis >= SAVE_INTERVAL) {
float temperature = ReadTemperature();
Serial.flush();
float pressure = ReadPressure();
Serial.flush();
float humidity = ReadHumidity();
Serial.flush();
float acc_x, acc_y, acc_z;
ReadAcceleration(acc_x, acc_y, acc_z);
Serial.flush();
float g_x, g_y, g_z;
ReadGyroscope(g_x, g_y, g_z);
Serial.flush();
float m_x, m_y, m_z;
ReadMagneticField(m_x, m_y, m_z);
Serial.flush();
DataPoint newItem = {state.VehicleState, millis(), pressure, temperature, acc_x, acc_y, acc_z, g_x, g_y, g_z, m_x, m_y, m_z};
Serial.flush();
state.CurrentDataPoint = newItem;
Serial.flush();
previousMillis = currentMillis;
}
if(DEBUG) Serial.println("END Sensors::Loop()");
}
float Sensors::ReadPressure() {
if(DEBUG) Serial.println("BEGIN Sensors::ReadPressure()");
return BARO.readPressure();
}
The print statements are obviously for debugging purposes...
Serial output:
(...)
BEGIN Sensors::Loop()
BEGIN Sensors::ReadTemperature()
BEGIN Sensors::ReadPressure()
The SAVE_INTERVAL is 50ms. If I replace the ReadPressure() with 0.0, the sketch doesn't crash...
This library seems to work:
https://github.com/pilotak/LPS35HW
If you want to try it, don't forget to Init it with !lps.begin(&Wire1)
FYI:
I created a topic in the Arduino forum. Maybe the issues are related: https://forum.arduino.cc/t/sensor-readings-slow-down-after-a-couple-of-minutes/869046/7
Hi guys,
I lost several hours to solve this strange bug. The board I am using is Arduino Nano BLE Sense 33.
In short, I use sprintf to format sensor readings before Serial.print() in loop(). At the start, the program works perfectly. However, after many iterations of loop() the system hangs.
After I checked the program carefully, I found It's pressure sensor reading function causing the crash.
pressure = BARO.readPressure();
If I comment out this function, or simply replace this line with:
pressure = 5;
The program will not crash anymore.
Why I found the pressure sensor cause the froze:
I changed the code like this:
After several loops, the program freezes after the serial port outputs "test1". No output and No error printed in serial port afterward.
What weird is that, if I comment out all the other sensor reading functions except the pressure sensor, the program can run further. (At least for 20 minutes. I don't know if it will crash after a longer period)
Well the code crashes here in the library: https://github.com/arduino-libraries/Arduino_LPS22HB/blob/d3d6bacb12cd4e8519494e3a440a1f7c29e9cfc3/src/BARO.cpp#L60-L63
Here is my sketch: simply read some sensor value, format them and print to the serial port.