Learning braces

Hi,
I'm trying to learn syntax and am using the edge detection tutorial as practise. I can't get which starting brace works with which ending one.
I can't read code and translate to pseudo code to plain English yet to work out the logical grouping of statements.
Some help, using this as an example, would be great.

Cheers guys.

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

} 
}

(deleted)

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.

Put the brace on a line by itself, then press CTRL+t to format the code.

Or edit the auto Format parameters to make CTRL+t to put the braces on their own line automatically.

UKHeliBob:
Or edit the auto Format parameters to make CTRL+t to put the braces on their own line automatically.

Oooo! You can do that? Where?

(deleted)

(deleted)

I'm glad you found it.

It is easy once you know that it is possible, and there are other options too as you will have seen

I found a post with this link to the Artistic Style page that lists the option parameters for the auto format function (program).

Great; thank you all. I've learnt loads :slight_smile:

You don't need "--style=allman", "--style=bsd", "--style=break", and "-A1" since they all mean the same thing. You only need one of them. Other styles are available. The options with "--" don't need "--" in the configuration file.

For Mac users:
Ctrl-Click on Arduino.app and select "Show Package Content". The formatter.conf file is in Contents/Java/lib.

Note that the Arduino IDE uses an outdated version of Artistic Style (2.05.1) and that documentation is for 3.1 so not all the options on that page work.

jonwasser, thankyou - helpful.Pert - noted. A bit beyond me, but thanks