Coding Screen
If you open the coding screen of your IDE, you will realize that it is divided into two sections namely, setup() and loop(). The setup segment is the first block and is implemented first for preparing the necessary environment needed for running other commands. This coding screen is shown below:
It is important to note that the setup and loop blocks must have statements that are enclosed within curly brackets. Depending on the type of project you are working on, you can initialize the setup in setup() and define other necessary statements in the loop() block. Let us study each section individually
For example
void setup ( ) {
Coding statement 1;
Coding statement 2;
Coding statement n;
}
void loop ( ) {
Coding statement 1;
Coding statement 2;
Coding statement n;
}
Setup
Setup contains the very beginning section of the code that must be executed first. The pin modes, libraries, variables, etc., are included in the setup section so that no problem occurs when the remaining code runs. It is executed only once during the uploading of the program and after resetting or powering up the Arduino board.
Zero setup () resides at the top of each sketch. When the program runs after completion, it heads towards the setup section to initialize the setup and include all the necessary libraries all at once.
Loop
The loop contains statements that are executed repeatedly. Unlike, the setup section there is no restriction on running this code once, it can run multiple times according to the value of variables.
Time
The basic unit of measuring time in Arduino programming is a millisecond.
1 sec = 1000 milliseconds
Timing adjustments can be made in milliseconds. A better explanation for this can be that a 2-second delay corresponds to 2000 milliseconds.
Example
A simple example of blinking the LED using Arduino is considered.
The steps are:
- Go to the menu bar. Click on the File button in the bar.
- Click on the Examples in the menu bar.
- Click on the Basics option.
- You will see Blink, click it.
This is the window that opens:
Note: The pinMode will be the main function in the void setup() and digitalWrite( ) and delay ( ) will be the main function in the void setup()
Arduino Coding Basics
In some previous articles, we have seen and understood the concepts of Arduino Boards, Arduino IDEs, and the installation procedure for Arduino software. Arduino IDE (Integrated Development Environment) is an essential which makes the task of uploading code on Arduino boards, an easy task. Instead of writing them at the assembly level, the IDEs make it convenient and the codes are written in high-level languages like C and C++. Let us get introduced to the basics of Arduino Coding.
Make sure that you have an appropriate IDE for coding before starting your journey to Arduino coding. Let us study every character, command, and function that is used in Arduino. Keep your Arduino IDE switched on so that you can apply the concepts you learn from the article.
Table of Content
- What is Arduino?
- Electronic Signals
- Brackets
- Line Comment
- Coding Screen
- Time
- pinMode ( )
- digitalWrite( )
- delay ( )
- Solved Example
- Advantages and Disadvantages of Arduino
- Application of Arduino