Interrupt Help with Traffic Light Intersection Project

I am trying to make a realistic intersection with traffic lights. red1, yellow1, and green1 are the lights for road #1 and red2, yellow2, and green2 are the lights for road #2. switch1 is for road #1 emergencies, so if the switch is clicked, green1 and red2 should be high for 5 seconds. Then it should go to the BEGINNING of the loop() function.

The interrupt does half it's job, turning green1 and red2 HIGH and turning the other lights LOW, but the problem is that the delay() function doesn't run so it goes straight to where the loop() function left it off.

What I want is for the delay() function to run (Including in the loop() function), and the loop() function to start from the beginning after the interrupt does it's job.

What I want is either an alternative for interrupt, or some other way which can make my code work, which is provided below. I would really appreciate it if someone fixed my code with an example.

Thanks!!!

volatile int red1 = 13;
volatile int yellow1 = 12;
volatile int green1 = 11;
volatile int red2 = 10;
volatile int yellow2 = 9;
volatile int green2 = 8;

int switch1 = 2;                                             
int switch2 = 3;

void setup() {
  pinMode(red1, OUTPUT);
  pinMode(yellow1, OUTPUT);
  pinMode(green1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(yellow2, OUTPUT);
  pinMode(green2, OUTPUT);

  pinMode(switch1, INPUT);
  pinMode(switch2, INPUT);
  attachInterrupt(digitalPinToInterrupt(switch1), Lane1E, CHANGE);
}

void Lane1E () {
  digitalWrite(green1, HIGH);
  digitalWrite(red2, HIGH);
  digitalWrite(green2, LOW);
  digitalWrite(red1, LOW);
  digitalWrite(yellow1, LOW);
  digitalWrite(yellow2, LOW);
  delay(5000);
  loop();
}

void loop() {
  digitalWrite(green1, HIGH);
  digitalWrite(red2, HIGH); 
  delay(15000);  
  digitalWrite(green1, LOW);    
  digitalWrite(yellow1, HIGH);   
  delay(2500); 
  digitalWrite(yellow1, LOW);    
  digitalWrite(red1, HIGH);   
  delay(1500);  
  digitalWrite(red1, LOW);
  digitalWrite(red2, LOW);

  digitalWrite(green2, HIGH);
  digitalWrite(red1, HIGH);  
  delay(15000);  
  digitalWrite(green2, LOW);                  
  digitalWrite(yellow2, HIGH);   
  delay(2500); 
  digitalWrite(yellow2, LOW);     
  digitalWrite(red2, HIGH);   
  delay(1500);  
  digitalWrite(red2, LOW);
  digitalWrite(red1, LOW);
}

Why have you declared your variables as volatile?

I have been looking around the internet and I see the most coders set their variables as volatile. I am really just a beginner so I don't know much coding.

Really? Did you look up "volatile" to see what it does?

In which of the example sketches that are included in the Arduino IDE, can you find this:

 loop();

inside of an ISR?

Get rid of all the calls to delay(), implement a state machine and the need for interrupts disappears.

Nope. But my code does the same thing if I remove volatile.

Rasbir16:
But my code does the same thing if I remove volatile.

Just a bit slower.

AWOL, can you type up an example for me. I would greatly appreciate it if you do.

Rasbir16:
AWOL, can you type up an example for me. I would greatly appreciate it if you do.

I'm sure you would. If you google "Arduino traffic lights" you will save everyone a lot of time.

There are plenty of examples and tutorials online. Have a look at Robin2's excellent demo as a first stab

Is it possible if you can provide a much more simpler example? I have already told you that I am a beginner. But thanks anyway.

Rasbir16:
Is it possible if you can provide a much more simpler example? I have already told you that I am a beginner. But thanks anyway.

Most of the examples that you can google are extremely simple. They are just linear programs with delay() statements to control the timing.

You are attemping to add a feature that will complicate that. You shouldn't be surprised that it will make the program more complicated. If you can't understand something, you need to back down and try some simpler projects first.

Nobody is going to "fix your code" for free. Not when it's already been done and posted online so many times before. Unless you are designing actual traffic lights, it is an academic exercise and you will learn nothing from having someone else do it for you.

It was my mistake posting this on the forum.

Rasbir16:
It was my mistake posting this on the forum.

No, your mistake was not following up on the advice you were given.

Rasbir16:
It was my mistake posting this on the forum.

Going to another Forum is not going to make your problem any easier.

You just need to spend a few hours studying the advice you have already received.

A great deal of what you need is in the link in Reply #9. You may also like to look at Planning and Implementing a Program

...R