6 years, 2 months ago.

i have problem with push button counter

Hi, i'm beginner , therefor i can't understand some simple thing like push button Counter. my problem: in need to write a simple program, which one, counting the push button,wenn pressed and save it in one register for later. for example: if button pressed one time, led1 on. led 2&3 off. if button pressed two time, just led 2 on. led1&3 off if button pressed 3 time, just led3 on, led1&2 off and wenn pressed a gain, repeat it same. i founded this code: int main() { int count=0; int old_pb=0; int new_pb; pb.mode(PullUp); wait(.001); while(1) { new_pb = pb; if ((new_pb==0) && (old_pb==1)) count++; myled4 = count & 0x01; myled3 = (count & 0x02)>>1; myled2 = (count & 0x04)>>2; myled = (count & 0x08)>>3; old_pb = new_pb;

its very helpful, but here stay leds 1& 2 & 3 & 4 on, after that, that i pressed button many time. i dont know, how can use count int for my Programm, so that, just one led stay on and the other led off. who can help me in this case? Thanks

1 Answer

6 years, 2 months ago.

Hello Da,

You can do it for example as below (including a simple button debouncing). Please notice that the variables used in Interrupt Service Routines (ISR) are declared as volatile. That's telling the compiler that their values can change anytime during the program execution (due to some "misterious" reasons unknown to the smart compiler). Otherwise the compiler would discard the expressions defined in ISR during code optimization in order to make the program run faster and to reduce it's size. But then the program wouldn't behave as we intended.

#include "mbed.h"

// Substitute "p21" with actual pin name of your button.
#define USER_BUTTON p21

DigitalOut      led1(LED1);
DigitalOut      led2(LED2);
DigitalOut      led3(LED3);
InterruptIn     button1(USER_BUTTON);
volatile bool   button1Pressed = false; // Used in the main loop
volatile bool   button1Enabled = true;  // Used for debouncing
Timeout         button1Timeout;         // Used for debouncing
volatile int    counter = 0;            // button1 press event counter

// ISR (Interrupt Service Routine) to enable button when bouncing is over
void enableButton1(void)
{
    button1Enabled = true;
}

// ISR (Interrupt Service Rountine) handling button pressed event
void onButton1Pressed(void)
{
    if (button1Enabled) // Enabled when the button is not bouncing
    {
        button1Enabled = false; // Disable the button while it's bouncing
        button1Pressed = true;  // To be read in the main loop
        button1Timeout.attach(callback(enableButton1), 0.3);    // Debounce time 300 ms
        counter++;      // Increment button's "press event" counter
    }
}

int main()
{
    button1.mode(PullUp);                       // Connect internal pull-up resistor to keed button1 at +3.3V when not pressed
    button1.fall(callback(onButton1Pressed));   // Attach ISR to handle button press event
    while (1)
    {
        // Here we check the status of "button1Pressed" flag. Normally it's "false" but set to "true" on button1 pressed event.
        if (button1Pressed)
        {
            button1Pressed = false; // Reset "button1Pressed" flag to make it ready to be set again in "onButton1Pressed" ISR.
            switch (counter)        // Check how many times was "button1" pressed.
            {
            case 1:                 // One time -> Turn led1 on and the other leds off.
                led1 = 1;
                led2 = 0;
                led3 = 0;
                break;
            case 2:                 // Two times -> Turn led2 on and the other leds off.
                led1 = 0;
                led2 = 1;
                led3 = 0;
                break;
            case 3:                 // Three times -> Turn led3 on and the other leds off.
                led1 = 0;
                led2 = 0;
                led3 = 1;
                counter = 0;        // Reset "counter" to start counting from zero again.
            }
        }
    }
}

Accepted Answer

It was very helpful. Thanks a lot.

posted by Da Ar 24 May 2018