Here is the program Auto Formatted in the IDE and with each { and } on its own line.
Doing this makes it much more obvious whech { belongs with which }
In the IDE, if you click just after a { then the corresponding ? will be indicated
// declarations
// constant variables
const int buttonPin = 2;
const int ledPin = 13;
// variable variables
int buttonCount = 0; // counts the button presses
int buttonState = 0; // current number of button presses
int lastButtonState = 0; //last number of button presses
void setup ()
{
// code to run once
pinMode (buttonPin, INPUT);
pinMode (ledPin, OUTPUT);
Serial.begin (9600);
}
void loop()
{
// put your main code here, to run repeatedly:
// read the buttonPin
buttonState = digitalRead (buttonPin);
// compare buttonState to the lastbuttonState
if (buttonState != lastButtonState)
{
// if the buttonState is high then the button went from Off to On
if (buttonState == HIGH)
{
// then increment the buttonCount
buttonCount ++;
// and print to the serial monitor
Serial.println("On");
Serial.print("number of button pushes: ");
Serial.println(buttonCount);
// but if it didn't
}
else
{
// then if the buttonState is low, it went from On to Off
Serial.println ("Off");
}
delay (50);
lastButtonState = buttonState;
// turn on the LED every four button pushes by checking the modulo of buttonCount
if (buttonCount % 4 == 0)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite (ledPin, LOW);
}
}
}
Each {} pair encloses a block of code such as here
if (buttonState == HIGH)
{
// then increment the buttonCount
buttonCount ++;
// and print to the serial monitor
Serial.println("On");
Serial.print("number of button pushes: ");
Serial.println(buttonCount);
// but if it didn't
}
If the test at the start returns true then the block of code will be executed.