Multiple loops with different delays

Hi all, any idea why the below code doesn't work, 1 loop runs runs, the other loop doesn't appear to start.
Thank you.

void setup()
{
    Serial.begin(9600);       // for diagnostics
    pinMode(3, INPUT);        // switch is attached to Arduino pin 7
    pinMode(8, OUTPUT);        // LED pin 8
    RunPing();
    ReadSensor();
}

void RunPing()
{
    int var = 0;
    while(var < 1){
      Serial.println("in ping loop");
       delay(5000);
    }
}

void ReadSensor()
{
    int varx = 0;
    Serial.println("Reading A0");
    while(varx < 1){
     int SensorValue = analogRead(A0);
     Serial.println(SensorValue);
      delay(100);
    }
}

void loop()
{
 //main loop here
}

What in RunPing() increments var?

delay() stops execution of everything, not just one loop. It literally pauses the main thread for that duration. To pause loops separately, utilize the millis() and micros() functions, similar to how the BlinkWithoutDelay example does it. That will be a good read for this subject.

Hi thanks for the quick replies, I've found a ncie library that does exactly what I need.
http://playground.arduino.cc/Code/SimpleTimer

When you get cheesed off with the Library look at several things at a time

...R

Robin2:
When you get cheesed off with the Library look at several things at a time

...R

Noted! I've now switched to using the mills() function in the main loop. Much more efficient and less wasted memory, that particular timer module had quite a few lines of code, achieved what I needed in only a few lines :slight_smile: