Go Down

Topic: External Interrupts don't work in MEGA2560 (Read 654 times) previous topic - next topic

meteorfox

My problem is that I've been trying to take external interrupts from a button to change a variable in the program. I have tried everything, attachInterrupts(), volatiles, even tried with standard <avr/interrupt.h>. Please help! Because I was working in a bigger project and didn't want to confuse you guys with unrelated code, I modify a the Button sketch to insted use interrupts but still this simple example does not work.

Code: [Select]


...
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
volatile bool buttonState = false;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
//  digitalWrite(buttonPin, HIGH);
 
  attachInterrupt(buttonPin,flash,LOW); 
}

void loop(){
 
}

void flash()
{

  if (buttonState == true) {     
    // turn LED on:   
    digitalWrite(ledPin, LOW); 
    buttonState = false;
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, HIGH);
    buttonState = true;
  }
}


retrolefty

The user interrupt pin numbers and the user interrupt number are two separate identities. I know it doesn't make sense but:

User interrupt 0 supports arduino pin 2, and interrupt 1 supports arduino pin 3.

So change your attachment code

from : attachInterrupt(buttonPin,flash,LOW);
to :  attachInterrupt(0,flash,LOW);  // if a low is seen on pin 2, generate an interrupt to flash

From the arduino reference:

Quote
Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18).


Hope that helps.

Lefty  


CrossRoads

in the ISR flash, every time you get an interrupt you change the state of buttonState which is redundant with the state of the ledPin but shows whether the LED is on or not?

How is your buttonPin wired? Normally open, and pressing it closes to ground to bring the interrupt input Low?
Then you will need this line uncommented to turn on the internal pullup.
//  digitalWrite(buttonPin, HIGH);

You may need some debounce for the buttonPin -every time it is pressed, it will look like 01010101000000 before finally settling, and then similar on release,  so if you just press & release quick, you don't really know what you're gonna end up with.

keep this part in
#include <avr/interrupt.h>
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

meteorfox

Thank you all! This fixed the problem

Quote

User interrupt 0 supports arduino pin 2, and interrupt 1 supports arduino pin 3.

So change your attachment code

from : attachInterrupt(buttonPin,flash,LOW);
to :  attachInterrupt(0,flash,LOW);  // if a low is seen on pin 2, generate an interrupt to flash


and about this
Quote

in the ISR flash, every time you get an interrupt you change the state of buttonState which is redundant with the state of the ledPin but shows whether the LED is on or not?


Yes, I knew that but that was irrelevant to the problem, remember this is just a simple example to try to get working the external interrupts.

Quote
How is your buttonPin wired? Normally open, and pressing it closes to ground to bring the interrupt input Low?
Then you will need this line uncommented to turn on the internal pullup.
//  digitalWrite(buttonPin, HIGH);


I had a pull-up circuit (to 5V) interface between the button and the MCU.

Quote
You may need some debounce for the buttonPin -every time it is pressed, it will look like 01010101000000 before finally settling, and then similar on release,  so if you just press & release quick, you don't really know what you're gonna end up with.


Yes, I was aware of this but I knew this was not the problem. I tried the same example but without toggling the variable every time, so if that would have happened then the LED would just have stayed on. In fact, I can now see that it has a lot of bouncing! But I know how to fix that quickly :P

Anyway, many thanks I really appreciate your help!

Go Up