pine 140 Posted March 9, 2014 Share Posted March 9, 2014 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. Quote Link to post Share on other sites
Thorvard 14 Posted March 9, 2014 Share Posted March 9, 2014 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 pine 1 Quote Link to post Share on other sites
pine 140 Posted March 9, 2014 Author Share Posted March 9, 2014 Thanks Nick, worked like a charm Thorvard 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.