interrupt

hi everybody, I have school project to do motion sensor traffic lights using arduino I am done with the code which there is a main loop running and I connect the motion sensor to interrupt. my problem is the main loop is running and when an interrupt happen the interrupt function happen after that the program go back to the same point it left when the interrupt happened and this is my problem I want the main loop to start over again how to make that happen... plz help thanks in advance

That is how it is supposed to work. An interrupt happens and then the code picks up where it left off.

Why would you use an interrupt to read a motion sensor? How fast are the cars going? Can they cross the sensor in less than a millisecond?

Post your code and perhaps someone can help you figure out a better way to organize it so it does what you want.

Common misunderstanding of what an "interrupt" is on a computer.

You say you want "the main loop to start over again" when something happens. Actually, you do not mean that!

What you mean is that you want the main loop to behave differently when a particular event happens. So you need to code your main loop so that it monitors or "polls" for that event and alters its behaviour in a certain specified manner.

You have almost certainly not organised your "main loop" properly and do not understand the concept of it. I venture to say that if you post your actual present code (using the "code" tags generated by the first icon in the "widget" list above the submission window), we can help you sort it out.

thank you all for the reply i change my approach by using if statement but the problem is give me lights not in the same order

int calibrationTime = 30;
int PIR=8; 
int PIR1=9;
int grn=2; 
int ylw=3; 
int red=4;
int grn1=5; 
int ylw1=6; 
int red1=7; 
int grn2=10; 
int ylw2=11; 
int red2=12; 
int stat; 
int stat1; 
int i=0;




void setup(){
  pinMode(PIR,INPUT); 
  pinMode(PIR1,INPUT);
  pinMode(grn,OUTPUT);
  pinMode(ylw,OUTPUT);
  pinMode(red,OUTPUT);
 
  pinMode(grn1,OUTPUT);
  pinMode(ylw1,OUTPUT);
  pinMode(red1,OUTPUT);
  pinMode(grn2,OUTPUT);
  pinMode(ylw2,OUTPUT);
  pinMode(red2,OUTPUT);
  delay(2500); 
    for(int i = 0; i < calibrationTime; i++)
    {
           delay(100);
      }
  
   
}
void loop()
{
  stat =digitalRead(PIR);         //READ SENSOR 1
  if(stat == LOW)
  {
    
    
    
     digitalWrite(red,LOW); 
       digitalWrite(ylw,LOW);           //TRAFFIC LIGHT ONE
      digitalWrite(grn,HIGH); 
      
  digitalWrite(red1,HIGH);
     digitalWrite(ylw1,LOW);                //TRAFFIC LIGHT TWO
       digitalWrite(grn1,LOW); 
 
  
  
  
    digitalWrite(red2,HIGH);    
    digitalWrite(ylw2,LOW);                    //TRAFFIC LIGHT THREE
    digitalWrite(grn2,LOW); 
  
  }
  
 if(stat == HIGH)
  {
     digitalWrite(red,HIGH); 
     digitalWrite(ylw,LOW);
  digitalWrite(grn,LOW); 
  
  digitalWrite(red1,LOW);
    digitalWrite(ylw1,LOW);
      digitalWrite(grn1,HIGH); 
  

    digitalWrite(red2,HIGH);
    digitalWrite(grn2,LOW); 
    digitalWrite(ylw2,LOW);
  }
    
   stat1 =digitalRead(PIR1);  
   
   if(stat1 == LOW)
   {
    digitalWrite(red,LOW); 
       digitalWrite(ylw,LOW);
      digitalWrite(grn,HIGH); 
      
  digitalWrite(red1,HIGH);
     digitalWrite(ylw1,LOW); 
       digitalWrite(grn1,LOW); 
 
  
  
  
    digitalWrite(red2,HIGH);    
    digitalWrite(ylw2,LOW); 
    digitalWrite(grn2,LOW); 
  
   }
   
   if(stat1 == HIGH)
   
   {
     
     digitalWrite(red,HIGH); 
     digitalWrite(ylw,LOW);
  digitalWrite(grn,LOW); 
  
  digitalWrite(red1,HIGH);
    digitalWrite(ylw1,LOW);
      digitalWrite(grn1,LOW); 
  

    digitalWrite(red2,LOW);
     digitalWrite(ylw2,LOW);
     digitalWrite(grn2,HIGH);
     
     
   }
     
   
}

Same order as what?

There is no sequence about that code the lights will just be at a set pattern depending on the result of the two sensors. Remember that the loop function runs many thousand of times a second.

These lines do nothing useful and can be removed completely.

delay(2500); 
    for(int i = 0; i < calibrationTime; i++)
    {
           delay(100);
      }

You need to do a little thinking before you jump in with your code. How does a real traffic light system work?

It needs to “know” the state of the system at any instant. For example

traffic flowing East-West
stopping East-West traffic
starting North-South traffic
traffic flowing North-South
stopping North-South traffic
starting East-West traffic
repeat

Then you need a timing process that decides when the state should change
Maybe some of the state changes are a response to buttons - I have not bothered with pedestrian lights.
And then you need functions that are called to make the ncessary lights go ON or OFF for each state.

Have a look at planning and implementing a program - it should illustrate most of what you need.

And PLEASE use the auto-format option to tidy up the indentation of your code to make it easy to read.

…R

thank you for the quick answer and sorry very new to this I will check the link

I just need to know how 2 sensors reading can change the same output

No you don’t you need to know what you need to do and then that problem will drop out.

genx1988: I just need to know how 2 sensors reading can change the same output

Look at your code after if(stat1 == HIGH) You are overwriting the outputs for both sets of LEDs. Same for if(stat...

DrDiettrich: Look at your code after if(stat1 == HIGH) You are overwriting the outputs for both sets of LEDs. Same for if(stat...

it should be the same I have one main road which green light should be there when the sensors aren't detecting anything

genx1988: it should be the same I have one main road which green light should be there when the sensors aren't detecting anything

And now maybe with some punctuation?

genx1988: it should be the same I have one main road which green light should be there when the sensors aren't detecting anything

Then add another test: if (stat==HIGH && stat1==HIGH)

DrDiettrich:
Then add another test:
if (stat==HIGH && stat1==HIGH)

thank u so much going to do it