arduino problems

I have been working on this project for a while. I am new to this arduino format and would like some help with a problem. I was trying to to count the number of button pushes and on the third a buzzer would sound for a second. I wrote a program but when you turn it on the buzer sounds until you press the button then if you press it two more times the buzzer sounds but not for a second.

// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 12; // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

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

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter, DEC);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}

// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}

if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);

} else {
digitalWrite(ledPin, LOW);

}

}

sorry for the confusion. I am using a duemilanove and in the program where it says led or ledpin thats the buzzer. The schematic:
i have the 5v pin connected to the push button that in turn is connected to pin2 and a 10,000 ohms resistor which is connected to ground. Then from
pin12 i have a buzzer (sorry i don't know which type of buzzer) connected to ground.

if you need more info to help solve my problem just reply

if (buttonPushCounter % 4 == 0) {
   digitalWrite(ledPin, LOW);
 } else {
  digitalWrite(ledPin, LOW);
 }

Unless I'm missing something, that's not right. Whatever the state of buttonPushCounter, you're driving ledPin low.

I was trying to to count the number of button pushes and on the third a buzzer would sound for a second.

if (buttonPushCounter % 4 == 0)

buttonPushCounter buttonPushCounter % 4
0 0
1 1
2 2
3 3
4 0
5 1

Looks to me like it will take 4 pushes to get the buzzer to sound.

thanks for all the help but it when i turn the program on the buzzer sounds until you push the button then since i have the button pushes set to 4 and it counts the button push used to shut off the buzzer then it takes 3 more pushes before the buzzer will sound. what i want it to do is when you turn it on nothing will happen until you press the button 4 times then it will sound a buzzer for a second.

:o

Assuming your buzzer isn't some weird active-low device, then it sounds like you have a bit of a problem in your circuit as well as the code.

Something seems screwy here. Can you take a decent resolution picture of the circuit and connections?

It's buzzing when you turn it on because buttonPushCounter % 4 == 0 when buttonPushCounter == 0 as PaulS showed above. Modify this statement:

if (buttonPushCounter % 4 == 0)

like this:

if (buttonPushCounter % 4 == 0 && buttonPushCounter > 0)

Also, you said you only want it to sound for one second. You could implement that with a delay after you turn it on:

 if (buttonPushCounter % 4 == 0 && buttonPushCounter > 0) {
   digitalWrite(ledPin, HIGH);
   delay(1000);

 }

Or by setting a variable to millis() when you turn it on, then turning it off when millis() -1000 > your variable.

It's buzzing when you turn it on because buttonPushCounter % 4 == 0 when buttonPushCounter == 0 as PaulS showed above.

Hahahah, I missed that one.

Doh.

thanks for every thing every one ;D