The Syntax and the Structure of the Program

Syntax

Arduino IDE is a software that allows writing Arduino code. Each file with Arduino code is called a sketch. The Arduino programming language is similar to the C++ language. 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 giving a name to a constant value at the very beginning of the program.

#define constant 4

In this example, the value four 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 <Servo.h>

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 the only single line should be commented on.

/*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 the 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 a 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 an example, how a new empty sketch looks like.

title
Figure 1: The empty sketch in the Arduino IDE.

Each Arduino sketch contains multiple parts.

  1. Global definition section – the section where variables and constants are defined that the working range is in the whole sketch that means both in the initialisation and the loop sections. This section is at the very beginning of the sketch, before the setup function.
  2. Initialisation section – is executed only once before running 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 keyword 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 initialisation section from the rest of the code.

  3. Loop section – the part that is executed continuously, it reads inputs, processes data and gives output signals. After running 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.

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.

 title
Figure 2: The path to open the Blink LED example program.

When the Blink LED example program is opened, the following sketch should open in the programming environment:

 title
Figure 3: The Blink LED example program.

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 the program, the following things can be seen.

  1. 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 (+5 V) and 0 (0 V). Sending the output signal to the built-in LED: the LED is periodically turned on and off.
  2. 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 the 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.
  3. 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 initialisation 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 are expressions not usually separated by the semicolon?

3. How to establish 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.
en/iot-open/getting_familiar_with_your_hardware_rtu_itmo_sut/arduino_and_arduino_101_intel_curie/programming_fundamentals_rtu/building_your_first_project.txt · Last modified: 2020/07/27 09:00 by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0