How to use Interrupt in Arduino??[tutorial needed]

i just recently use arduino for my project and i`m wondering to use interrupt and i haven't successful to do it.. can someone give some tutorial and a bit explanation of basic use of interrupt??
thank you..

How, when, and why to use an interrupt depend so much on your particular application that a generic tutorial would not really be useful.

What is going to trigger the interrupt? An internal event or an external event?

What does the interrupt handler need to do?

Specific questions generally get much better answers than overly broad questions.

i'm trying to make an automatic door with PIR sensor..

what i'm already done is whenever the PIR sensor gives signal it would open and whenever the signal is gone the door is closed.. the case why i need interrupt is when the signal is gone and the door will automatically closed, but if the sensor give a signal when the close subroutine is one the move, the door will finish the subroutine first then open again...

what i want is to use the interrupt to open the door even the subroutine is on the move.. so overrides the close subroutine..

What is going to trigger the interrupt?

the sensor

An internal event or an external event?

not quite sure. but I think external,..?

What does the interrupt handler need to do?

do open subroutine

thank you..

i'm trying to make an automatic door with PIR sensor..

I don't think you need an interrupt.

An interrupt is designed to operate on the scale of machine instructions.
A 16MHz Arduino executes a typical instruction in the time it takes a beam of light to travel about 20 metres.

Are you sure you need to respond to your door opening quite so fast?

You should post the code that you are using today. While an interrupt can be used to interrupt the door closing function, and open the door, as soon as the interrupt has been handled (the door opened), the code will resume RIGHT WHERE IT LEFT OFF. That is, the door will close again.

I think it would be better to have the door closing function check that it is still necessary to close the door, periodically.

It's hard to tell you how to make that happen, though, without having any idea what the open and close functions are actually doing.

While an interrupt can be used to interrupt the door closing function, and open the door, as soon as the interrupt has been handled (the door opened), the code will resume RIGHT WHERE IT LEFT OFF. That is, the door will close again.

after reading this i realized it can't use interrupt, thanks to PaulS..

the haven't done with the code, and i`m just working with the basic logic and possibilities. when i thought i could use interrupt to my project, i'm asking for some tutorial that i can learn and implement it..

It's hard to tell you how to make that happen, though, without having any idea what the open and close functions are actually doing.

the code that i think of is just simply turn on the motor clockwise or clockwise to open or close the door.

can the subroutine do like this?

open()
{

while (limitswitch1 > 0);
{
analogWrite(motorCCW, 200);
}
digitalWrite(motorCCW,LOW);
}

close()
{
while (limitswitch2 >0);
{
analogWrite(motorCW, 200);
if (PIRsensor >0);
{
open();
break;
}
digitalWrite(motorCW, LOW);
}
}

the logic for open subroutine = turn on the motor to open the door until the limit switch1 is ON.
the logic for close subroutine = turn on the motor to close the door until the limit switch2 is ON but, if PIR sensor is ON, the subroutine will break and call open subroutine again..

is this applicable?

The logic is not bad. The implementation needs some help. There is nothing in the while loops to change the value in limitswitch1 or limitswitch2.

Once the motor has been turned on, it does not need to be turned on again.

So, the open function should look like this:

void open()
{
   analogWrite(motorCCW, 200);
   while(digitalRead(limitSwitchPin) == LOW)
   {
   }
   analogWrite(motorCCW, 0);
}

The while loop will end when the switch attached to limitSwitchPin goes LOW, and the motor will be turned off.

Currently, there is nothing to do in the while loop, but, you could add other code in the loop, and break out of the loop if conditions warrant. When a break occurs inside the while loop, the loop ends, just as if the limit switch had been hit.

Inside the while loop, you could even call close(), and then break out of the loop, though you should probably stop the motor before doing that.

thanks for the correction...

1 question, when we use break, how many } are break?

if there is a code like this

void loop()
{
if ()
 {
  while()
  {
  break;
  }
 x();
 }
y();
}

when break is occurred, which subroutine is done next? x or y?

x() is called next. The break statement exits the current loop, whether it is a for loop or a while loop.

Embedds has a few nice articles on AVR programming (Implementing AVR interrupts - Embedds). While they are geared toward the more generic AVR programming, the principal (and the code) applies to Arduino as well.

I have found those articles very educative.

thanks to PaulS and AlphaZeta for the info..

i`ll take a look at the link..