keeping the Arduino in a function for X time

It is a dust sensor.

It is counting over a period of time to give me the participial count in the air down to 1 micron. Also calculates how much air has moved through the sensor.

Now that I am reading about millis I might have missunderstood but I need to change up this code a little more to make it work how I want it to work.

int pin = 22;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 5000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;


void setup() {
 Serial.begin(9600);
 pinMode(22,INPUT);
 starttime = millis();
}


void loop() {
 sensor1();
 sensor1();
}





void sensor1()

{
 duration = pulseIn(pin, LOW);
 lowpulseoccupancy = lowpulseoccupancy+duration;


 if ((millis()-starttime) > sampletime_ms)
 {
   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
   //Serial.print(lowpulseoccupancy);
   // Serial.print(",");
   Serial.print(ratio);
   Serial.print(",");
   Serial.println(concentration);
   lowpulseoccupancy = 0;
   starttime = millis();
 }
}

dsm501 is the sensor name. Not a lot of info on it on the web.

I am thinking a dowhile is order to get this correct. Trying to figure out how to incorporate it right now.