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