#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
Adafruit_BME680 bme;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println(F("BME680 test"));
if (!bme.begin()) {
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
}
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_16X);
}
void loop() {
float tempReading[60];
for (int i = 0; i < 60; i++)
{
tempReading[i] = bme.temperature;
}
float tempTotal = 0;
for (int i = 0; i < 60; i++)
{
tempTotal += tempReading[i];
}
float averageTemp = tempTotal / 60;
Serial.println(averageTemp);
delay(1000);
}
which is supposed to measure the temperature over a full minute and then give me the average via Serial. However, it always says 0.00 (even after running for a full five minutes), which is obviously incorrect.
What did I do wrong?
@LarryD
I had forgotten an extremely important part of the code:
if (! bme.performReading()) {
Serial.println("Failed to perform reading :(");
return;
}
Now with this code:
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
Adafruit_BME680 bme;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println(F("BME680 test"));
if (!bme.begin()) {
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
}
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_16X);
}
void loop() {
if (! bme.performReading()) {
Serial.println("Failed to perform reading :(");
return;
}
float tempReading[60];
for (int i = 0; i < 60; i++)
{
tempReading[i] = bme.temperature;
}
float tempTotal = 0;
for (int i = 0; i < 60; i++)
{
tempTotal += tempReading[i];
}
float averageTemp = tempTotal / 60;
Serial.println(averageTemp);
delay(1000);
}
It gives me the average temp, however the 60 readings aren't spaced 1 second apart, they are all "at the same time".
I am wanting to get the average temperature over a minute, but it doesn't seem to be working.
According to the datasheet, some parts of this sensor take approximately 8 seconds to respond, so I have decided to take a reading every 10 seconds instead of every second.
This is the final code:
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
Adafruit_BME680 bme;
void setup() {
Serial.begin(2000000);
while (!Serial);
Serial.println(F("BME680 test"));
if (!bme.begin()) {
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
}
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_16X);
}
void loop() {
float tempReading[6];
for (int i = 0; i < 6; i++)
{
if (! bme.performReading()) {
Serial.println("Failed to perform reading :(");
return;
}
tempReading[i] = bme.temperature;
delay(10000); // wait ten seconds
}
float tempTotal = 0;
for (int i = 0; i < 6; i++)
{
tempTotal += tempReading[i];
}
float averageTemp = tempTotal / 6;
Serial.print("Average: ");
Serial.println(averageTemp);
}
Good to see the problem is solved now. Otherwise, I could have considered BM series of sensors inaccurate for temperature. I had the same experience when using BMP180.