diff --git a/Air_Quality_Sensor_PPD42/code/arduinomega_code/arduinomega_code.ino b/Air_Quality_Sensor_PPD42/code/arduinomega_code/arduinomega_code.ino new file mode 100644 index 0000000..89225b4 --- /dev/null +++ b/Air_Quality_Sensor_PPD42/code/arduinomega_code/arduinomega_code.ino @@ -0,0 +1,93 @@ +#include + +int DustSensePin = 13; + +//let's start with 100 readings. (copy and paste) +unsigned int airreading[100] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; +unsigned long starttime = 0; +unsigned long sampletime_ms = 60000;//sample 60s == 60000 ; +unsigned long lowpulseoccupancy = 0; +float ratio = 0; +float concentration = 0; +unsigned int x = 0; +unsigned int toplimit = 0; +unsigned long sum = 0; //highest number possible - 32 bit unsigned, so 4 billion +unsigned int average = 0; + + +void setup() +{ + Serial.begin(9600); + Serial.println("Starting Air Sensor, please wait for readings..."); + pinMode(DustSensePin,INPUT); + starttime = millis();//get the current time; +} + +void loop() +{ + + airreading[x] = pulseIn(DustSensePin, LOW, 500000); + + /*lowpulseoccupancy = lowpulseoccupancy+duration; + + if ((millis()-starttime) > sampletime_ms)//if the sample time == 30s + { + ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100 + concentration = 1.1*powf(ratio,3)-3.8*powf(ratio,2)+520*ratio+0.62; // using spec sheet curve + Serial.print(lowpulseoccupancy); + Serial.print(","); + Serial.print(ratio); + Serial.print(","); + Serial.println(concentration); + lowpulseoccupancy = 0; + starttime = millis(); + } + */ + //if ((millis()-starttime) > sampletime_ms){ + + //KISS. Relative readings only. + //Something else keeps the time. + if (airreading[x] != 0){ + Serial.println(airreading[x]); + x=x+1; + if(x>100){ + x = 0; + } + } + + //starttime = millis(); + //} + + + //KISS. Average for a minute, and store that data in a webpage (that part will be in ethernet sensor test 3). + // + if ((millis()-starttime) > sampletime_ms){ + toplimit = x; // don't read the zeros + + + if(toplimit == 0){ + toplimit = 1; //to avoid divide by zero, in case sum / toplimit is both zero + } + + for (x=0; x // Core graphics library +#include // Hardware-specific library +MCUFRIEND_kbv tft; + +#include +#include +#include + +#include + +#define BLACK 0x0000 +#define RED 0xF800 +#define GREEN 0x07E0 +#define WHITE 0xFFFF +#define GREY 0x8410 + +#include + +int DustSensePin = 13; + +//globals for ppd42 dust sensor +//let's start with 100 readings. (copy and paste) +unsigned int airreading[100] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; +unsigned long starttime = 0; +unsigned long sampletime_ms = 60000;//sample 60s == 60000 ; +unsigned long lowpulseoccupancy = 0; +float ratio = 0; +float concentration = 0; +unsigned int x = 0; +unsigned int toplimit = 0; +unsigned long sum = 0; //highest number possible - 32 bit unsigned, so 4 billion +unsigned int average = 0; +//end ppd42 + + +int brightnessread = 0; + +int cat = 0; +int resultsread1 = 0; + +void setup(void) +{ + Serial.begin(9600); + uint16_t ID = tft.readID(); + if (ID == 0xD3) ID = 0x9481; + tft.begin(ID); + tft.setRotation(1); + tft.WriteCmdData(0x51, 0x00); + //resultsread1 = tft.readReg(0x52); + //writeData(0x51); + pinMode(DustSensePin,INPUT); + starttime = millis();//get the current time; + + +} + +void loop(void) +{ + + + + + tft.fillScreen(BLACK); + tft.WriteCmdData(0x51, 0x0000); + tft.setTextColor(0XFFFF); + tft.setTextSize(2); + tft.setCursor(0, 36); + tft.println("running sensor now"); + + + airreading[x] = pulseIn(DustSensePin, LOW, 500000); + tft.println("Results are in:"); + //tft.println(airreading) + if ((millis()-starttime) > sampletime_ms)//if the sample time == 30s + { + ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100 + concentration = 1.1*powf(ratio,3)-3.8*powf(ratio,2)+520*ratio+0.62; // using spec sheet curve + tft.println(lowpulseoccupancy); + //tft.print(","); + tft.println(ratio); + //Serial.print(","); + tft.println(concentration); + lowpulseoccupancy = 0; + starttime = millis(); + } + + +} + +void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg) +{ + int16_t x1, y1; + uint16_t wid, ht; + tft.drawFastHLine(0, y, tft.width(), WHITE); + tft.setFont(f); + tft.setCursor(x, y); + tft.setTextColor(GREEN); + tft.setTextSize(sz); + tft.print(msg); + delay(1000); +} + + +uint32_t readReg32(uint16_t reg) +{ + uint16_t h = tft.readReg(reg, 0); + uint16_t l = tft.readReg(reg, 1); + return ((uint32_t) h << 16) | (l); +}