Arduino Syntax and Program FlowSyntaxSyntax in Arduino signifies the rules need to be followed for the successful uploading of the Arduino program to the board. The syntax of Arduino is similar to the grammar in English. It means that the rules must be followed in order to compile and run our code successfully. If we break those rules, our computer program may compile and run, but with some bugs. Let's understand with an example. As we open the Arduino IDE, the display will look like the below image: The two functions that encapsulate the pieces of code in the Arduino program are shown below: - void setup ( )
- void loop ( )
Functions- The functions in Arduino combine many pieces of lines of code into one.
- The functions usually return a value after finishing execution. But here, the function does not return any value due to the presence of void.
- The setup and loop function have void keyword present in front of their function name.
- The multiple lines of code that a function encapsulates are written inside curly brackets.
- Every closing curly bracket ' } ' must match the opening curly bracket '{ ' in the code.
- We can also write our own functions, which will be discussed later in this tutorial.
Spaces- Arduino ignores the white spaces and tabs before the coding statements.
- The coding statements in the code are intent (empty spacing at the starting) for the easy reading.
- In the function definition, loop, and conditional statements, 1 intent = 2 spaces.
- The compiler of Arduino also ignores the spaces in the parentheses, commas, blank lines, etc.
Tools Tab- The verify icon present on the tool tab only compiles the code. It is a quick method to check that whether the syntax of our program is correct or not.
- To compile, run, and upload the code to the board, we need to click on the Upload button.
Uses of Parentheses ( )- It denotes the function like void setup ( ) and void loop ( ).
- The parameter's inputs to the function are enclosed within the parentheses.
- It is also used to change the order of operations in mathematical operations.
Semicolon ;- It is the statement terminator in the C as well as C++.
- A statement is a command given to the Arduino, which instructs it to take some kind of action. Hence, the terminator is essential to signify the end of a statement.
- We can write one or more statements in a single line, but with semicolon indicating the end of each statement.
- The compiler will indicate an error if a semicolon is absent in any of the statements.
- It is recommended to write each statement with semicolon in a different line, which makes the code easier to read.
- We are not required to place a semicolon after the curly braces of the setup and loop function.
Arduino processes each statement sequentially. It executes one statement at a time before moving to the next statement. Program FlowThe program flow in Arduino is similar to the flowcharts. It represents the execution of a program in order. We recommend to draw the flowchart before writing the code. It helps us to understand the concept of code, which makes it the coding simpler and easier. Flow ChartsA flowchart uses shapes and arrows to represent the information or sequence of actions. An oval ellipse shows the Start of the sequence, and a square shows the action or processes that need to be performed. The Arduino coding process in the form of the flowchart is shown below: Here, the processor enters our code, and the execution of code begins. After the setup, the execution of the statement in the loop begins. The example of the flowchart in Arduino is shown below:
|