This is an old revision of the document!


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 <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 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.

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 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.
  2. 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.

  1. 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.

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

Hello World

en/iot-open/programming_fundamentals_rtu/building_your_first_project.1515765579.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
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