I'm very new to Arduino and I'm trying to determine the best(/least bulky) way to smooth the data on this basic sketch. I'd like to use an average of 4 parts. Here's the code:
int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
void setup()
{
Serial.begin(9600);
pinMode(pin,INPUT);
starttime = millis();
}
void loop()
{
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)
{
ratio = lowpulseoccupancy/(sampletime_ms*10.0);
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62;
Serial.print(millis());
Serial.print("\t");
Serial.print(concentration);
Serial.println("");
lowpulseoccupancy = 0;
starttime = millis();
}
}
I read the tutorial, and have seen a few other codes on different pages, but a lot of them seemed bulky, or were specific to certain sensors. I was hoping to just get a suggestion of the most efficient way to do this- is it introducing 4 concentration variables and dividing them? Or is there something I can do that's more clever?
Thank you in advance for your time. I know this is probably a silly question, but as I said, I'm very new!