Arduino interrupts

I have an Arduino Uno. It has two interrupts; int-0(pin 2) and int-1(pin 3). I attached a handler to interrupt 0 and a different one to interrupt 1, using attachInterrupt().

if Interrupt 0 is triggered and it calls its handler,If interrupt 0's handler is still executing when interrupt 1 is triggered, what will happen?

Will interrupt 1 interrupt interrupt 0, or will interrupt 1 wait until interrupt 0's handler is done executing?????

i am doing a small project to count the no.of persons getting IN and OUT of the single door(Assume like a bus door with steps).
this is my code..

// program to count the no.of persons in the room or in bus

 int b1 = 2;  //sensor1 for boarding checking
 int a1 = 3;  //sensor2 for boarding checking
 int b2 = 4;  //sensor3 for alighting checking
 int a2 = 5;  //sensor4 for alighting checking
 volatile int count = 0; //variable to store the count
 
void setup()
{

  pinMode(b1,INPUT);
  pinMode(b2,INPUT);
  pinMode(a1,INPUT);
  pinMode(a2,INPUT);
   
  attachInterrupt(0, Board, LOW);
  attachInterrupt(1, Alight, LOW);
  
  Serial.begin(9600);
  Serial.println("The occupancy Module is ready");
  
}

void loop()
{
  
}

void Board()
{
  for(int i=0; i < 1500; i++)
  {
    if(digitalRead(b2) == LOW)
    {
      count++;
      Serial.print("count = ");
      Serial.println(count);
    }
  }

}

void Alight()
{
  for(int j=0; j< 1500; j++)
  {
   if(digitalRead(a2) == LOW)
   {
     count--; 
     Serial.print("count = ");
     Serial.println(count);
   }
  }
}

i wrote a simple code using 4 sensors, and i am using the combination of 2 sensors to make sure the person has get IN or OUT
b1,b2 are for counting the boarded persons,
a1,a2 are for counting the alighted persons,

a1,b1, sensors i am using as interrupt purpose and in the sub routine i am checking for a2 & b2 respectively
i think this code will work for single person boarding and alighting cases..

i have some doubts about this code...
when we are considering the public buses
-> what if two persons (1 boarding and another alighting at same time) passing through the door???
-> how the ISR's will behave????

any one reading post please help me out , give any suggestions to improve my code and which others cases i need to take into consideration...

thanks in advance...

The Arduino can only process one interrupt at a time, and whichever one comes first gets processed first. The second will be processed afterwards (delayed).

Programming interrupt handlers (ISRs) isn't as easy as it might at first appear though. There are some rules:

  1. Keep the ISR as short and fast as possible so as to not miss any other interrupts.
  2. Never ever ever use anything that requires interrupts to be working inside an ISR. That includes things like delay(), millis(), Serial, etc.

You are using Serial in your ISRs. That is very very VERY bad. It will not work and your program will likely lock up.

Will interrupt 1 interrupt interrupt 0, or will interrupt 1 wait until interrupt 0's handler is done executing?????

The latter.

hi thanks for reply and suggestions.
i modified a code a little bit can you see once again and any mistakes i have made in it...

// program to find out the occupancy of the Bus

 int b1    = 2;  //sensor1 for boarding checking
 int a1    = 3;  //sensor2 for boarding checking
 int b2    = 4;  //sensor3 for alighting checking
 int a2    = 5;  //sensor4 for alighting checking
 int Led1  = 6; //Led to indicate the Boarding
 int Led2  = 7; //Led to indicate the Alighting
 volatile int a = 0;
 volatile int b = 0;
 volatile int count = 0; //variable to store the count
 
void setup()
{

  pinMode(b1,INPUT);
  pinMode(b2,INPUT);
  pinMode(a1,INPUT);
  pinMode(a2,INPUT);
  
  
  attachInterrupt(0, Board, LOW);
  attachInterrupt(1, Alight, LOW);
  
  Serial.begin(9600);
  Serial.println("The occupancy Module is ready");
  
}

void loop()
{
  if (a == 0 && b == 0)
  {
    Serial.print("count = ");
    Serial.println(count);
  }
 
}

void Board()
{
  b=1;
  digitalWrite(Led1, HIGH);
  
  for(int i=0; i < 1500; i++)
  {
    if(digitalRead(b2) == LOW)
      count++;
   }
   
  digitalWrite(Led1, LOW);  
  b=0;
}

void Alight()
{
  a=1;
  
  digitalWrite(Led2, HIGH);
  
  for(int j=0; j< 1500; j++)
  {
   if(digitalRead(a2) == LOW)
      count--; 
  }
  
  digitalWrite(Led2, LOW);
    
}

and any more suggestions to reach my project requirement?????

thanks in advance...

  for(int j=0; j< 1500; j++)
  {
   if(digitalRead(a2) == LOW)
      count--; 
  }
[code]
What did I say about keeping ISRs short and fast?  1,500 iterations of digital reading isn't going to be particularly fast...

[/code]

hi

what you are saying is true. but the requirement is when b1 sensor got the signal then i am checking the b2 sensor to make sure the person has boarded. for this i need to check the b2 sensor for some time that's why i programmed like that. is there any way to achieve this with out using the for loop inside the ISR???

as i already said i beginner to the Arduino programming

if it is possible you or any one reading this post please give any suggestions...

thanks in advance....

You do it all outside the ISR.

In an ISR you set a flag to say the sensor has been triggered. Then in your main loop you can look for a situation where the flag is set and the second sensor is triggered. Then, after a timeout period, if the second sensor hasn't been triggered, then reset the flag.

By the way, the whole thing can be done without interrupts at all. Interrupts are great for capturing very brief events, but anything longer period (>1ms) can be done without interrupts.

thanks for the suggestion i will do the modifications.

but what about the situation of occurring two interrupts at a time??? i will loose data at this point is there any solution to this???

if it is you or any one reading this post please suggest me...

thanks in advance...

I take it that your code is supposed to keep track of the number of people in a specific area, with one entrance and one exit? Increment on someone entering, and decrement on someone leaving?

I am also assuming something along the lines of a pair of laser or infra-red beam sensors at each entrance/exit?

Your current approach is subtly flawed. At the moment you're identifying the first beam break, then sitting and waiting for the second beam break. While you're doing that waiting nothing else can happen.

Instead you should be thinking along the lines of "The first beam broke at time X. The second at time Y. Y is greater than X, so someone entered." With people entering and leaving you could have all sorts of combinations of beam breaks happening, such as:

  1. Entry beam A
  2. Entry beam B
  3. Exit beam A
    4 Exit beam B

or

  1. Entry beam A
  2. Exit beam A
  3. Exit beam B
  4. Entry beam B

etc. You don't want to be waiting for someone to completely enter before you start looking to see if someone has exited.

You should be working with timestamps (millis()) not loops and interrupts.

this idea is very nice and i think this will work for most of the cases.

but as per my knowledge the millis() Returns the number of milliseconds since the Arduino board began running the current program. but is it possible to get the current instance beam broken for every sensor, and after finalising the person has boarded or alighted we need to get the fresh time stamps again.

i think it is not possible with millis() correct me if i am wrong ???
and is there any way to get the current time stamp ???

you are any one reading this post please suggest me..

thanks in advance...

Take a look at the BlinkWithoutDelay example

(If I had a penny...)

and is there any way to get the current time stamp ???

Unless you have a clock attached to the Arduino you cannot get the current time but you don't need it. Millis() gives you a number that increases as time goes by. So, by looking at millis() you can decide if one event occurred before or after another, which is what you need.