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