Button programming

Hi!

I have a memory game project and for that I need to use buttons and leds. The thing I want is if the user pushes the first button, the first led starts to light. If the second button is pressed the second led should light. But maybe I missed somethink when learnt how the buttons should be programmed because when I check if the first button is pressed the first led is starts lighting, but when I press the second button the first led starts lighting again (the second led won't because I didn't write the code for that, but it shouldn't light the first led either). Its not finished I know. I didn't wrote the code for all the buttons and leds because I just tested. I don't ask you guys to solve the whole thing, just to help me why is this bug appears.

  • The switch wiring is very weird, not sure what you are trying to do.
  • LEDs need series current limiting resistors.

FYI

When I run the simulation you link, and hit the Pause button, the places where the switches are wired to all read "input floating".

This means the value returned by digitalRead() for the buttons will be HIGH or LOW, but not necessarily because you are, or are not, pressing the buttons.

The buttons connect the diagonally opposite corners.

In the wokwi, wire the switch between ground and the pin, and use INPUT_PULLUP mode. One side to ground, a diagonally opposite corner to the corresponding input pin.

Expect digitalRead() to return LOW when the button is being held down (pressed). I haven't looked at your code to even guess what does what yet.

a7

I already did what you said. I did the wireing the same and I use INPUT_PULLUP as pinmode for those pins where I actually check if the button pressed or not

Good. Post the link to the corrected wokwi simulation.

a7

I mean no need to chanche the original wokwi link because I used those things for the first try also

Oh dear, forgive me, I did not see that you have as @LarryD did see, a very unusual arrangement for the buttons.

Buttons are not typically connected to two inputs.

Explain why you did that.

Pins 17..21 will always read LOW, you've wired them to ground.

Pins 3..7 will function and should read LOW when you press them.

a7

1 Like

I thought one of them is for the input and the other is for checking the value. Now I think so I missed it but can you explain it a little bit more?

  • No.

  • Inputs 4, 5, 6 and 7 are the correct way to wire the switches.

  • These inputs are used as S3 is shown in the image example offered you.

1 Like

Now the main problem is solved, thanks a lot. But there is like a bug, cause sometimes I press any button its like it stuck and the led lights permanently. I mean hardly ever but sometimes they do. Is it only a bug or this is the "bouncing problem"?

I would say what you said this way:

The button places a value on the input pin, digitslRead() is for checking the value.

Read more code. Read more about coding. You never saw such a circuit for a button ever, anywhere.

Now you code has some problems, at the very least it does not appear the you ever change pastTime.

Even when you do, put your finger on the code and see what happens, I think you've not got the pattern correct if you are working with millis() to only do something if it is time.

And your else clause is always going to turn off alla LEDs soon enough you won't see the, I think.

Can't read it too well as I am in transit looking through the tiny window.

a7

Oh, new code.

Now I see the else clause never runs.

The observed behaviour is due to contact bounce on the switches, google and read about switch debouncing

arduino switch debouncing

or

arduino button debouncing

Sry, can't point you to a good article just now.

a7

  • Probably on the way to the beach :sunglasses:
1 Like

I wrote its not finished. I know about the "pastTime" I should update it at the end of the if statement and so on. I know there is a lot to do on this code but I tried to test if the main things works

@botond500 in the wokwi, you can actually get perfect buttons… it's a crutch and you will need to overcome it one day, but

click on a button, and you get a pop up where you can turn off simukated debouncing.

This is handy if you just want to see the effect of bouncing, or if you are using the simulator for another issue and just don't want to bother with doing the proper treatment necessary IRL.

HTH

a7

1 Like

Thank you guys! I know I have a lot to learn about arduino, electronics and coding as well. I try and now I understand more. Now its up to me so I will do the rest and learn learn learn....

And… be sure to account for this:

The digitslRead() of a button in that mode and configuration will be LOW when it is being pressed, and HIGH when you finger is off it.

Good luck, we here when you get stucked.

a7

1 Like

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