simulate parallelism

This commit is contained in:
2023-04-11 18:26:24 -07:00
parent 6d7d817d60
commit 2da8860215

86
aqi.ino
View File

@@ -8,6 +8,26 @@ SensirionI2CSen44 sen44;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int buttonPin = 6; 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 heatIndex(float t, float rh) {
float hi = 0.0; float hi = 0.0;
if (t <= 40.0) if (t <= 40.0)
@@ -32,6 +52,7 @@ float heatIndex(float t, float rh) {
return hi; return hi;
} }
// convert celcius to fahrenheit
float celsiusToFahrenheit(float degrees) { float celsiusToFahrenheit(float degrees) {
return degrees * 1.8 + 32; return degrees * 1.8 + 32;
} }
@@ -75,21 +96,25 @@ void setup() {
} }
void loop() { void loop() {
currentTime = millis();
delay(20);
if (currentTime - buttonTime >= 100) {
buttonTime = currentTime;
if (digitalRead(buttonPin) == LOW) {
isStandardValues = !isStandardValues;
}
}
if (currentTime - sen44Time >= 1000) {
sen44Time = currentTime;
uint16_t error; uint16_t error;
char errorMessage[256]; char errorMessage[256];
delay(1000);
// Read Measurement // Read Measurement
uint16_t massConcentrationPm1p0;
uint16_t massConcentrationPm2p5;
uint16_t massConcentrationPm4p0;
uint16_t massConcentrationPm10p0;
float vocIndex;
float ambientHumidity;
float ambientTemperature;
float feelsLikeTemperature;
error = sen44.readMeasuredMassConcentrationAndAmbientValues( error = sen44.readMeasuredMassConcentrationAndAmbientValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, vocIndex, ambientHumidity, ambientTemperature); massConcentrationPm10p0, vocIndex, ambientHumidity, ambientTemperature);
@@ -104,26 +129,14 @@ void loop() {
ambientTemperature = celsiusToFahrenheit(ambientTemperature); ambientTemperature = celsiusToFahrenheit(ambientTemperature);
feelsLikeTemperature = heatIndex(ambientTemperature, ambientHumidity); feelsLikeTemperature = heatIndex(ambientTemperature, ambientHumidity);
}
if (currentTime - lcdTime >= 500) {
lcdTime = currentTime;
lcd.clear(); lcd.clear();
if (digitalRead(buttonPin) == LOW) { if (isStandardValues) {
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);
} else {
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print("t:"); lcd.print("t:");
lcd.print(round(ambientTemperature)); lcd.print(round(ambientTemperature));
@@ -139,5 +152,22 @@ void loop() {
lcd.setCursor(8, 1); lcd.setCursor(8, 1);
lcd.print("v:"); lcd.print("v:");
lcd.print(vocIndex, 0); 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);
}
} }
} }