counting button presses

I am trying to count the number of times I press a button on pin 12. When I press the button it writes digital pin 2 HIGH turning on an LED. I want digital pin 3 to turn on after the button is pressed 4 times. The LED on pin 2 is fine, turning on when the button is held down and turning off when the button is released. Pin 3 however turns on upon the first button press and remains on forever. How do I make Pin 3 turn on only after 4 button presses?

Here is my code:

int button = 12;
int LED1 = 2;
int LED2 = 3;
int presses = 0;
void setup() {
  pinMode(button, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}
 void loop() {
   if (digitalRead (button) == HIGH){
     presses = (presses + 1);
     digitalWrite (LED1, HIGH);
   }
   if (digitalRead (button) == LOW){
     digitalWrite (LED1, LOW);
   }
   if (presses > 3){
     digitalWrite (LED2, HIGH);
   }
}

you have to distinct between when the button is pressed from when the button is pressed and later on released.

so, you will add a variable that remembers the state of the button and only when the button has been released you will count a new button press.

HTH

Look at the Debounce example sketch. You can add a counter to it do something when the count reaches a predefined number.

Thanks for the advice. Looking at some different debounce programs helped a lot! Here is the code I came up with today. It is the prototype of the ammunition control in a laser tag system I am making. A bar of ten LEDs indicates the percentage of 50 total shots left in the laser gun. Upon pressing the reload button on pin 14 the bar graph powers back up one LED at a time. For the reload button on pin 14 should I just copy paste the debounce code for the trigger button? Right now it is just reading the reload button it straight up with no debounce.

int triggerPin = 12;
int ammoPin = 14;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;
int LED6 = 7;
int LED7 = 8;
int LED8 = 9;
int LED9 = 10;
int LED10 = 11;
int triggerreading;      // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
int count=0; //number of times button was pressed

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;     // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(triggerPin, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);
  pinMode(LED10, OUTPUT);
  reload();
}
void reload()
{
  digitalWrite(LED10, HIGH);
  delay(500);
  digitalWrite(LED9, HIGH);
  delay(500);
  digitalWrite(LED8, HIGH);
  delay(500);
  digitalWrite(LED7, HIGH);
  delay(500);
  digitalWrite(LED6, HIGH);
  delay(500);
  digitalWrite(LED5, HIGH);
  delay(500);
  digitalWrite(LED4, HIGH);
  delay(500);
  digitalWrite(LED3, HIGH);
  delay(500);
  digitalWrite(LED2, HIGH);
  delay(500);
  digitalWrite(LED1, HIGH);
}
  
void loop()
{
  triggerreading = digitalRead(triggerPin);

  // if we just pressed the button (i.e. the input went from LOW to HIGH),
  // and we've waited long enough since the last press to ignore any noise...  
  if (triggerreading == HIGH && previous == LOW && millis() - time > debounce) {
    //increment count
    count++;

    // ... and remember when the last button press was
    time = millis();    
  }
  previous = triggerreading;

  if (count==5)
  {
    digitalWrite(LED1, LOW);
    //count=0; //reset count
  }
  if (count==10){
  digitalWrite(LED2, LOW);
}
if (count==15){
  digitalWrite(LED3, LOW);
}
if (count==20){
  digitalWrite(LED4, LOW);
}
if (count==25){
  digitalWrite(LED5, LOW);
}
if (count==30){
  digitalWrite(LED6, LOW);
}
if (count==35){
  digitalWrite(LED7, LOW);
}
if (count==40){
  digitalWrite(LED8, LOW);
}
if (count==45){
  digitalWrite(LED9, LOW);
}
if (count==50){
  digitalWrite(LED10, LOW);
}
if (digitalRead(ammoPin) == HIGH){ //if reload button is pressed
  count=0;
  reload();
}

}

williamanos: I am trying to count the number of times I press a button on pin 12. When I press the button it writes digital pin 2 HIGH turning on an LED. I want digital pin 3 to turn on after the button is pressed 4 times. The LED on pin 2 is fine, turning on when the button is held down and turning off when the button is released. Pin 3 however turns on upon the first button press and remains on forever. How do I make Pin 3 turn on only after 4 button presses?

Check this out http://arduino.cc/en/Tutorial/ButtonStateChange

I have implemented this in my sketch. I use it to count up to 9 button pushes, each doing their own function, and then reset to zero when it has passed 9.

Here's a little snip

//----------------------------------  Button Counter Control- USED FOR TIMING CONTROL
       buttonstate = digitalRead(timingPIN);
       if ((buttonstate != lastbuttonstate) && (buttonstate == HIGH)){ buttonpushcounter++;} // If the buttonstate is greater than last, and it is high, increase counter
       lastbuttonstate = buttonstate;
       if( buttonpushcounter > 9) {buttonpushcounter = 0;} // If buttonstate has been pushed more than 9 times, reset to 0