Jump to content
Sign in to follow this  
pine

Energia button debounce interrupt

Recommended Posts

Sorry if this has been asked and answered before, but the results from searches didn't explain the reason not working on my device and code..

 

Basically trying to do a very simple debounce in an interrupt for an Energia code sketch that paint a 1602 LCD:

const int buttonPin = PUSH1;     // the number of the pushbutton pin
int buttonState;             // the current reading from the input pin
volatile int lastButtonState = LOW;   // the previous reading from the input pin
volatile long lastDebounceTime = 0;  // the last time the output pin was toggled
volatile long debounceDelay = 500;    // the debounce time; increase if the output flickers


void setup() {
...
  pinMode(buttonPin, INPUT_PULLUP);

  attachInterrupt(buttonPin, switchDisplayMode, FALLING);
}

...


void switchDisplayMode() {
  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
    
    // start
    displayMode++;
    if (displayMode > 5) {
      displayMode = 0;
    }
//    delayMicroseconds(6000000);
    refreshDisplay();
    // end
    lastButtonState = reading;
  }
}

In the above code, debounce did not work all the times, like 3 in 5 times it seem to work, the rest are not. The displayMode variable incremented twice for each button clicked event. The target device is F5529 LP.

 

I have tried the delayMicroseconds which I believed to be irrelevant to the debounce logic if implemented correctly, but that doesn't seem to do the trick either.. Am I doing it wrongly? Any advice will be much appreciated. Thanks in advance.

Share this post


Link to post
Share on other sites

You attached the Interrupt as FALLING, this means the the interrupt is only triggered when the buttonpin changes its state from HIGH to LOW, therefore 'reading' should be always LOW when the interrupt is called and 'lastDebounceTime = millis();' is never executed.

So '((millis() - lastDebounceTime) > debounceDelay)' is always true.

 

Try: 

void switchDisplayMode() {
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // start
    displayMode++;
    if (displayMode > 5) {
      displayMode = 0;
    }
//    delayMicroseconds(6000000);
    refreshDisplay();
    // end
    lastDebounceTime = millis();
  }
}

Haven't tested the code myself, im a beginner @ MSP430, but you can give it a try.

You might want to reduce debounceDelay later, cause half a second is a long time when pushing buttons :)

 

And you should declare 'displayMode' as volatile if youre accessing the variable outside the interrupt routine.

 

regards,

Nick

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×