I have a code with a bme680 and a bno055. the bme680 slows down all the data print code.
I would like the code to be 100ms fast to print the bno055 data quickly and next to it print the bme680 data (even if it is slow it can keep the previous data until it manages to update). even if I separate the two loops with different speeds or it happens that the bme680 only gives values equal to 0 or the code slows everything down to 1.4 seconds. Are there solutions I can use or less complex libraries like those from Adafruit for BME?
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <SPI.h>
#include <Adafruit_BME680.h>
// Dichiarazione dei pin
#define BME_SCK 22
#define BME_MOSI 21
#define SEALEVELPRESSURE_HPA (1013.25)
// Dichiarazione degli oggetti dei sensori
Adafruit_BNO055 bno = Adafruit_BNO055(-1, 0x28, &Wire);
Adafruit_BME680 bme;
// Dichiarazione delle variabili per i dati dei sensori
float roll, pitch, yaw, roll_d, pitch_d, yaw_d;
float temperature, humidity, pressure, altitude;
// Variabili per la gestione del tempo
uint32_t lastExecution = 0;
uint32_t lastPrintTime = 0;
// Funzione per leggere i dati del sensore BNO055 e stamparli
void readAndPrintBNO055Data(void * parameter) {
while (true) {
// Lettura dei dati del sensore BNO055
imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
yaw = euler.x();
roll = euler.y();
pitch = -euler.z();
imu::Vector<3> gyro = bno.getVector(Adafruit_BNO055::VECTOR_GYROSCOPE);
yaw_d = gyro.z();
roll_d = gyro.y();
pitch_d = gyro.x();
// Stampa dei dati del sensore BNO055
Serial.print("£ , ");
Serial.print(roll);
Serial.print(" , ");
Serial.print(pitch);
Serial.print(" , ");
Serial.print(yaw);
Serial.print(" , ");
Serial.print(roll_d);
Serial.print(" , ");
Serial.print(pitch_d);
Serial.print(" , ");
Serial.print(yaw_d);
Serial.print(" , ");
Serial.print(temperature);
Serial.print(",");
Serial.print(pressure);
Serial.print(",");
Serial.print(humidity);
Serial.print(",");
Serial.print(altitude);
Serial.println(" , $");
// Attendi per un secondo prima di eseguire un'altra lettura
delay(1000);
}
}
void setup() {
Serial.begin(115200);
delay(1000);
// Inizializzazione del sensore BNO055
if (!bno.begin(OPERATION_MODE_NDOF)) {
Serial.println("Could not find BNO055 sensor!");
while (1);
}
// Inizializzazione del sensore BME680
if (!bme.begin()) {
Serial.println("Could not find BME680 sensor!");
while (1);
}
// Configurazione delle impostazioni del sensore BME680
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
// Creazione di un task separato per leggere e stampare i dati del sensore BNO055
xTaskCreatePinnedToCore(
readAndPrintBNO055Data, // Funzione da eseguire
"readAndPrintBNO055Data", // Nome del task
10000, // Dimensione dello stack del task
NULL, // Parametro passato alla funzione
1, // Priorità del task
NULL, // Handle del task (opzionale)
0 // Core su cui eseguire il task (Core 0)
);
}
void loop() {
unsigned long currentMillis = millis();
// Controllo del tempo per la lettura del sensore BME680 ogni secondo
if (currentMillis - lastExecution >= 2000) {
lastExecution = currentMillis;
// Lettura dei dati del sensore BME680
temperature = bme.readTemperature();
humidity = bme.readHumidity();
pressure = bme.readPressure() / 100;
altitude = 44330.0 * (1.0 - pow(pressure / SEALEVELPRESSURE_HPA, 0.190284));
}
}