====== The syntax and the structure of the program ======
===== Syntax =====
Arduino IDE is a software that allows to write Arduino code. Each file with Arduino code is called //a sketch//. The Arduino programming language is similar to the C++ language. In order for the Arduino IDE to compile the written code without errors, it is important to follow the pre-defined syntax.
=== Define ===
"//#define//" is a component that allows to give a name to a constant value at the very beginning of the program.
#define constant 4
In this example the value 4 is assigned to the //constant//.
Note that at the end of this expression semicolon (;) is not necessary and between the name and the value, the sign of equality (=) should not be added!
=== Include ===
"//#include//" is a component that allows to include libraries from the outside of the program. Just like the //#define//, //#include// is not terminated with the semicolon at the end of the line!
#include
In this example the library called "//Servo.h//" that manages servomotors has been added to the sketch.
=== Comments ===
There are two ways to write comments in the sketch so that the written text is not compiled as a part of the running code.
//Single line comment is written here
The double backslash is used when only single line should be commented.
/*Multi-line comments are written here
Second line of the comment
...
*/
In this way the backslash followed by asterisk defines the beginning of the block comment and the asterisk followed by backslash defines the end of the block comment.
=== Semicolon ===
The semicolon (//;//) is used at the end of each statement of the code.
int pin = 5;
In this example a single statement is //int pin = 5// and the semicolon at the end tells compiler that this is the end of the statement and it can continue with the next one.
=== Curly braces ===
Curly braces are used to enclose further block of instructions. Curly braces usually follow different functions that will be viewed in the further sections.
Each opening curly brace should always be by a closing curly brace. Otherwise the compiler will show an error.
void function(datatype argument){
statements(s)
}
Use of curly braces in the own defined function.
===== Structure of the program =====
Below is given the example, how a new empty sketch looks like.
Each Arduino sketch contains multiple parts:
- Global definition section - the section where variables and constants are defined that working range is in the whole sketch that means both in the initialization and the loop sections. This section is at the very beginning of the sketch, before the setup function.
- Initialization section - is executed only once before executing the basic program. In this part usually all variables, I/O of the board pins, constants, are defined, etc. The most essential is to define all inputs and outputs that will be used in the program that defines how each pin will be used.
The construction of the setup function:
void setup() //The result data type and the name of the function
{ //Beginning of the initialization function
//The body of the function - contains all executable statements
} //The end of the initialization function
As it was already mentioned, this function will execute only once. The function is described by the result data type (number, symbol array or something else). In this example the key word //setup// means that the setup function does not have the result that means it is executed only once and that is all. The name necessarily should be //setup//, so that the built-in subsystem of the board program execution could differ the initialization section from the rest of the code.
- Loop section - the part that is executed continuously, it reads inputs, processes data and gives output signals. After executing the last statement of the function the program continues again with the first statement of the same loop function. This is the main part of the program execution that encloses all the steps of program execution, including the logic.
The construction of the loop function is the following:
void loop() //The result data type and the name of the function
{ //Beginning of the loop function
//Logic //The body of the function - contains all executable statements
} //The end of the loop function
The result data type of this function is the same as previous - //void// - that shows that the function does not have the result, it will be executed in the loop continuously while the program is working.
===== Blink LED =====
The code of the //Blink LED// program code will be viewed now. The example can be opened by following the path in Arduino IDE: //File->Examples->01.Basics>Blink//.
When the //Blink LED// example program is opened, the following sketch should open in the programming environment:
The code of the example program is the following:
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output. LED_BUILTIN stands for the built-in LED on the board.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
In the source code of program following things can be seen:
- It it defined that the LED_BUILTIN is set to be the output of the program. In this example sketch, the output periodically sends the specific signal that is in the level of the logical 1 (+5V) and 0 (0V). Sending output signal to the built-in LED, the LED is periodically turned on and off.
- Continuous executable function //loop()// is created that allocates 1 second (1000 ms) of time to each level of signal. It is done by pausing the execution of program. While the program is not changing the states of the inputs/outputs, they remain unchanged. In this way, when the +5 V signal is sent to the LED output and the program execution is paused, the LED will continue to shine until the level of the output will be set to 0 V.
- The last row indicates that the program will be paused for a 1 second also when the output level is set to be 0 V. In this way the period of LED on and off are equal. After executing this program, the program returns to the first line of the //loop()// function and the execution starts from the beginning.
===== Hello World =====
"//Hello World//" program is the simplest program, because it simply outputs the text to the screen.
Here is the //Hello World// program for Arduino that outputs the text on the Serial Monitor each second:
void setup() {
Serial.begin(9600); //establishes the connection with the serial port
}
void loop() {
Serial.println("Hello World"); //prints out the line with the text
delay(1000); //pause for 1 second
}
Serial Monitor can be found following the path: //Tools->Serial Monitor//.
In the code can be seen that the //setup()// function contains the following command:
Serial.begin(9600);
This statement opens the serial port at the initialization of the program so that the Serial Monitor can be used for outputting text or values on the screen.
For printing out text the following command is used:
Serial.println("Hello World");
**Check yourself**
1. How to attach any library to a sketch?
2. What command expressions are not usually separate by semicolon?
3. How to establish a serial communication between devices?
4. How does delay() command works?
*Stops LED blinking specified number of milliseconds
*Stops program execution for a specified number of seconds
*Stops program execution for a specified number of milliseconds