2560 interrupts

Hi all. Need help on my arduino mega 2560 all interrupts are not functioning even if i tried samples from internet. Does anyone know how to activate or how can i make my interrupts work?i really need help i have project which needs interrupt.

Show an example of your code that does not work (and use code tags as described in the "how to use this forum" thread).
Show a diagram so we can see what and how everything is connected; photo/scan of hand drawn diagram is fine.

Describe what you need to achieve; e.g. one usually does not need interrupts for buttons so if you feel you need an interrupt to read a button, you're more than likely on the wrong track.

@Ethyenian12345

You can try the following steps to make at least one interrupt (the INT0) working. If this one works, then you must say that you will be trying yourself for the other interrupts to work.

1. Connect one terminal of a ‘Normally Open’ switch (K1) at DPin-21 (digital Pin-21) of the MEGA. DPin-21 is connected with the INT0-pin of ATMEGA (see schematic at Step-12). Connect other terminal of the switch at GND point. This K1 is your interrupting device, and it is going to inject interrupt signal (IRQ-signal) at the INT0-pin of MEGA when you just press and release. Do not forget to enable the internal pull-up resistor of DPin-21 using the commandpinMode(21, INPUT_PULLUP in the void setup() function.

int counter = 3;
void setup()
{
   pinMode(21, INPUT_PULLUP);
   pinMoe(13, OUTPUT);
   pinMode(8, OUTput);
   attachInterrupt(digitalPinToInterrupt(21), isrInterruptZero, FALLING);

}

2. You need a Main Line Program (MLP) which the MCU will keep executing again-and-again until interruption comes from K1. The MLP could be of continuous blinking of L of the MEGA at 1-sec interval. Write codes for the MLP and keep it under void loop() function. Check with the ATMEGA schematic Step-12 that L is connected with DPin-13.

void loop()
{
   //set direction direction of DPin-13 as output under setup with command pinMode(13, OUTPUT)
  digitalWrite(13, HIGH);      //writing LH at DPin-13 to ignite L
  delay(1000);                    // calling function of Arduino for 1000 mS (1-sec) Time Delay
  digitalWrite(13, LOW);
  delay(1000);

}

3. You need an Interrupt Sub Routine (ISR) which the MCU will execute after receiving IRQ-signal from K1. The ISR could be blinking LED2 (external LED2) connected at DPin-8 of ATMEGA only for 3 times at 2-sec interval. Connect LED2 with a 2.2k series resistor with DPin-8 of the MEGA. Now, write codes for the ISR in the isrInterruptZero() functon.

void isrInterruptZero()
{
  //set direction direction of DPin-8 as output under setup() with command pinMode(8, OUTPUT)
  //set counter for 3 under at the top of setup() with command int counter = 3;
  //initialize INT0 interrupt under setup using the command attcahInterrupt(2, isrInterruptZero, FALLING);
 interrupts();     //pls don't ask me why do we need to write it here? Arduino-Hidden function!
 //------------------------------------------------------
 while (counter != 0)
 {

    digitalWrite(8, HIGH);      //writing LH at DPin-13 to ignite L
    delay(2000);                    // calling function of Arduino for 1000 mS (1-sec) Time Delay
    digitalWrite(8, LOW);
    delay(2000);
    counter--;

 }

}

4. Compile your program and upload it into the ATMEGA.
5. Press the RESET switch of the ATMEGA.
6. Check that L is blinking.
7. After a while, gently press K1.
8. Check that L is not blinking, but LED2 has started blinking at 2-sec interval.
9. Check that LED2 has completed 3 blinkings.
10. Check that LED2 has stopped blinking.
11. Check that L has started blinking again.

If Step-10 and Step-11 are not working, we are in trouble!

Good Luck!

//---------------------------------------------------------------------------------------------------------------
12. Schematic diagram (unofficial) of Arduino ATMEGA
please download the attached file.

ArduinoMEGASchematic.doc (1.13 MB)

GolamMostafa:
You can try the following steps to make at least one interrupt (the INT0) working

Please don't try code that has delay() inside an ISR.

...R

Actually, test delay(). If it works, then interrupts are working.

@Robin2

I am aware about the problem of using delay() function inside an ISR; but, for demonstration purposes we (I) use it time-to-time.

Ethyenian12345 (the OP) made the post at 5.46 am., and no task arrived to him until 10.10 am. For 5 hrs he was sitting idle at least on his interrupt issue!

I wished to give him (the OP) some thing so that he can start the task, interrupt the MCU, and see visible result (blinking LED2) in the ISR. He will observe unexpected things like the MCU is not returning to MLP after 3 blinks in the ISR, and that's why I focused doubt on the functioning of Step-10, 11 of my algorithm.

I am known about the limitation of this experiment. It is not a clean experiment; there are multiple interrupts due to bouncing of the interrupting switch (K1); the process is not repeated due to the reason of calling delay() function in the ISR; the ISR can be triggered using the attcahInterrupt(2, arg2, arg3); function; etc.

In honor of your request for not using delay() function in the ISR, I have to make my 2-sec timeDelay() sub prgram using Timer-1 of TC1. At this time, I am reluctant to make the OP's life miserable by creating another function or instead writing in-line codes.

Thanks for the meticulous observation.

GolamMostafa:
Thanks for the meticulous observation.

There is no need to use a Timer if you don't use delay() in an interrupt. The interrupt can raise a flag that is identified by the code in loop() and the delay() can take place there. That way the OP can have an example of good ISR practice and the simplicity of delay(). Or the ISR could be used to change the variable that represents the length of the delay().

...R

GolamMostafa:
Ethyenian12345 (the OP) made the post at 5.46 am., and no task arrived to him until 10.10 am. For 5 hrs he was sitting idle at least on his interrupt issue!

OP could have replied to my reply :wink: So I doubt he/she was sitting idle on the interrupt issue.

@sterretje

Sorry, perhaps, I missed your post.

I duly acknowledge and announce that Post #1 inquired of the OP at 7:26 am (the OP made the post at 5:46 a.m.) about his/her progress. Should the OP respond, it was very much sure that @sterretje would post some tasks/hints for him.

Robin2:
Please don't try code that has delay() inside an ISR.

...R

I'm much less nitpicky about code that's purely meant to test or diagnose an issue. Plus, he does turn interrupts on at the beginning of the ISR, so it'll work well enough to learn if you can set up the ISR properly.

Though is is accompanied with this amusing comment:

//pls don't ask me why do we need to write it here? Arduino-Hidden function!

Jiggy-Ninja:
I'm much less nitpicky about code that's purely meant to test or diagnose an issue.

My concern is that it can start a newbie in the wrong direction.

Cruddy convenience code is fine for experts who will recognize it for what it is :slight_smile:

...R