1

I am using the MSP430f5529 launchpad and coding in Energia.

The goal is to make a program that can detect whether the onboard pushbutton is up or down (as part of an eventual Morse code decoder), which I planned to implement using two attachInterrupts.

If I use one, for example attachInterrupt(BUTTON, buttonPress, FALLING), it works fine and the button press is detected. If I use attachInterrupt(BUTTON, buttonRelease, RISING), the button release is detected.

However, if I have bothattachInterrupts in the same program, only one of them works; based on trial and error, it seems whichever interrupt was declared last takes precedence.

In the below code, for example, only buttonPress fires. If I swap the order of my attachInterrupts, only buttonRelease will fire.

#define BUTTON PUSH2

volatile int buttonDown = 987; // set to an arbitrary value so I can tell if the functions changed it at all

void setup() {
  Serial.begin(9600);
  Serial.println("Test code for buttons");

  // Enable internal pullup
  pinMode(BUTTON, INPUT_PULLUP);
  // attaching release before press
  attachInterrupt(BUTTON, buttonRelease, RISING);
  attachInterrupt(BUTTON, buttonPress, FALLING);
}

void loop() {
  Serial.print("buttonDown: ");
  Serial.println(buttonDown);
}

void buttonPress() {
  buttonDown = 1;
}

void buttonRelease() {
  buttonDown = 0;
}

How can I make both buttonRelease and buttonPress work at the correct times? I tried to just check if the button state changed using a single interrupt (that detected CHANGE, not just RISING or FALLING), but that seemed to lead to even more problems down the road, since the hardware would sometimes register a change in the middle of running the code needed to process what to do with that change.

Somatic
  • 113
  • 3

1 Answers1

2

For the pins that support pin change interrupts (P1, P2), each pin has a flag that controls which edge the interrupt is triggered on. This means that the pin cannot trigger an interrupt for a rising edge AND a falling edge like you want. See Page 308

C_Elegans
  • 2,871
  • 14
  • 27