Trying to read button presses

Post here first, or if you can't find a relevant section!
User avatar
efftek
Posts: 47
Joined: Tue Jan 10, 2017 9:42 pm
Location: UK / Cyprus
Contact:

Trying to read button presses

Postby efftek » Sun Mar 19, 2017 8:28 pm

Hi,

I have two buttons connected to my blue pill on PB10 and PB11. Ultimately, I need six buttons, I'm just messing around with code to get two working. So, I initialise both pins with INPUT_PULLUP so it will detect when the pin is pulled low by a button press and it was working well with just one button. Now I have added a second button, it only detects one press at a time, ie I cant press the same button twice. If a press is detected, I have a debounce delay of 100mS then a while loop waits until the button is released so holding the button down doesn't give multiple detects. One butto turns the board LED on, the other turns it off so I have a visual representation that it is working. Theres also a serial output to show which button was pressed for when I have more than two buttons connected. I cannot for the life of me see why it wont detect the same button pressed repeated times. Any ideas?

Code: Select all

#define DEBOUNCE 100

void setup()
{
  pinMode(PC13, OUTPUT);
  digitalWrite(PC13, LOW);

  pinMode(PB10, INPUT_PULLUP);
  pinMode(PB11, INPUT_PULLUP);
  Serial.begin(115200);
}


int isButtonPressed(int pin)
{
    if (digitalRead(pin))
    {
        delay(DEBOUNCE);
        while (digitalRead(pin));
        return true;
    }
    else
    return false;
}

void loop()
{
    if (isButtonPressed(PB10))
    {
      Serial.println("You pressed UP");
      digitalWrite(PC13, HIGH);
      delay(500);
    }
   
    if (isButtonPressed(PB11))
    {
      Serial.println("You pressed DOWN");
      digitalWrite(PC13, LOW);
      delay(500);
    }
}


Thanks, Steve.

User avatar
BennehBoy
Posts: 388
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Re: Trying to read button presses

Postby BennehBoy » Sun Mar 19, 2017 8:50 pm

I use a little library to read them inside a state machine, it works nicely.

probably best to look at the code -> https://github.com/BennehBoy/LRDuinoTD5
-------------------------------------
https://github.com/BennehBoy

User avatar
efftek
Posts: 47
Joined: Tue Jan 10, 2017 9:42 pm
Location: UK / Cyprus
Contact:

Re: Trying to read button presses

Postby efftek » Sun Mar 19, 2017 9:06 pm

Thankyou. I have downloaded the files but will look at them later, my head is getting heavy and my eyes grow dim, I need to stop for tonight!

Sorry thats what happens when your designing a board using Eagle all day!

Steve.

enif
Posts: 32
Joined: Wed Jul 29, 2015 4:49 pm
Location: Switzerland
Contact:

Re: Trying to read button presses

Postby enif » Mon Mar 20, 2017 8:55 am

Aren't your buttons are pressed when digitalRead(pin) is false? But your code seems to test it for true. So as long as button PB11 is not pressed, it does not return from isButtonPressed(PB11).

But anyway, I doubt that the way you try to debounce is working well, so indeed you will probably be better off using the library...

User avatar
efftek
Posts: 47
Joined: Tue Jan 10, 2017 9:42 pm
Location: UK / Cyprus
Contact:

Re: Trying to read button presses

Postby efftek » Mon Mar 20, 2017 2:09 pm

enif wrote:But anyway, I doubt that the way you try to debounce is working well, so indeed you will probably be better off using the library...


This code was copied from the Arduino playground so I would have expected the trues and flames to be correct and the debounce to work too - they have much more experience than me! However, the library works and is my way forward.

Nutsy
Posts: 200
Joined: Sun Jul 24, 2016 4:26 pm

Re: Trying to read button presses

Postby Nutsy » Tue Mar 21, 2017 10:49 am

What about using interrupts?

User avatar
BennehBoy
Posts: 388
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Re: Trying to read button presses

Postby BennehBoy » Tue Mar 21, 2017 11:05 am

Nutsy wrote:What about using interrupts?


It's possible, but a check in the main loop should be fast enough for human interaction. With the benefit of knowing exactly where in the code it's being triggered, ie not in the middle of something time sensitive.
-------------------------------------
https://github.com/BennehBoy

Nutsy
Posts: 200
Joined: Sun Jul 24, 2016 4:26 pm

Re: Trying to read button presses

Postby Nutsy » Tue Mar 21, 2017 4:04 pm

fair enough... Might sound superfluous but in my project, to get button presses to work properly im using the built in nextion library button interrupts. (on screen buttons) Well a mixture of that and some of my own serial checking stuff... the library really is a total mess... Bearly functions as its designed too.

but in any case, what im doing is having the interrupts flag a boolean thats checked in due course of the loop run then takes action to what it needs to do...

A hardware interrupt can run in the same way. A small function to set a boolean thats later checked.

Or depending on ease of coding... the interrupt call can do everything thats required later... Though Im not 100% sure if a function called by an interrupt call blocks other interrupts...

User avatar
BennehBoy
Posts: 388
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Re: Trying to read button presses

Postby BennehBoy » Tue Mar 21, 2017 6:37 pm

Yeah best to spend as little time in an interrupt as possible.
-------------------------------------
https://github.com/BennehBoy

User avatar
efftek
Posts: 47
Joined: Tue Jan 10, 2017 9:42 pm
Location: UK / Cyprus
Contact:

Re: Trying to read button presses

Postby efftek » Tue Mar 21, 2017 8:27 pm

Nutsy wrote:What about using interrupts?


I thought about that but then as I'm collecting 1024 samples from the ADC as 391uS intervals every so often, thats a function I don't want interrupted otherwise the FFT will be all to cock.

Anyhow, the library seems to work well. My only objection is that if I hold a button down too long, I get a second press when it is released. I'm not sure what will happen if I hold down a button and press a second button before I release the first but if I can read two button presses at once, that is something I could exploit.

Steve.


Return to “General discussion”

Who is online

Users browsing this forum: michael_l and 2 guests