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” 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” 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.
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.
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 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.
Below is given the example, how a new empty sketch looks like.
Each Arduino sketch contains multiple parts:
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.
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.
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:
“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