#include<M5StickC.h> #define pin 26
unsignedlongduration;unsignedlongstarttime;unsignedlongsampletime_ms=30000;unsignedlonglowpulseoccupancy=0;floatratio=0;floatconcentration=0;voidsetup(){M5.begin();M5.Lcd.setRotation(1);M5.Lcd.setTextSize(2);pinMode(pin,INPUT);starttime=millis();//get the current time;
Serial.print("start");}// the loop routine runs over and over again forever
voidloop(){duration=pulseIn(pin,LOW);lowpulseoccupancy=lowpulseoccupancy+duration;if((millis()-starttime)>sampletime_ms)//if the sampel time == 30s
{ratio=lowpulseoccupancy/(sampletime_ms*10.0);// Integer percentage 0=>100
concentration=1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62;// using spec sheet curve
M5.Lcd.fillScreen(TFT_BLACK);M5.Lcd.setCursor(25,20);M5.Lcd.printf("%4.2f pcs",concentration);M5.Lcd.setCursor(25,40);M5.Lcd.printf("/0.01cf",concentration);lowpulseoccupancy=0;starttime=millis();}}