Author Topic: For Interrupt 2 Pin D2 or how can I use the PinChangeInterrupt  (Read 1373 times)

Hayato

  • NewMember
  • *
  • Posts: 2
Hello,

I am learning from the example TxRxBlinky in the library.  And I want to set 3 push button it and just repeat the example code on other two push button.
I set push button 2 on D10 which is INT0, But I can not set the last one on D2 because it is used for radio.
So I use the <PinChangeInterrupt.h> library to set up on pin 14, but it still do not work.
from the guide, I set BUTTON_INT2 as 30 which is the PCINT number for ATmega1284P.

the following is the relative code:

Code: [Select]
#define BUTTON_INT2    30 
#define BUTTON_PIN2    14

attachPinChangeInterrupt(BUTTON_INT2, handleButton2, FALLING);

#define FLAG_INTERRUPT2 0x03

boolean buttonPressed2 = false;

void handleButton2()
{
  mainEventFlags |= FLAG_INTERRUPT2;
}

if (mainEventFlags & FLAG_INTERRUPT2)
  {
    LowPower.powerDown(SLEEP_120MS, ADC_OFF, BOD_ON);
    mainEventFlags &= ~FLAG_INTERRUPT2;
    if (!digitalRead(BUTTON_PIN2)) {
      buttonPressed2=true;
    }
  }

if (buttonPressed2)
  {
    Serial.println("Button pressed!");
    buttonPressed2 = false;
   
    if (radio.sendWithRetry(RECEIVER, "Hi", 2)) //target node Id, message as string or byte array, message length
      Blink(LED, 40, 3); //blink LED 3 times, 40ms between blinks
  }
« Last Edit: March 21, 2018, 08:30:03 AM by Felix »

Uncle Buzz

  • Full Member
  • ***
  • Posts: 146
  • Country: fr
Re: For Interrupt 2 Pin D2 or how can I use the PinChangeInterrupt
« Reply #1 on: March 21, 2018, 05:15:12 AM »
I don't know if there is something wrong with your interrupt number, but in my case I declare the interrupt like this :
Code: [Select]
pinMode(myPin, INPUT_PULLUP);
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(myPin), myInterruptFunction, CHANGE);
So same code works on 328p or 1284p.
Don't forget to declare the pinMode since declaring interrupt won't declare the pin as an input.

Why do you declare your FLAG at 0x03 ? it uses 2 bits and could be in conflict if you use  0x01 or 0x02 for other buttons, you should use 0x01, 0x02, 0x04 or 0b001, 0b010, 0b100... something with exclusive bits