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