const int buttonPin = 2; // set the input pin for the push button
int buttonState = 0; // variable for reading the push button state
int count = 0; // variable for counting the number of button presses
int led = 7;
void setup() {
pinMode(buttonPin, INPUT); // set the push button pin as input
Serial.begin(9600); // initialize serial communication at 9600 bits per second
pinMode(led,OUTPUT);
Serial.println("Buttton Counter");
}
void loop() {
buttonState = digitalRead(buttonPin); // read the state of the push button
if (buttonState == HIGH) { // if the button is pressed
count++; // increment the count variable
digitalWrite(led,HIGH);
Serial.print("Button pressed. Count = "); // print a message to the serial monitor
Serial.println(count); // print the count variable to the serial monitor
delay(500); // wait for half a second to avoid multiple counts for one button press
digitalWrite(led,LOW);
}
}