I am creating a memory game with the Arduino. It has four buttons, each for a different colored led. The leds will blink in a random sequence and either fail or add another light in the next sequence if the user presses the correct buttons. I have also attached an lcd screen to late implement a score tally.
Right now, the welcome lights display and the sound is working fine, but when the sequence starts, it will act as though I am pressing a button and proceed through the fail light method. I think the last method is causing this issue where I am comparing the button pressed to the sequence.
I would appreciate any help!
This is essentially my wiring just with different pins chosen: enter image description here
-
What have you done to try to understand the problem? Put some debug prints in, see where it's going wrong.Mark Smith– Mark Smith2018年04月19日 14:03:22 +00:00Commented Apr 19, 2018 at 14:03
-
I have outputted the button states as well as the button state right before it checks the sequence along with the seq. In the serial monitor it will say button3 = 1 when I have not pressed any buttons since the start of the program.Katie– Katie2018年04月19日 14:25:12 +00:00Commented Apr 19, 2018 at 14:25
-
the buttons in schema have pull-down resistorsJuraj– Juraj ♦2018年04月19日 18:49:33 +00:00Commented Apr 19, 2018 at 18:49
-
That was one of the things I was thinking could be wrong since I declare the pinModes with INPUT_PULLUP. Should I instead just use INPUT? Or would it be more efficient to not have the pull down resistors?Katie– Katie2018年04月19日 18:55:34 +00:00Commented Apr 19, 2018 at 18:55
2 Answers 2
I found that I did not need the pull down resistors for each button since I had declared the pinMode for each button to be INPUT_PULLUP. So instead of the schematic above, I took out the resistors for the buttons and instead wired one side of the buttons to ground.
The problem might be that you do not debounce the buttons. When you press a button, there is somehwere between fully pressed and not pressed (or backwards) a state there it 'bounces', e.g. you get lots of state transitions, resulting in seeing a button pressed twice or many times instead of once.
Check the debounce example in the default Arduino Tutorial
-
Thank you. I don't even press any buttons though, and it does this.Katie– Katie2018年04月19日 13:58:53 +00:00Commented Apr 19, 2018 at 13:58
-
Check the variable button just before FailLight, is it -1 (expected) or has it a button (press) value?Michel Keijzers– Michel Keijzers2018年04月19日 14:03:08 +00:00Commented Apr 19, 2018 at 14:03
-
the button value is 0 before the fail lightKatie– Katie2018年04月19日 14:11:50 +00:00Commented Apr 19, 2018 at 14:11
-
Are you sure that if(button0 == LOW){ should be LOW and not HIGH?Michel Keijzers– Michel Keijzers2018年04月19日 14:16:05 +00:00Commented Apr 19, 2018 at 14:16
-
well for the pinMode of the buttons, i set them to INPUT_PULLUP so doesn't that make the states opposite? (HIGH when not pressed and LOW when pressed)Katie– Katie2018年04月19日 14:20:04 +00:00Commented Apr 19, 2018 at 14:20