Button Pressing

I'd like a bit of guidance in how I can start a project which involves my Arduino counting the number of buttons being pressed(held down constantly) at once. I'm not really sure how to start this off. I'd eventually like to put this number onto some sort of display. I'm a beginner.

And then when you get sick and tired of typing the same stuff for loads of buttons, read up on arrays.

Here's a hint: Explain also why you want to do this?

What are these buttons? How many?

How have you connected them?

I recommend using ezButton library. It supports multiple buttons counting, and also support deboucing

You will need to be careful about how you determine the buttons are being held down "at the same time". An arduino executes instructions much, much faster than it takes to push a button, so if someone were to press three buttons at once, the arduino will almost never see the button presses simultaneously, but rather a sequence of one button, then a short time later two buttons, then a short time after that three buttons. You will also get the same result when releasing the buttons, where it will go from three, to two, then one, then finally no buttons pressed.

I took the OP not to mean that they wanted to know that x buttons all became pressed at the same instant, but rather that at a particular instant x buttons where already down.

That is to say, this:

counting the number of buttons being pressed(held down constantly) at once.

... does not mean:

counting the number of buttons becoming pressed(held down constantly) at once.

... but rather:

counting the number of buttons that are alreadypressed(held down constantly) at once.

Then it's a simple matter to read all the pins at the top of loop() and count++ for each one that's low each time through and then reset count to 0.

I took the OP not to mean that they wanted to know that x buttons all became pressed at the same instant, but rather that at a particular instant x buttons where already down.

That is to say, this:

counting the number of buttons being pressed(held down constantly) at once.

... does not mean:

counting the number of buttons becoming pressed(held down constantly) at once.

... but rather:

counting the number of buttons that are alreadypressed(held down constantly) at once.

Then it's a simple matter to read all the pins at the top of loop() and count++ for each one that's low each time through and then reset count to 0.

The problem is, if someone tries to press three buttons at once, if the loop runs fast enough you are going to see one button pressed for a few iterations of the loop, then two buttons pressed for a few more iterations, then finally three buttons pressed. Not necessarily a problem, but its something the code has to expect and be able to handle properly.

david_2018:
The problem is.... Not necessarily a problem

Not sure what that means but the point is actually that the OP's statement of the requirement is ambiguous. I think he or she just wants to know that at time t=t1 "x" buttons are pressed and that at time t=t2 "y" buttons are pressed, and I wrote him or her two sketches (one ugly one, one array one) and PM'd them earlier today.

He or she can use them or not, depending on whether or not I understood the requirement correctly.

To clarify, I'm asking to have about 15 buttons available to be pressed. When one is pressed(held down), it adds to the count on the LCD screen. When another is held down at the same time, it adds another count to the LCD screen. When either of those buttons is un-pressed, it will remove a count from the LCD screen. Sorry if my wording is bad. So, it's mainly about counting the buttons being held down at once. How could I do this?

Pretty straightforward really. :sunglasses:

In the loop, you poll (test) each button in turn, adding to a count that you started at zero, with each button you find pressed.

At the end of the loop, you can - note this - see whether this count is the same as the last count you made.

If it is the same, you do nothing and let the loop return to the start by itself.

If it is different, then you save this value as the "last" value - for the next count cycle - and you update the display with this number.

Note that it is important not to do anything to the display if nothing has changed, as re-writing the display with the same value would cause annoying flicker. You may also wish to add a small delay - say 200 ms - to the loop so that button bounce does not cause display flicker.

Connect the buttons between a pin and ground and use INPUT_PULLUP on each pin to provide a pull-up to the button.

A Nano is the appropriate Arduino for this sort of project, use a 1602 LCD display with an I2C "backpack" fitted which connects to A4 and A5, and you can use pins 2 to 13 plus A0 to A2 as the button pins.

"pretty straightforward"

OK then, let's start.

What have you done so far? Any coding at all (with your Arduino and the Arduino IDE)?

Do you have your 1602 LCD? Which one? If not, what else? Clearly no point if you do not have the parts.

Can't even begin to suggest code until we know what the target is.

Read the instructions to understand how to post the code you have used so far.

“about 15 buttons available to be pressed.“

You could do 20 if you use your right toes too. :wink:

“ When one is pressed(held down), it adds to the count on the LCD screen. When another is held down at the same time, it adds another count to the LCD screen. . . . When either of those buttons is un-pressed, it will remove a count from the LCD screen. ”

