Routines not working independently??

Hello world,

Here's the setup:

There's 4 PIR motion detectors, a servo turning 360 continuously and a rotary encoder connected to the motor. If a sensor is triggered it lights an LED (same one for all four, just to test) and the motor stops when it reaches a different encoder position for each sensor, then carries on when the sensor pin returns to LOW. Right now sensor 1 and 2 work fine and independently, but sensors 3 and 4, although they light the LED, do not stop the motor - UNLESS sensor 1 is also triggered at the same time. I thought this might just be a bracket thing, but they all look fine. I've stared at this for hours and can't work it out, any help much appreciated.

Here's the loop:

void loop() {

//sensor 1 (12 o clock)-----------------------------
val1 = digitalRead(PIR1Pin); // read input value and store it in val
if (val1 == LOW) { // check the sensor
digitalWrite(ledPin, LOW); // turn LED off
}
if (val1 == HIGH) { // check the sensor
digitalWrite(ledPin, HIGH); // turn LED on
}

if (val1 == HIGH){ //sensor triggered
while ( (encoder0Pos == 12) && (digitalRead(PIR1Pin) == HIGH) ) //when encoder reaches 12...
pulseWidth = centerServo; //stop motor
}

if (val1 == LOW){ //sensor LOW
pulseWidth = 1540;} //carry on

//sensor 1 end-----------------------------

//sensor 2 (3 o clock)-----------------------------
val2 = digitalRead(PIR2Pin); // read input value and store it in val
if (val2 == LOW) { // check the sensor
digitalWrite(ledPin, LOW); // turn LED off
}
if (val2 == HIGH) { // check the sensor
digitalWrite(ledPin, HIGH); // turn LED on
}

if (val2 == HIGH){ //sensor triggered
while ( (encoder0Pos == 3) && (digitalRead(PIR2Pin) == HIGH) ) //when encoder reaches 3...
pulseWidth = centerServo; //stop motor
}

if (val2 == LOW){ //sensor LOW
pulseWidth = 1540;} //carry on

//sensor 2 end-----------------------------

//sensor 3 (6 o clock)-----------------------------
val3 = digitalRead(PIR3Pin); // read input value and store it in val
if (val3 == LOW) { // check the sensor
digitalWrite(ledPin, LOW); // turn LED off
}
if (val3 == HIGH) { // check the sensor
digitalWrite(ledPin, HIGH); // turn LED on
}

if (val3 == HIGH){ //sensor triggered
while ( (encoder0Pos == 6) && (digitalRead(PIR1Pin) == HIGH) ) //when encoder reaches 6...
pulseWidth = centerServo; //stop motor
}

if (val3 == LOW){ //sensor LOW
pulseWidth = 1540;} //carry on

//sensor 3 end-----------------------------

//sensor 4 (9 o clock)-----------------------------
val4 = digitalRead(PIR4Pin); // read input value and store it in val
if (val4 == LOW) { // check the sensor
digitalWrite(ledPin, LOW); // turn LED off
}
if (val4 == HIGH) { // check the sensor
digitalWrite(ledPin, HIGH); // turn LED on
}

if (val4 == HIGH){ //sensor triggered
while ( (encoder0Pos == 9) && (digitalRead(PIR1Pin) == HIGH) ) //when encoder reaches 9...
pulseWidth = centerServo; //stop motor
}

if (val4 == LOW){ //sensor LOW
pulseWidth = 1540;} //carry on

//sensor 4 end-----------------------------

// pulse the servo every 20 ms (refreshTime) with current pulseWidth
// this will hold the servo's position if unchanged, or move it if changed
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // start the pulse
delayMicroseconds(pulseWidth); // pulse width
digitalWrite(servoPin, LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse
}

}

while ( (encoder0Pos == 12) && (digitalRead(PIR1Pin) == HIGH) )   //when encoder reaches 12... 
...
while ( (encoder0Pos == 3) && (digitalRead(PIR2Pin) == HIGH) )   //when encoder reaches 3... 
...
while ( (encoder0Pos == 6) && (digitalRead(PIR1Pin) == HIGH) )   //when encoder reaches 6... 
...
while ( (encoder0Pos == 9) && (digitalRead(PIR1Pin) == HIGH) )   //when encoder reaches 9...

Looks like the last two should be PIR3Pin and PIR4Pin.

Ha Ha, thanks for pointing out my glaring error. Must have got caught up somewhere else!