From 2da8860215f6bac6c598a65868b6fef9bfd8069d Mon Sep 17 00:00:00 2001 From: igor Date: Tue, 11 Apr 2023 18:26:24 -0700 Subject: [PATCH] simulate parallelism --- aqi.ino | 132 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 81 insertions(+), 51 deletions(-) diff --git a/aqi.ino b/aqi.ino index b556546..ce7ae47 100644 --- a/aqi.ino +++ b/aqi.ino @@ -8,6 +8,26 @@ SensirionI2CSen44 sen44; LiquidCrystal lcd(12, 11, 5, 4, 3, 2); const int buttonPin = 6; +// to simulate "parallelism" +unsigned long currentTime; +unsigned long sen44Time = 0; +unsigned long lcdTime = 0; +unsigned long buttonTime = 0; + +// display standard values by default +bool isStandardValues = true; + +// sen44 values +uint16_t massConcentrationPm1p0 = 0; +uint16_t massConcentrationPm2p5 = 0; +uint16_t massConcentrationPm4p0 = 0; +uint16_t massConcentrationPm10p0 = 0; +float vocIndex = 0; +float ambientHumidity = 0; +float ambientTemperature = 0; +float feelsLikeTemperature = 0; + +// calculate heat index float heatIndex(float t, float rh) { float hi = 0.0; if (t <= 40.0) @@ -32,6 +52,7 @@ float heatIndex(float t, float rh) { return hi; } +// convert celcius to fahrenheit float celsiusToFahrenheit(float degrees) { return degrees * 1.8 + 32; } @@ -75,69 +96,78 @@ void setup() { } void loop() { - uint16_t error; - char errorMessage[256]; + currentTime = millis(); - delay(1000); + delay(20); - // Read Measurement - uint16_t massConcentrationPm1p0; - uint16_t massConcentrationPm2p5; - uint16_t massConcentrationPm4p0; - uint16_t massConcentrationPm10p0; - float vocIndex; - float ambientHumidity; - float ambientTemperature; - float feelsLikeTemperature; + if (currentTime - buttonTime >= 100) { + buttonTime = currentTime; - error = sen44.readMeasuredMassConcentrationAndAmbientValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, vocIndex, ambientHumidity, ambientTemperature); - - if (error) { - Serial.print("Error trying to execute " - "readMeasuredMassConcentrationAndAmbientValues(): "); - errorToString(error, errorMessage, 256); - Serial.println(errorMessage); - return; + if (digitalRead(buttonPin) == LOW) { + isStandardValues = !isStandardValues; + } } - ambientTemperature = celsiusToFahrenheit(ambientTemperature); - feelsLikeTemperature = heatIndex(ambientTemperature, ambientHumidity); + if (currentTime - sen44Time >= 1000) { + sen44Time = currentTime; - lcd.clear(); + uint16_t error; + char errorMessage[256]; - if (digitalRead(buttonPin) == LOW) { - lcd.setCursor(0, 0); - lcd.print(" 1:"); - lcd.print(massConcentrationPm1p0); + // Read Measurement + error = sen44.readMeasuredMassConcentrationAndAmbientValues( + massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, + massConcentrationPm10p0, vocIndex, ambientHumidity, ambientTemperature); - lcd.setCursor(8, 0); - lcd.print(" 2:"); - lcd.print(massConcentrationPm2p5); + if (error) { + Serial.print("Error trying to execute " + "readMeasuredMassConcentrationAndAmbientValues(): "); + errorToString(error, errorMessage, 256); + Serial.println(errorMessage); + return; + } - lcd.setCursor(0, 1); - lcd.print(" 4:"); - lcd.print(massConcentrationPm4p0); + ambientTemperature = celsiusToFahrenheit(ambientTemperature); + feelsLikeTemperature = heatIndex(ambientTemperature, ambientHumidity); + } - lcd.setCursor(8, 1); - lcd.print("10:"); - lcd.print(massConcentrationPm10p0); - } else { - lcd.setCursor(0, 0); - lcd.print("t:"); - lcd.print(round(ambientTemperature)); + if (currentTime - lcdTime >= 500) { + lcdTime = currentTime; - lcd.setCursor(8, 0); - lcd.print("h:"); - lcd.print(round(ambientHumidity)); + lcd.clear(); - lcd.setCursor(0, 1); - lcd.print("f:"); - lcd.print(round(feelsLikeTemperature)); + if (isStandardValues) { + lcd.setCursor(0, 0); + lcd.print("t:"); + lcd.print(round(ambientTemperature)); - lcd.setCursor(8, 1); - lcd.print("v:"); - lcd.print(vocIndex, 0); + lcd.setCursor(8, 0); + lcd.print("h:"); + lcd.print(round(ambientHumidity)); + + lcd.setCursor(0, 1); + lcd.print("f:"); + lcd.print(round(feelsLikeTemperature)); + + lcd.setCursor(8, 1); + lcd.print("v:"); + lcd.print(vocIndex, 0); + } else { + lcd.setCursor(0, 0); + lcd.print(" 1:"); + lcd.print(massConcentrationPm1p0); + + lcd.setCursor(8, 0); + lcd.print(" 2:"); + lcd.print(massConcentrationPm2p5); + + lcd.setCursor(0, 1); + lcd.print(" 4:"); + lcd.print(massConcentrationPm4p0); + + lcd.setCursor(8, 1); + lcd.print("10:"); + lcd.print(massConcentrationPm10p0); + } } }