#include <math.h>
|
|
|
|
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<toplimit; x++){
|
|
sum = sum + airreading[x];
|
|
|
|
}
|
|
average = sum / toplimit;
|
|
Serial.print("Average Get!: ");
|
|
Serial.println(average); //average when 0 is 65535, so it's invalid (dividing by zero - sum divided by top limit which are both zero)
|
|
//but we don't even want the average. It's just for testing here.
|
|
Serial.print("Sum Get!: ");
|
|
Serial.println(sum); //this is what we care about
|
|
|
|
//reset everything after a minute
|
|
sum = 0;
|
|
average = 0;
|
|
x = 0;
|
|
toplimit = 0;
|
|
for (x=0; x<toplimit; x++){
|
|
airreading[x] = 0;
|
|
}
|
|
starttime = millis();
|
|
}
|
|
}
|