Pages: [1]   Go Down
Author Topic: External Interrupts don't work in MEGA2560  (Read 607 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

...
// 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;
  }
}
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  

Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27115
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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>
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-razz

Anyway, many thanks I really appreciate your help!
Logged

Pages: [1]   Go Up
Jump to: