Arduino Program Structure
After understanding the Arduino, it's types and installation
of IDE in the previous Articles. Now lets get started with the programming part
of Arduino.
As all of you know that the Arduino programs are written in
"Integrated Development Environment (IDE)". Programs written in IDE
are also called as "sketches".
These sketches are written in the text editor and are saved
with the file extension .ino. The editor has features for cutting/pasting and
for searching/replacing text. The message area gives feedback while saving and
exporting and also displays errors.
The console displays text output by the Arduino Software
(IDE), including complete error messages and other information. The bottom
right hand corner of the window displays the configured board and serial port.
The toolbar buttons allow you to verify and upload programs, create, open, and
save sketches, and open the serial monitor.
Additional commands are found within the five menus: File,
Edit, Sketch, Tools, Help. The menus are context sensitive, which means only
those items relevant to the work currently being carried out are available.
-File
-Edit
-Sketch
-Tools
-Help
Sketchbook
The Arduino Software (IDE) uses the concept of a sketchbook:
a standard place to store your programs (or sketches). The sketches in your
sketchbook can be opened from the File > Sketchbook menu or from the Open
button on the toolbar. The first time you run the Arduino software, it will
automatically create a directory for your sketchbook. You can view or change
the location of the sketchbook location from with the Preferences dialog.
Beginning with version 1.0, files are saved with a .ino file
extension. Previous versions use the .pde extension. You may still open .pde
named files in version 1.0 and later, the software will automatically rename
the extension to. ino.
Note: It's important to use the latest version of Arduino IDE.
Arduino programs can be divided in three main parts: Structure, Functions , and Values (variables and constants).
Structure
The structure of Arduino program is pretty simple. Arduino programs have a minimum of 2 blocks,
- setup () function.
- Loop () function.
Here, setup () is the preparation block and loop () is an
execution block.
Setup () function:
The setup () function is called when a sketch starts. Use it
to initialize the variables, pin modes, start using libraries, etc. The setup
function will only run once, after each power up or reset of the Arduino board.
The setup function is the first to execute when the program
is executed, and this function is called only once. The setup function is used
to initialize the pin modes and start serial communication. This function has
to be included even if there are no statements to execute.
Loop Function:
After creating a setup () function, which initializes and
sets the initial values, the loop () function does precisely what its name
suggests, and loops consecutively, allowing your program to change and respond.
Use it to actively control the Arduino board.
After the setup () function is executed, the execution block
runs next. The execution block hosts statements like reading inputs, triggering
outputs, checking conditions etc.
Ex:
void setup ()
{
pinMode (pin-number, OUTPUT); // set the ‘pin-number’ as
output
pinMode (pin-number, INPUT); // set the ‘pin-number’ as
output
}
Void loop ()
{
digitalWrite (pin-number, HIGH); // turns ON the component
connected to ‘pin-number’
delay (1000); // wait for 1 sec
digitalWrite (pin-number, LOW); // turns OFF the component
connected to ‘pin-number’
delay (1000); //wait for 1sec
}
Note: Arduino always measures the time duration in
millisecond. Therefore, whenever you mention the delay, keep it in milliseconds.
Writing the code for Arduino is the same as writing it in C
or C++. The concepts like data types, variables, constants, Operators, Loops,
Strings, String Objects, Array is the same as C/C++.
IMP: If you didn’t understand what is pinMode and
digitalWrite. Don’t worry! I am going to explain it in my next article.
0 Comments