element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Waiting for input from & debouncing one of multiple buttons
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 2 replies
  • Subscribers 389 subscribers
  • Views 1997 views
  • Users 0 members are here
  • debouncing
Related

Waiting for input from & debouncing one of multiple buttons

opalko
opalko over 4 years ago

Very new to Arduino. What I am trying to build from scratch is a Simon type game: 4 buttons with an LED that corresponds to each button.  The computer generates a random sequence of lighting the LED's, the player has to repeat the sequence by pressing the appropriate button for the LED, whereupon the computer shows the lighting of the next LED in the sequence one at a time until the user completes the sequence or hits an incorrect button.

 

What I am having trouble with (I think) is waiting for input on one of four buttons and debouncing that input so that the input is not registered as more than one press.  I started with the Bounce2 library but got lost quickly with how to use it in an array and then came across j-bellevance's EdgeDebounceLite library .  I tried to incorporate it into my code but the results of repeating the sequence by pressing buttons is successful sometimes and fails other times, even if it is just one item in the sequence (when it is clearly correct), so I don't know if debouncing is working incorrectly or it is something else.   Here is my code for checking player input:

 

void checkPlayerInput() {
  count = 0;
  while (inputCorrect == true && count <= sequenceCounter) {
   notPressed = true;
   while (notPressed) {
    buttonStatus0 = debounce.pin(buttonPin[0]);
    buttonStatus1 = debounce.pin(buttonPin[1]);
    buttonStatus2 = debounce.pin(buttonPin[2]);
    buttonStatus3 = debounce.pin(buttonPin[3]);
    if (buttonStatus0 == LOW) {     
     // turn LED 1 on:    
     digitalWrite(ledPins[0], HIGH);
     pressedButton = 0; 
     delay(150);
     digitalWrite(ledPins[0], LOW);
     // set notPressed flag
     notPressed = false;
    } else if (buttonStatus1 == LOW) {     
     // turn LED 2 on:    
     digitalWrite(ledPins[1], HIGH);
     pressedButton = 1;
     delay(150);
     digitalWrite(ledPins[1], LOW);
     // set notPressed flag
     notPressed = false;
    } else if (buttonStatus2 == LOW) {     
     // turn LED 3 on:    
     digitalWrite(ledPins[2], HIGH);
     pressedButton = 2;       
     delay(150);
     digitalWrite(ledPins[2], LOW);
     // set notPressed flag
     notPressed = false;
    } else if (buttonStatus3 == LOW) {     
     // turn LED 4 on:    
     digitalWrite(ledPins[3], HIGH);
     pressedButton = 3; 
      delay(150);
     digitalWrite(ledPins[3], LOW);
      // set notPressed flag
     notPressed = false;
    }  
   } //while 1
      currentItemInSequence = randArray[count];
   if (pressedButton == currentItemInSequence) { 
    count++;
     } else {
    inputCorrect = false;
   }
 } // while 2
}

 

As I said this doesn't seem to always verify the input as correct every time (most times it is ok) even when the correct sequence is entered. 

 

I went back to j-bellevance's "array of pins" example and tried it to see how the debouncing worked, but I am not sure even that is working correctly.  Here is the code from his github "array of pins" example :

 

include <EdgeDebounceLite.h>
EdgeDebounceLite debounce;

byte buttonPins[] {2, 3, 4, 5};

void setup() {
  for (byte i = 0 ; i <4 ; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
  Serial.begin(9600);
}

void loop() {
  for ( byte i = 0 ; i < 4 ; i++) {
    if (debounce.pin(buttonPins[i]) == LOW) {
      Serial.print(F("Button on pin "));
      Serial.print(buttonPins[i]);
      Serial.println(F(" was pressed."));
    }
  }
}

 

As it is, when I run this, I get "Button on pin xxx was pressed" for as long as I hold down the button and not just one "Button on pin xxx was pressed" as I would expect. Like this for just one quick button press:

 

15:12:19.598 -> Button on pin 3 was pressed. 
15:12:19.598 -> Button on pin 3 was pressed. 
15:12:19.598 -> Button on pin 3 was pressed. 
15:12:19.598 -> Button on pin 3 was pressed. 
15:12:19.598 -> Button on pin 3 was pressed. 
15:12:19.598 -> Button on pin 3 was pressed. 
15:12:19.632 -> Button on pin 3 was pressed. 
15:12:19.632 -> Button on pin 3 was pressed. 
15:12:19.632 -> Button on pin 3 was pressed. 
15:12:19.632 -> Button on pin 3 was pressed. 
15:12:19.632 -> Button on pin 3 was pressed. 
15:12:19.632 -> Button on pin 3 was pressed. 
15:12:19.632 -> Button on pin 3 was pressed. 
15:12:19.665 -> Button on pin 3 was pressed. 
15:12:19.665 -> Button on pin 3 was pressed. 
15:12:19.665 -> Button on pin 3 was pressed. 
15:12:19.665 -> Button on pin 3 was pressed. 
15:12:19.665 -> Button on pin 3 was pressed.

 

 

Is this correct?  Is there a better way to wait for input on multiple pins and debounce appropriately?

Thanks for any help

Robert Opalko

  • Sign in to reply
  • Cancel

Top Replies

  • fmilburn
    fmilburn over 4 years ago +4 verified
    Hi Robert, The EdgeDebounceLite library that you referenced uses an algorithm based on an article by Jack Ganssle that I am familiar with. The algorithm ensure that the bounce is removed but does not manage…
  • opalko
    opalko over 4 years ago in reply to fmilburn +2
    Frank, thanks for this. I didn't realize the state of the switch wasn't managed - it makes sense now! In between my post and your reply I ended up going back to the bounce2 library and using that and getting…
Parents
  • fmilburn
    0 fmilburn over 4 years ago

    Hi Robert,

     

    The EdgeDebounceLite library that you referenced uses an algorithm based on an article by Jack Ganssle that I am familiar with.  The algorithm ensure that the bounce is removed but does not manage the state of the switch - you have to do that in your code.  As long as the button is low (and has passed the debounce check) it will record low as you observed above.  So what you have to do is watch for changes in both high and low states and act accordingly.

     

    It isn't Arduino but find some code of mine here based on the same debouncing algorithm.  Look at lines 38 or so through to 47.  The code is waiting for a change of button state and then takes the appropriate action depending on whether it has gone high or low.  If there hasn't been a change then nothing happens.

     

    Frank

     

    PS:  The article by Ganssle is worth reading as well as the Hackaday article I reference in my post even if you use the Arduino library and don't write your own.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • opalko
    0 opalko over 4 years ago in reply to fmilburn

    Frank, thanks for this. I didn't realize the state of the switch wasn't managed - it makes sense now!  In between my post and your reply I ended up going back to the bounce2 library and using that and getting it to work.  I may try it again with the EdgeDebounceLite library to see if I can make it work also. 

     

    I will look up the code on your blog and the Ganssle article as well  - I get his newsletter by email now but I will admit that I don't understand most of what is being discussed! image

     

    Thanks again

    Robert Opalko

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • opalko
    0 opalko over 4 years ago in reply to fmilburn

    Frank, thanks for this. I didn't realize the state of the switch wasn't managed - it makes sense now!  In between my post and your reply I ended up going back to the bounce2 library and using that and getting it to work.  I may try it again with the EdgeDebounceLite library to see if I can make it work also. 

     

    I will look up the code on your blog and the Ganssle article as well  - I get his newsletter by email now but I will admit that I don't understand most of what is being discussed! image

     

    Thanks again

    Robert Opalko

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube