How can I make my train signal restart when interrupted ?

Hello all, I currently have a signal set up on my train layout, It can do it what it needs to do,
as in stay green, go red when it is interrupted via IR sensor, switch to yellow then green.

My question is, How do i re-work the code so that it can get always go back to red when ever the signal is cut, instead of having to wait for the delay?

For example, train comes in, signal is cut, signal goes to red, stays red then goes to yellow. If i try to cut the signal at any point, it stays yellow for a fixed amount of time before going to red again. How can i fix it so that when ever it is interrupted it will go to red no matter what and restart the timer to yellow then green?

Thanks for any help

int green = 2; // green
int yellow = 3; // yellow
int red = 4; //red
int isObstaclePin = 9;  //  input pin from IR sensor
int isObstacle = HIGH;  // HIGH MEANS NO OBSTACLE

void setup() {
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(isObstaclePin, INPUT);
  Serial.begin(9600);  
}

void loop() {
  isObstacle = digitalRead(isObstaclePin);
  if (isObstacle == HIGH)
  {
    digitalWrite(green, HIGH);
    delay(10);

  }
  else
  {
    Serial.println("clear");
    digitalWrite(green, LOW);
    digitalWrite(red, HIGH);
    delay(10000);
    digitalWrite(red, LOW);
    delay(10);
    digitalWrite(yellow, HIGH);
    delay(10000);
    digitalWrite(yellow, LOW);
    delay(10);
    
  }
}

How do i re-work the code so that it can get always go back to red when ever the signal is cut, instead of having to wait for the delay?

Don't use delay()

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

By using millis() for timing you don't need to wait for the timing period to finish befor you do something else as you do when using delay(0

UKHeliBob:
Don't use delay()

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

By using millis() for timing you don't need to wait for the timing period to finish befor you do something else as you do when using delay(0

millis() looks like a function that keeps adding time, can i just define millis() with a pre-set time (5000 = 5 seconds?) and replace delay() with it?

When you read the URLs offered, you will understand.