Simultaneous Button push

Hello,
I have difficulties to detect simultaneous button push with Arduino mega2560. The most bizarre thing is that it worked well for some time and then did not.
Basically, I control different things with different buttons while using simultaneous button push to minimize button quantity. Something like that:

    while(button3.isPressed() && button4.isPressed())
    { delay(50); 
        while (button3.isPressed() || button4.isPressed())
        { // wait
        }
        // do stuff    
    }

But now every time more then one button is pushed I get HIGH on all buttons (while using pinMode(xx, INPUT_PULLUP)).
All my buttons are connected to ground and individual digital pins. The reduced code that still doesn’t work is:

const int BUTTON_PIN_1 = 8;
const int BUTTON_PIN_2 = 9;
 
void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN_1, INPUT_PULLUP);
  pinMode(BUTTON_PIN_2, INPUT_PULLUP);
}

void loop() {
  Serial.print(" - ");
  Serial.print(digitalRead(BUTTON_PIN_1));
  Serial.print(" - ");
  Serial.print(digitalRead(BUTTON_PIN_2));
  Serial.println(" - ");
}

I see:
-1-1- - no buttons are pushed
-0-1- - one buttons is pushed
-1-1- - both buttons are pushed
while according to my understanding it should be:
-0-0- - both buttons are pushed

Or maybe it doesn't work that way at all with the internal pullup resistor?

Implementing simultaneous button press has to be carefully thought through, especially if the single buttons also have other functions. The main problem is that you can never guarantee to press both buttons at exactly the same time and any failure to detect a simultaneous activation of the two buttons could mean that their individual functions are executed instead.
In practice, this means that a single button press is not acted upon until release, allowing the the detection of any press of another button that may come along in the meantime before that first button is released

Looking at you simple code, do you see LOW at the appropriate point when only 1 button is pressed?

So no buttons pressed, both HIGH, 1 button pressed it shows LOW, both buttons pressed both HIGH?

Steve

The problem is simply one of collectively debouncing multiple buttons.

Upon any button being pressed, ignore this and wait 50--100ms or so and then read all the buttons again,
this time act upon the values, including checking for multiple buttons changing since last time
they were recorded.

The wait doesn't have to mean delay(), but can be done by using state and a timeout.

This both debounces the buttons and is insensitive to the precise order they were pressed
during the time window. The same/simlar logic can be used for release to debounce that
transition.

The behaviour can be improved somewhat to be more sensitive to individual very short
presses, since you might want to act on these even if shorter than the 50--100ms debounce
interval.

Thanks for the comments. But I didnt want this post to be about debouncing or button working principle, especially because there are no problems in my first short example regarding debouncing or false button reading. In this example the controller will land in the while loop only if these two buttons are pressed or about to be pressed and wait until these buttons are released (or about to be released). Besides, my functions <buttonX.isPressed()> are debounced, which is not important here.

Looking at you simple code, do you see LOW at the appropriate point when only 1 button is pressed?

So no buttons pressed, both HIGH, 1 button pressed it shows LOW, both buttons pressed both HIGH?

Steve

yes, thats exactly what I get and dont understand.

I don't see an isPressed() function in the arduino Reference..
Is that part of some library ?

I have found the easiest way to handle buttons is by setting up their input pins to trigger an interrupt. In the interrupt, all I do is toggle a boolean variable, which I poll in the main loop to see if the button was pushed. If it was, you can service it, then resets the boolean for another button push. Far cleaner and easier than messing with all the timing difficulties associated with debounce.

Which button library did you use?

I refer you to
attachInterrupt() - Arduino Reference to set up interrupst on pins. The Uno itself only has pins 2 and 3 available for this, so if you need more interrupts, you need to look at some of the UNO's siblings.

raschemmel:
I don't see an isPressed() function in the arduino Reference..
Is that part of some library ?

It's in Button, yes. (All least I think that's the name; I'm on work laptop atm and don't have it installed here. Author Breitzig or similar iirc.)
I had a quick look at the source, wondering if it had intelligence to check for a new press, but afaics it just looks for a button is pressed. What it does have, is the ability to define a button as having a pulldown or pullup, so it takes care of high/low vs low/high depending on the wiring.

Hi,
You show only three different button presses.

-1-1- - no buttons are pushed
-0-1- - one buttons is pushed
-1-1- - both buttons are pushed

Do you see
-1-0- if the other button on its own is pressed?

Can you post a picture of your project so we can see your layout?
Can you please post a circuit diagram?

Do you have this on protoboard and do you have a DMM to verify that pressing each button really does gnd the appropriate input pin?

Thanks.. Tom.. :slight_smile:

TomGeorge:
Hi,
You show only three different button presses.
Do you see
-1-0- if the other button on its own is pressed?

Can you post a picture of your project so we can see your layout?
Can you please post a circuit diagram?

Do you have this on protoboard and do you have a DMM to verify that pressing each button really does gnd the appropriate input pin?

Thanks.. Tom.. :slight_smile:

yes I actualy use 4 buttons and everyone individualy works and prints "-0-". I don’t have the schematics of my overall construction. The test schematic is just: all (2 pin) buttons are connected to ground and individual digital pins (not 13).

It seems like the problem is my arduino or the enviroment the arduino is in. My overall setup includes steperdrivers, releys, display, 4 buttons and power supplies.
I tried today the third arduino and everything worked fine with code that didn’t work 2 days ago.

I had already replaced the Arduino, buttons and wiring once inside my device. I guess the problem is either the static discharges (of which I had many during assembly) or some component of the assembly causes malfunction of the Arduino. That would explain why the first two arduinos that I tried inside the devise didn’t work and the third one that I tried outside of the device did.

Now the fun part. I did tried to rewire the 1. Arduino and the results were:
Using internal pullup -> doesn’t work
Rewire and change buttons -> doesn’t work
Using external 30kOhm pullup -> doesn’t work
Using external 30kOhm pulldown -> it works
Using internal pullup again -> it works

So I will put the first arduino back in and see how long it lasts.

Hi,

Do you have this on protoboard and do you have a DMM to verify that pressing each button really does gnd the appropriate input pin?

Tom... :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.