keeping the Arduino in a function for X time

So here is a rough example. I want the arduino to run sensor1 for 5 seconds before coming back and moving to sensor2.

Mostly loop through sensor1. It requires ms 5000 to get a proper reading.

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

Have you tried the blink without delay technique?

You could use delay(5000) but you cannot do much else during the 5 second delay time.

Alternatively, you could use millis(). In the long run, it is probably better to learn to use millis() (because it does not prevent you from doing other things) and avoid delay(...). See Using millis() for timing. A beginner's guide.

Ill have to read on blink

I want it to run the internal code during this process.

I thought delay stays on that line for the given time.

grio:
I want the arduino to run sensor1 for 5 seconds before coming back and moving to sensor2.

what kind of sensor?

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.

unsigned long startCollecting = millis();
while(millis() - startCollecting < collectionTime)
{
   // get some data from the sensor
   // do something with the data
}

That will spin, doing useful stuff for however long collectionTime is set to.

grio:
I thought delay stays on that line for the given time.

You thought correctly. That is why the Blink Without Delay (BWOD) technique uses millis() and is preferable in this case.