Best method for trigger only one event when a button is pressed? (re: interrupts?)

Status
Not open for further replies.

sofakng

Active member
I'm new to Teensy (and microcontrollers like Arduino, etc) and I have a question.

What is the best method for trigger an event (code) when a push-button is pressed?

It looks like I have three options: (?)
1) Use interrupts (ie. attachInterrupt method).
2) Use Bounce library and check for fallingEdge, etc.
3) Write my own code to check pin state every loop iteration and make sure each state change is only fired once.

What is the best/easiest approach?

It seems like using interrupts with attachInterrupt is the easiest since it will have the Teensy board call my function exactly one time (?) when my push button is pressed.

Is that right? Do I also need to worry bounce/debouncing?
 
To me the easiest is probably use the bounce library(https://www.pjrc.com/teensy/td_libs_Bounce.html). As for interrupts not sure how they keep your interrupt to only be called once?

Often with questions like this, I would simply try bounce and see if it works well enough for you.

Sometimes in the past I have simply checked the state, did a small delay and checked again, which usually was good enough to catch a button press.

But if the code was such that I might reenter the same code again (like call to loop) and I don't want the function to be called again while the button is still pressed, then I needed to remember the state from before and do something only if the state changes...

But then that just replicated the bounce library...
 
OK - Thanks for the information.

If I use the Bounce library, it looks like I still need to keep track of the previous state?

For example, if I'm checking pushbutton.fallingEdge() in a loop, can it be true for several iterations?

I'm hoping for something simple like this:

if (pushButton.isPushed)
disableEthernet();

However, if I have that in a loop, I don't want disableEthernet to be called the entire time the button is pressed. I only want it to be called unless the button is released and press a second time.
 
What I often suggest is to take a look at the code. That is the sources are installed wherever you installed Arduino and Teensy stuff.
In my case; it is at: c:\arduino-1.8.1\hardware\teensy\avr\libraries\Bounce

As the code mentions you should call update reasonably often. This is what checks to see the state of the button. It keeps two variables in it (actually a few more as well), but one is the current state (that is the value returned by digitalRead). And another variable stateChanged which is did the call to update find a different value than the previous call.

And then the call fallingEdge simply returns stateChanged && !state;
So it will return true if the last call to update found digitalRead returned a 0 and the previous call had returned 1...

So again should work fine! But again sometimes 2 minutes of trying it out is more convincing, like maybe try something like:
Code:
#include <Bounce.h>
#define BUTTON1 1

Bounce bouncer1 = Bounce(BUTTON1, 5); 

void setup() {
    pinMode(BUTTON1, INPUT_PULLUP);
    while (!Serial && (millis() < 4000)) ; 
    Serial.begin(115200);
}

void loop() {
    bouncer1.update();   // could also check return value here... 

    if (bouncer1.fallingEdge()) {
        Serial.println("Button Pressed");
    }
}
Warning typed in on the fly so might have missed something
 
What is the best method for trigger an event (code) when a push-button is pressed?
2) Use Bounce library and check for fallingEdge, etc.

The Bounce library is your best choice.

It seems like using interrupts with attachInterrupt is the easiest since it will have the Teensy board call my function exactly one time (?) when my push button is pressed.

No, this is the worst way. Interrupts are terribly frustrating due to mechanical chatter.


If I use the Bounce library, it looks like I still need to keep track of the previous state?

For example, if I'm checking pushbutton.fallingEdge() in a loop, can it be true for several iterations?

The Bounce library does this for you. The fallingEdge() function returns true just once (per update) when the button changes.
 
Status
Not open for further replies.
Back
Top