Function for when two sensors detect something

I've been making a code for a traffic light system with two sensors, and I was trying to make a code for when the two sensors sense something at the same time, but whenever I make the sensors sense something at the same time, sometimes it doesn't work. The desired result is that if both sensor1 and sensor2 sense something, the code that happens when sensor1 senses something starts and then after that, the code for sensor2 starts immediately. Sometimes it works, but it is really unreliable. Is there any way I can make the results more consistent? Here's the code.

int sensorRed1 = 13;
int sensorRed2 = 12;
int g1 = 11;
int y1 = 10;
int r1 = 9;
int arrowg = 8;
int g3 = 7;
int y3 = 6;
int r3 = 5;
int g2 = 2;
int y2 = 3;
int r2 = 4;
int val = 0;
int val2 = 0;
int pirState1 = LOW;
int pirState2 = LOW;


void setup() {
  pinMode(g2, OUTPUT);
  pinMode(arrowg, OUTPUT);
  pinMode(g1, OUTPUT);
  pinMode(y1, OUTPUT);
  pinMode(r1, OUTPUT);
  pinMode(g3, OUTPUT);
  pinMode(y2, OUTPUT);
  pinMode(y3, OUTPUT);
  pinMode(r2, OUTPUT);
  pinMode(r3, OUTPUT);
  pinMode(sensorRed1, INPUT);
  pinMode(sensorRed2, INPUT);
  Serial.begin(9600);


}
void changeLightsJari() {
   digitalWrite(r1, HIGH);
   digitalWrite(g2, LOW);
   digitalWrite(g3, LOW);
   
   
  
   digitalWrite(y2, HIGH);
   digitalWrite(y3, HIGH);
   delay(4000);
   
    digitalWrite(y1, HIGH);
   digitalWrite(y2, LOW);
   digitalWrite(y3, LOW);
   digitalWrite(r1, LOW);
   digitalWrite(r2, HIGH);
   digitalWrite(r3, HIGH);
   delay(1000);
   
   digitalWrite(y1, LOW);
   digitalWrite(y2, LOW);
   digitalWrite(y3, LOW);
   digitalWrite(r1, LOW);
   digitalWrite(g1, HIGH);
   digitalWrite(r2, HIGH);
   digitalWrite(r3, HIGH);
   delay(9000);
   
   digitalWrite(g1, LOW);
   digitalWrite(r2, HIGH);
   digitalWrite(r3, HIGH);
   digitalWrite(y1, HIGH);
   delay(3000);
   
   digitalWrite(r1, HIGH);
   digitalWrite(g1, LOW);
   digitalWrite(r2, LOW);
   digitalWrite(r3, LOW);
   digitalWrite(y3, HIGH);
   digitalWrite(y2, HIGH);
   digitalWrite(y1, LOW);
   delay(1000);
   
   digitalWrite(y1, LOW);
   digitalWrite(y2, LOW);
   digitalWrite(y3, LOW);
   digitalWrite(r1, HIGH);
   digitalWrite(g2, HIGH);
   digitalWrite(g3, HIGH);


  if (pirState1 == LOW) {
    Serial.println("Motion detected!");
    pirState1 = HIGH;       // update variable state to HIGH
      digitalWrite(g2, HIGH);
      digitalWrite(g3, HIGH);
  
      
  }
  else {
     digitalWrite(r2, LOW);
      digitalWrite(r3, LOW);
      digitalWrite(y2, LOW);
      digitalWrite(y3, LOW);
      digitalWrite(g1, LOW);
      digitalWrite(y1, LOW);
      
    if (pirState1 == HIGH) {
      Serial.println("Motion stopped!");
      pirState1 = LOW;       // update variable state to LOW

    }
  }
}
void changeLightsMae() { //mae
 digitalWrite(g2,LOW);
  digitalWrite(g3,HIGH);
  digitalWrite(r1,HIGH);
  
   digitalWrite(g3, HIGH);
   digitalWrite(g2, LOW);
   digitalWrite(y2, HIGH);
   digitalWrite(r2, LOW);
   digitalWrite(r1,HIGH);
   delay(4000);
   
   digitalWrite(r2, HIGH);
   digitalWrite(y2, LOW);
   digitalWrite(arrowg, HIGH);
   digitalWrite(g3, HIGH);
   digitalWrite(r1,HIGH);
   delay(10000);
   
   digitalWrite(arrowg, LOW);
   digitalWrite(r2, LOW);
   digitalWrite(g3, HIGH);
   digitalWrite(y2, HIGH);
   digitalWrite(r1,HIGH);
   delay(4000);
   
   digitalWrite(y2, LOW);
   digitalWrite(g3, HIGH);
   digitalWrite(g2, HIGH);
   digitalWrite(r1,HIGH);

  if (pirState2 == LOW) {
    Serial.println("Motion detected!");
    pirState2 = HIGH;       // update variable state to HIGH
    digitalWrite(g2, HIGH);
    digitalWrite(g3, HIGH);
    digitalWrite(r1,HIGH);
} 
else {
     digitalWrite(arrowg, LOW);
    digitalWrite(r2, LOW);
    digitalWrite(r3, LOW);
    digitalWrite(y2, LOW);
    digitalWrite(y3, LOW);

    if (pirState2 == HIGH) {
      Serial.println("Motion stopped!");
      pirState2 = LOW;       // update variable state to LOW

    }
  }
}



