Arduino Programing Overview
The Arduino program is divided into three main parts that are structure, values, and functions.
When writing a code, the important thing is following the syntax of the language being used because in order to run the code successfully the correct syntax is necessary. So, when writing a program for Arduino following syntax should be followed:
- To complete the statement a semicolon “;” is used at the end of the statement.
- To enclose the block parenthesis “{}” are used. Block in a program contains some statements, declaration of the variables, functions, or loops.
- Comments can be written for each statement in the code to better understand the statement functionality. It can be done by using double forward slash “//” at the start of the comment if there is only a single line comment. However, if there are multi line comments in a row, a forward slash asterisk “/*” at the start and asterisk forward slash “*/” at the end of the comment. Comments can also be used to exclude any statement.
The figure below given gives a much better understanding of the syntax used for coding in Arduino software:
After understanding the syntax, let’s move towards how to use variables in the Arduino program and what type of variables are used in program structure. To store any values which will be used in the program can be a number or an alphabet.
Using the variables gives the option of saving, changing, updating and accessing the information when the program is running. There are different types of variables that can be used including char, int, double, string, float, unsigned int, long and unsigned long.
The following are operators used in the programing of Arduino:
- For assigning any value to a variable or a character equal to “=” sign is used
- There are different mathematical operators like percentage, multiply, subtraction, addition can be used (%, +, *, -, /)
- For comparison of the different values the operators like less than equal to, greater than equal to, equal to, less than, greater than are used (==, ,=)
- Logical operators are used to define the conditional statements such as AND (&&), NOT(!) and OR (||) operators
Arduino Program structure
The Arduino program structure is divided into two functions: the set-up function and the loop function.
The setup() function contains initialization of the libraries, variables used for the code. Similarly, pin modes of the Arduino are also declared in this function. It also initializes the communication between the Arduino board and the computer. It only runs once.
The loop() function keeps on repeating the instructions and actively controls and monitors the Arduino.
Example
To understand the program structure of Arduino an example code is compiled. The code is about blinking of the LED light with a delay of 1000 milliseconds.
First in the setup function the pin mode is initialized, pin 8 has been set as OUTPUT. Coming to the loop function, the state (HIGH/LOW) of the LED changes after the delay of 1000 milliseconds. Similarly, we can say that the implementation of the setup function is carried out in a loop function. The Arduino code for blinking of LED is given as:
pinMode(8, OUTPUT);
}
void loop() {
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
delay(1000);
}
Conclusion
To write a program in any language its basic prerequisites must be known which involve syntax, declaring and initialization of variables and incorporating different operators. This article briefly explains the syntax, using variables and different operators to give a better understanding of program structure.