Button Press Count

So I am new to using and coding in Arduino and have almost no experience besides some basic tutorials. What I want to do is use the push button that comes with the arduino to control an LED. I want the LED to stay on if the button has been pushed 5 times or less, and then turn off when the it has been pushed 6 times. I was trying to use a If…Else… logic, Attached is what I have so far. The code runs but the light just stays on, I believe this is due to the fact that there is no count of the button press, but I’m not sure how to fix this issue.

int p=0;
void setup() {
  // put your setup code here, to run once:
  pinMode(2,INPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  p=digitalRead(2);
  if (p <=5)
  {
    digitalWrite(13, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
  }
}

ButtonPressCode.txt (307 Bytes)

Read through the 'how to use this forum' posts at the top of certain topics. Lots of useful info including, information on posting using code tags.

In the Arduino IDE are examples demonstrating basic concepts (IDE/file/examples/...). Among the most basic are debouncing and state change detection. The code's already written for you and many require only an external switch wired to an input. Study them. If you don't know what something like if([i]condition[/i]) means go to the reference page for a brief explanation.

What I want to do is use the push button that comes with the arduino to control an LED.

Does this mean the button mounted on the Arduino board or, a switch, along with some other parts, that came packaged with the Arduino?

Sorry, I'm still learning how to do this correctly. I posted my code using the code tags now. The switch just came in a kit I got and its attached to the breadboard. It's just a normal push button switch. In searching around I have not been able to figure out the best way to incorporate button press count into my code. I have seen one other example of count on this website but it was much longer and I could not comprehend how to use it with my code.

p=digitalRead(2);
if (p <=5)

Your basic tutorials should have told you that a digitalRead is going to give you either a HIGH (1) or LOW (0) result. Depending on how the switch is wired (most connect the switch to ground and configure the pin as INPUT PULLUP) you can then test the state as being ON or OFF (pushed or un-pushed) and if so, increment a counter (counter++;). Be careful with your timing as every time you whip around the loop, the switch may still be pressed from the last time you registered it, so you don’t want to count it twice. To avoid that, use a flag (throw-away boolean variable) and set it WHEN the switch gets pressed and you increment the counter. Then when you come back again to check the state of the switch and it is still engaged you know to ignore it. When the switch becomes dis-engaged, clear the flag and you’re ready to mark the next button push.

p=digitalRead(2);
  if (p <=5)
  {
    digitalWrite(13, HIGH);
  }

This is reading your button pin…
But as noted above, digitalRead() will only ever return 0 or 1, so the if() will always be true.

As you commented earlier, you need to ‘count’ the presses, and only then perform an action.
Also read about and use ‘state change’ detection, because your current loop will execute far faster than you can press & release the button…

Good luck.

If you want to count presses of the button and not just count up as fast as you can the whole time the button is held down, then you should also take a look at the "State Change Example" and think about only adding to the count once when the button first changes from not-pressed to pressed.

After meeting with a professor I was able to get the code to work out how I wanted it to. The following is the code we wrote in case anyone else ever needs it. Thank you so much to everyone who helped me with this question.

byte ButtonDown =0;
byte ButtonCounter =0;
void setup() {
  // put your setup code here, to run once:
    Serial.begin(9600);
  pinMode(2,INPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

}

void loop() {
  // put your main code here, to run repeatedly:
  
  if (digitalRead(2)) //button up
  {
    ButtonDown=0;
  }
  else  //button down
  {
    if (!ButtonDown) //button was previously up
    {
      ButtonCounter++;
      ButtonDown=1;
    }
  }
  if (ButtonCounter >=5) 
  {
    digitalWrite(13, LOW);
  }
   Serial.println(ButtonCounter);
  delay(10);        // delay in between reads for stability

// delay in between reads for stability

While this does reduce the problem of debouncing the button it locks up the Arduino for a large amount of time. It could literally do 160,000 things in that time.

As you move on to larger projects, you will have to move away from using delay() like this.