This says ‘whenever’ a switch is first closed, increment the counter; decrement the counter when a switch is open, i.e. if two (or more) switches were closed.

Correct?

So far I don't have parts get and haven't started coding so bear with me. And yes I think that's correct. For example, any switch closed adds one to the count. Any other switch closed adds another. And so on. Then, any switch openned decrements the count. Another switch openned will decrements the count. So essentially it's 'counting the number of closed switches with every loop'
I don't have much experience with coding but I'm currently learning. :slight_smile:

If any of the switches going closed increments and opening any switch decrements then maybe all you need to do is wire the 15 switches in parallel to one Arduino input.

If however you want to hold a switch (closed) ‘while’ letting go of any switch (open) then you will need individual inputs.

An UNO has D2-D13 ‘twelve‘ and A0-A5 (D14-D19) ‘six’ that can be used as digital inputs.

A Mega has many more I/O pins.

What are you going to use this for?

Okay, I'm using this to have the arduino count the number of people holding onto the device (when they hold it, they need to hold a button so the arduino knows they are holding the device). It is going to be quite a long product as you can probably imagine. The device must count the amount of people who are closing a switch at a time and display this on an LCD screen. This should be for 15-20 people. Hope that's made it clearer and easier to understand :slight_smile:

-I get what you're saying. I would ideally like to have it so i can have any switch open and closed at any time rather than the parallel way you explained it, but it would be okay to do it the parallel way if that's MUCH easier. It doesn't compromise much having it that way, so if it's easier, let's do it that way. (Making it cheaper is also a factor)

Sounds like you need individual switches each on their own input.

You want to wire this for 15-20 switches (Maximum for an UNO is 12+6=18) .

If everyone sits down and closes a switch the counter will be 18.

If everyone opens their switch the counter goes to 0.

If only 4 people let go of their switch, the counter reads 14.

Now, if ‘one’ of the 4 persons pushes the switch the counter goes to 15.

This is very doable and quite easy to code.

How long are the switch runs?

Suggest long runs have a strong pull down with a .1uF capacitor across it.

Is this a voting machine?

EDIT:

Something similar.

You will need a display, I2C LCD with UNO (or use a Mega and a LCD or use serial out to a PC).

Original design had a LED at each switch box to show the user their switch was pushed (when LED was ON).

#define switchClosed  HIGH

unsigned long switchMillis;
unsigned long printMillis;

byte counter;

//******************************************************************************
void setup()
{
  Serial.begin(9600);

  //************************************
  for ( byte x = 2; x < 17; x++)
  {
    pinMode(x, INPUT_PULLUP);
  }

} //END of setup()

//******************************************************************************
void loop()
{
  //************************************
  if (millis() - switchMillis >= 50)
  {
    //resart timer
    switchMillis = millis();

    checkSwitches();
  }

  //************************************
  if (millis() - printMillis >= 1000)
  {
    //restart timer
    printMillis = millis();

    Serial.print("Number of switches pressed = ");
    Serial.println(counter);
  }

} //END of loop()

//******************************************************************************
void checkSwitches()
{
  counter = 0;

  //************************************
  for ( byte x = 2; x < 17; x++)
  {
    if (digitalRead(x) == switchClosed)
    {
      counter++;
    }
  }

} //END of checkSwitches()

//******************************************************************************

Wow, that diagram is a lot to take in but i get it :slight_smile: thankyou so much. I tried to draw up my own circuit diagram but i couldn't get my head round it. I'll definately try this as i have got an arduino uno at home. Im guessing the breadboard is going to be quite big...

It's just a person holding object counter haha. Could this be modified to have the LCD display?

Your understanding of what i aim to accomplish is spot on though.

“ Could this be modified to have the LCD display? ”

Yes, add an I2C LCD display to the UNO, pins A4 and A5.

This would be better as you have access to the UNO pins.

You need this card ‘and’ an appropriate LCD.

See:

I'm going to start my testing within 7 days but i need to gather all the parts. I have an uno thankfully but i just need to get the rest.

I'm going to try use the backpack LCD idea and follow the circuit diagram and see if it works with the code that you sent me. Thankyou! Will i need to solder the backpack onto the LCD? and on the diagram there are 4 wires coming from the LCD whilst on the picture of the backpack, there are 5 places for it to come out from. Do i just ignore the one not used in the diagram and follow as labelled?