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);
}
// 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);
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
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.