Reading button presses

Hie guys,

I was compiling a source code which i want to read specific button presses and switch on
an Led. Briefly there is a single button input and two leds, Led1 and Led2. If two button counts have been detected Led1 blinks once and if three button counts have been detected Led2 blinks once.

When i compiled the code, it had no challenges, but when i run the script on the arduino board
i seem not to be getting a response.
Your inputs would be very helpful cause am stuck… :confused:

Button_Presses_Schematic.ino (1.24 KB)

Why does your code properly refer to switches, while your post refers to buttons?

Why do you read the state of a pin with a switch connected to it, and increment buttonPresses? You don't have buttons sewn onto the Arduino, do you?

How IS your switch wired?

if (ButtonPresses == 4) {
  int ButtonPresses = 0;   // Reset the variable to zero
}

If the "button" was pressed 4 times, create a new variable with the same name as the global variable, and set it to 0, leaving the global value unchanged. Why on earth would you do THAT?

@ PaulS thanks for your feedback. Am using a button, although i had named it SwitchPin. Sorry for naming error. Any suggestions on how can make the code work ?

Looking to your feedback

Look at what @PaulS wrote

if (ButtonPresses == 4) {

int ButtonPresses = 0;  // Reset the variable to zero
}



If the "button" was pressed 4 times, create a **new** variable with the same name as the global variable, and set it to 0, **leaving the global value unchanged**. Why on earth would you do THAT?

And to make it even more clear, int ButtonPresses creates a new variable.

Until you KNOW that the switch is wired correctly, and that LOW means pressed, dump the code that counts switch presses, and the code that uses the number of switch presses to do anything.

Just Serial.print() a message when the switch becomes pressed. See the state change detection example.

Print a message when the switch becomes released.

When that works, and not a microsecond before that, you can add code to count the number of times the switch became pressed, and you can add code to deal with the count.

250 milliseconds is an eternity for the switch to be bouncing.

This

if (digitalRead(SwitchPin1) == LOW)  // Check if button is low
{
  ButtonPresses ++ ;            //Increasing the variable every time the button s pressed
  delay(250);                 //Debounce button press
}

does not debounce

See https://www.arduino.cc/en/Tutorial/Debounce

jbellavance:
This

does not debounce

It does not matter, since even the crappiest switch won’t bounce for 250 milliseconds.

It does not matter, since even the crappiest switch won't bounce for 250 milliseconds.

Agreed.

It was just to mention that the switch still needs debouncing.

Until you KNOW that the switch is wired correctly, and that LOW means pressed, dump the code that counts switch presses, and the code that uses the number of switch presses to do anything.

Just Serial.print() a message when the switch becomes pressed. See the state change detection example.

IMHO, it is important to take this into consideration to get the switch to work properly so that Serial.print() returns something predictable.

PaulS:
It does not matter, since even the crappiest switch won't bounce for 250 milliseconds.

that is kinda like a stuck switch LOL..

ts, do take a picture of ur wiring.