void loop() {
  val2 =  digitalRead(sensorRed2);
  val = digitalRead(sensorRed1); //sa subroad to
  if ((val2 == HIGH) && (val == HIGH)) {
    digitalWrite(g2, HIGH);
    digitalWrite(g3, HIGH);
    delay(1000);
    
changeLightsMae();
    changeLightsJari();
    
    
    if ((pirState2 == LOW) && (pirState1 == LOW)) {
      Serial.println("Motion detectedboth!");
      ((pirState2 = HIGH) && (pirState1 = HIGH));

    }
  } else {
    if ((pirState2 == HIGH) && (pirState1 == HIGH)) {
      Serial.println("Motion endedBOTH!");
       ((pirState2 = LOW) && (pirState1 = LOW));

      }
    }
  val = digitalRead(sensorRed1); //sa subroad to
  if (val == HIGH) {
    digitalWrite(g2, HIGH);
    digitalWrite(g3, HIGH);
    delay(10000);
  
    changeLightsJari();
    delay(1000);
    
  if (pirState1 == LOW) {
    Serial.println("Motion detected!");
    pirState1 = HIGH;

  }} else {
    if (pirState1 == HIGH) {
      Serial.println("Motion ended!");
      pirState1 = LOW;

    }
  }
  
  val2 = digitalRead(sensorRed2); //sa subroad to
  if (val2 == HIGH) {
    digitalWrite(g2, HIGH);
    digitalWrite(g3, HIGH);
    
  
  changeLightsMae();
    
    
  if (pirState2 == LOW) {
    Serial.println("Motion detected2!");
    pirState2 = HIGH;

  }} else {
    if (pirState2 == HIGH) {
      Serial.println("Motion ended2!");
      pirState2 = LOW;

    }
  }
}}

//;3

I suspect all the delay()s in your program are the problem.

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

Remove excess (dead) code.

Why do you query the same sensors multiple times in loop()? You better check the sensors once, then determine their state, and finally do whatever is required for the current traffic light state.

I miss any traffic light state values in your code, did you ever look at state machines?

Your main loop should look something like this...

void loop() {
  readSensors();
  doCalculations();
  setOutputs();
}

There should be no delays inside any of those functions. doCalculations() should look at the sensor inputs and the time since it last changed the lights to make a decision of whether it should change anything on this particular iteration of loop().

DrDiettrich:
Remove excess (dead) code.

Why do you query the same sensors multiple times in loop()? You better check the sensors once, then determine their state, and finally do whatever is required for the current traffic light state.

I miss any traffic light state values in your code, did you ever look at state machines?

Can you explain this in Layman's terms, I'm afraid I don't understand what query is.

query sensor = read status of sensor. Hint: use Google!