Options to Multiple Loops on Arduino

Hi, I'm new to Arduino and am currently using it for my Final Year Project in my university. I proposed to come up with an Intelligent Car Park system where drivers can check for availability of parking berths in a car park wirelessly using their mobile devices.

I am now working on the programming on Arduino, and am faced with a situation. I currently have 2 PIR sensors in my circuit, and I wish to have them both work simultaneously. However, I can only get readings from either one of the sensors at one particular time in the Serial Monitor. I suspect it is because I put the coding for the functionality of both sensors in one loop as shown in the coding below.

void loop(){

     if(digitalRead(pirPin1) == HIGH){
       digitalWrite(ledPin1, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("Motion detected at Sensor 1 at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin1) == LOW){       
       digitalWrite(ledPin1, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("Motion ended at Sensor 1 at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
       if(digitalRead(pirPin2) == HIGH){
       digitalWrite(ledPin2, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("Motion detected at Sensor 2 at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin2) == LOW){       
       digitalWrite(ledPin2, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("Motion ended at Sensor 2 at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
       
  }

If both sensors detect motion at a particular time, eg. at 100 seconds, only readings from a sensor can be obtained on the Serial Monitor. I tried separating the coding for the functionality of each sensor into one loop in itself, but I realized Arduino programming only allows one loop in a program. Hence, I'd like to know if there is any way I can separate the coding, or if there is any way I can get readings from both sensors at one time.

Thank you in advance for your help :slight_smile:

There can be only one setup() and only one loop() in Arduino.

I think the problem with your code is you are sharing key state variables between the code that manages the two sensors, when those two code sections should be completely independent.

I'm not 100% sure, but I suspect you should use takeLowTime1 and takeLowTime2, lowIn1 and lowIn2, lockLow1 and lockLow2.

You have identical code for two sensors, so you have (at least, IMHO) two ways to improve your code.
Use a global array for each state variable (including the pin number), and identify the sensor by its index (i.e. 0 and 1), then encapsulate the code into a function that takes that index as its only argument. In loop(), you call that function twice, once with 0 then with 1 (or use a for() loop).
Or you could encapsulate both the state variables and the function in a class, and give the constructor at least one argument, the sensor pin number.
Then you would instantiate two objects and call each one's function inside loop().

HTH

Thank you very much for your reply. I tried using takeLowTime1 & takeLowTime2, lowIn1 and lowIn2, lockLow1 & lockLow2, and the problem seemed to be solved magically. Thanks :slight_smile:

ISFJ:
Thank you very much for your reply. I tried using takeLowTime1 & takeLowTime2, lowIn1 and lowIn2, lockLow1 & lockLow2, and the problem seemed to be solved magically. Thanks :slight_smile:

I'm glad to hear that.

The problem then was you had two state machines interfering with each other.

Now it's time to improve your code: de-duplicate it by using an array and only one function (just kidding) :slight_smile:

Now it's time to improve your code: de-duplicate it by using an array and only one function (just kidding) :slight_smile:

Wow this may prove to be a challenge to me since I'm still new to Arduino. But I'll try and let you know, thanks! :slight_smile:

         takeLowTime = true;

I just can't see how a variable called takeLowTime can meaningfully accept true or false. That name implies that it will hold a time value.