Commit 80e716af authored by Eric Duminil's avatar Eric Duminil
Browse files

Wait until measurements are stable. Code mostly from Mario Lukas. GPL

parent 8c8966c0
......@@ -46,33 +46,61 @@ namespace sensor {
}
// SCD30 has its own timer.
Serial.println("\nSetting SCD30 timestep to " + String(config::measurement_timestep) + " s.");
Serial.println();
Serial.print(F("Setting SCD30 timestep to "));
Serial.print(config::measurement_timestep);
Serial.println(" s.");
scd30.setMeasurementInterval(config::measurement_timestep); // [s]
Serial.print("Setting temperature offset to -");
Serial.print(F("Setting temperature offset to -"));
Serial.print(abs(config::temperature_offset));
Serial.println(" K.");
scd30.setTemperatureOffset(abs(config::temperature_offset)); // setTemperatureOffset only accepts positive numbers, but shifts the temperature down.
Serial.print("Temperature offset is : -");
Serial.print(F("Temperature offset is : -"));
Serial.print(scd30.getTemperatureOffset());
Serial.println(" K");
Serial.print("Auto-calibration is ");
Serial.print(F("Auto-calibration is "));
Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF.");
}
// Force SCD30 calibration with countdown.
void startCalibrationProcess() {
void waitUntilMeasurementsAreStable() {
//TODO: Refactor completely, in order to avoid very long loop?
/** From the sensor documentation:
* For best results, the sensor has to be run in a stable environment in continuous mode at
* a measurement rate of 2s for at least two minutes before applying the FRC command and sending the reference value.
*/
Serial.println("Setting SCD30 timestep to 2s, prior to calibration.");
scd30.setMeasurementInterval(2); // [s] The change will only take effect after next measurement.
LedEffects::showKITTWheel(color::blue, config::measurement_timestep);
Serial.println("Waiting 2 minutes.");
LedEffects::showKITTWheel(color::blue, 120);
Serial.println(F("Setting SCD30 timestep to 2s, prior to calibration."));
scd30.setMeasurementInterval(2); // [s] The change will only take effect after next measurement.
Serial.println(F("Waiting until the measurements are stable for at least 2 minutes."));
Serial.println(F("It could take a very long time."));
//########################################################################################################
// (c) Mario Lukas
// https://github.com/mariolukas/Watterott-CO2-Ampel-Plus-Firmware/blob/main/CO2-Ampel_Plus/Sensor.cpp#L57
uint32_t last_color = color::blue;
int stable_measurements = 0, last_co2 = 0;
for (stable_measurements = 0; stable_measurements < 60;) {
if (scd30.dataAvailable()) {
co2 = scd30.getCO2();
//No more than +/-30ppm variation compared to previous measurement.
if ((co2 > (last_co2 - 30)) && (co2 < (last_co2 + 30))) {
last_color = color::green;
stable_measurements++;
} else {
last_color = color::red;
stable_measurements = 0;
}
last_co2 = co2;
}
LedEffects::showKITTWheel(last_color, 1);
}
//########################################################################################################
}
void startCalibrationProcess() {
waitUntilMeasurementsAreStable();
Serial.print("Starting SCD30 calibration...");
scd30.setAltitudeCompensation(config::altitude_above_sea_level);
scd30.setForcedRecalibrationFactor(config::co2_calibration_level);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment