Last active
March 26, 2019 15:27
-
-
Save Dvorson/51f53c2219ae75d387dd95bf892acd24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <U8g2lib.h> | |
#define PM10PIN 3 | |
#define PM25PIN 6 | |
#define ALARMSOUNDER 8 | |
int i; | |
unsigned long duration10; | |
unsigned long duration25; | |
unsigned long starttime; | |
unsigned long endtime; | |
unsigned long sampletime_ms = 30000; | |
unsigned long lowpulseoccupancy10 = 0; | |
unsigned long lowpulseoccupancy25 = 0; | |
float ratio10 = 0; | |
float ratio25 = 0; | |
float concentration10 = 0; | |
float concentration25 = 0; | |
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ SCL, /* data=*/ SDA); | |
void playAlarm() { | |
for(i = 0; i < 255; i = i + 2) { | |
tone(ALARMSOUNDER, i*50); | |
delay(5); | |
} | |
for(i = 255; i > 1; i = i - 2) { | |
tone(ALARMSOUNDER, i*50); | |
delay(5); | |
} | |
noTone(ALARMSOUNDER); | |
} | |
void setup() { | |
Serial.begin(115200); | |
u8g2.begin(); | |
u8g2.setFontMode(1); | |
u8g2.setFont(u8g2_font_profont15_mf); | |
pinMode(PM10PIN,INPUT); | |
pinMode(PM25PIN,INPUT); | |
pinMode(ALARMSOUNDER, OUTPUT); | |
starttime = millis(); | |
} | |
void loop() { | |
duration10 = pulseIn(PM10PIN, LOW); | |
duration25 = pulseIn(PM25PIN, LOW); | |
if (10 < duration10 < 90) lowpulseoccupancy10 += duration10; | |
if (10 < duration25 < 90) lowpulseoccupancy25 += duration25; | |
endtime = millis(); | |
if ((endtime-starttime) > sampletime_ms) { | |
ratio10 = (lowpulseoccupancy10-endtime+starttime + sampletime_ms)/(sampletime_ms*10.0); // Integer percentage 0=>100 | |
ratio25 = (lowpulseoccupancy25-endtime+starttime + sampletime_ms)/(sampletime_ms*10.0); // Integer percentage 0=>100 | |
concentration10 = 1.1*pow(ratio10,3)-3.8*pow(ratio10,2)+520*ratio10+0.62; // using spec sheet curve | |
concentration25 = 1.1*pow(ratio25,3)-3.8*pow(ratio25,2)+520*ratio25+0.62; // using spec sheet curve | |
u8g2.setCursor(0,15); | |
u8g2.print("PM1.0: "); | |
if (lowpulseoccupancy10 != 0) { | |
u8g2.print(String(concentration10) + "ppm"); | |
} else { | |
u8g2.print(String(0)); | |
} | |
u8g2.setCursor(0,32); | |
u8g2.print("PM2.5: "); | |
if (lowpulseoccupancy25 != 0) { | |
u8g2.print(String(concentration25) + "ppm"); | |
} else { | |
u8g2.print(String(0)); | |
} | |
u8g2.sendBuffer(); | |
u8g2.clearBuffer(); | |
if ((lowpulseoccupancy10 != 0 && lowpulseoccupancy25 !=0) && (concentration10 > 5000 || concentration25 > 5000)) playAlarm(); | |
lowpulseoccupancy10 = 0; | |
lowpulseoccupancy25 = 0; | |
starttime = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment