Counting Button Press not working

Hello,

I'm trying to count momentary button presses. I modified the button example to try it. It is not working. It compiles fine, and LED on Pin 13 will light up with the press of each button. But the serial monitor always reads "0" and does not count up and down. Your Help is appreciated.

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 28 Oct 2010
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 8;     // the number of the pushbutton pin
const int buttonPin2 = 9;  
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0; 
int presses  = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
pinMode(buttonPin2, INPUT); 
Serial.begin(9600);
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH); 
   presses++; 
  } 
  if (buttonState2 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH); 
   presses--; 
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
  int sensorValue = digitalRead(8);
  Serial.println(sensorValue, DEC);
  if (presses <= 0) {
    presses = 0;
  }
  if (presses >= 12) {
    presses = 12;
  }
}

OK I see what I messed up, I did not have the monitor reading the presses. The changed code is here, and it works, however now I have a new problem. Switch bounce is causing it to count unreliably. How can I fix Switch bounce in code? I'm not sure how to do that. Or is it better to use some type of hardware, like a flip flop to get rid of the switch bounce?

semi working code:

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 28 Oct 2010
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 8;     // the number of the pushbutton pin
const int buttonPin2 = 9;  
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0; 
int presses  = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
pinMode(buttonPin2, INPUT); 
Serial.begin(9600);
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH); 
   presses++; 
  } 
  if (buttonState2 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH); 
   presses--; 
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
  int sensorValue = presses; //digitalRead(8 && 9);
  Serial.println(sensorValue, DEC);
  if (presses <= 0) {
    presses = 0;
  }
  if (presses >= 12) {
    presses = 12;
  }
}

(deleted)

Delay did solve the problem. Thanks spycatcher2k
Is there a way to do it, without using Delay? Once I incorporate this into another code snippet, I don't want the delay to affect anything else the arduino may be doing.

Remember the millis() when you notice a button press, and ignore a button LOW if (millis() - remembered) is too small...

Yes, I never understood the flash without delay examples, and was never successful at using the millisec stuff. Do you have an example of how it could work?

knightschade:
Yes, I never understood the flash without delay examples, and was never successful at using the millisec stuff. Do you have an example of how it could work?

That's one of the more fundamental concepts behind the Arduino, so I would strongly recommend going through the examples to learn.

Any other suggestions? I went through the examples and don't understand. Perhaps someone can dumb it down a bit for me?

Thanks Nick that was excellent! It actually helped. Subconsciously I also have a hankering for eggs and bacon? :roll_eyes: