This_is_Mine
This_is_Mine

Reputation: 65

Arduino Mega 2560 Interrupt w/ Rotary Encoder

I started out using the Uno and I was able to get an interrupt working from a rotary library I found online but when I moved the project to the Mega and tried changing it for the different pins it stops. I spent a few hours trying to figure out the interrupt pin on the mega from online sources and just can't find any good resource to explain the mega interrupt pins sufficiently.

I am trying to use interrupts like so.

  Rotary r = Rotary(10,11);
void setup(){
  PCICR |= (1 << PCIE0);
  PCMSK0 |= (1 << PCINT4) | (1 << PCINT5);
  sei();
  }

ISR(PCINT0_vect){
//stuff
}

It doesnt really matter what pin I am using for the interrupt if someone has a preferred method. I just need it to work.

Upvotes: 1

Views: 6401

Answers (1)

UncleO
UncleO

Reputation: 8449

Arduino interrupts are described here. It is easier to use than the example code you provide.

//Mega2560
// external interrupt int.0    int.1    int.2   int.3   int.4   int.5            
// pin                  2         3      21      20      19      18



void setup()
{
  // interrupt # 0, pin 2
  attachInterrupt(0, myISR, CHANGE); // Also LOW, RISING, FALLING
}

void loop()
{

}

void myISR() // must return void and take no arguments
{
  // stuff
}

You don't need to enable interrupts with sei();, because attachInterrupt() does it for you. But you can disable interrupts with cli(); and re-enable them with sei();

Upvotes: 0

Related Questions