Project with tactile buttons

Hello
I know I'm a newbie in arduino. I try to do little fun things by "stealing" pieces of code and then inserting them into my code. Then I learn a little along the way.
I'm creating a code with five buttons (the small arduino tactile buttons) which must activate each LED when pressed (and turn off the others - but I have that part under control).
My problem is that I don't have an action when I press. Sometimes I have to press 10 times, other times just once.
The code below on one button.
The button is mounted with 5V and GND with 10K resistor on one side, signal to board on the other.
As I said - it works... but it's not guaranteed to work

  Serial.begin(9600); 
 byte buttonStateGreen = digitalRead(btnPinGreen);  
 buttonStateGreen = pulseIn(btnPinGreen, LOW, 1500000);
 if (buttonStateGreen > 50) {
 digitalWrite(ledGreen, HIGH);
  }

Please help me :slight_smile:

Take the delays out of your code. The usual beginners logic problem!

Look up debounce

Newbie u know - please describe "delay".
If its 1500000, then I have tried that - without luck

Thnx - can u give an example

That seems totally wrong.
You never declared the pin mode as INPUT.
pulseIn returns an unsigned long.

Basically don't use pulseIn to read the state of a pin, just use digitalRead

In the IDE, click on Files->Examples->02 Digital->Button

Start with Buton, then go on to Bounce. Now try to do what you were doing with just one button. Once that works, scale it up to 5. The samples are in the IDE.

SRY - I did mean:
The button is mounted with 5V and signal to board on one side, GND with 10K resistor on the other.

OK so the button is correctly connected.
So try the examples.

Thanks - I've learned two things:

  1. Keep it simple
  2. Look right inside the IDE if there should be a solution - which there apparently was here!

I think I got the answer to my problem; unless something else occurs in the same code :slight_smile: !

You would be surprised at all the useful stuff there. Also, when you install a library, they usually have examples with them, so you should check the examples folder for New examples

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