The setup( ) and loop( )
In Arduino, we call a program using the vocabulary, a Sketch.It contains some lines of programming codes and comments that are going to be executed by the Arduino board.
Following is an example of comments made in Arduino IDE.
/*
*LED at 13
*Let it glow first then do blinking
*/
We use /*….*/ to cover up comments so that when the execution whatever in between the /*….*/ won’t be considered. We usually put comments to enable the new reader/user of that code to understand how the codes are working and what is happening inside the codes. We add * in next line just to make the comments beautifully lines. For one line comments we use //. There are a lot of ways to write sketches.
But those ways must have at least two parts.
1. setup( )
2. loop( )
You can see in which order I’ve mentioned these two. The setup comes first and the loop follows. This is conventional. Usually we used to call them as setup block and loop block.
Then, what is a block?
Block is composed with a number of lines of codes that runs together. To avoid the multi usage of the vocabulary, also we used to call block as block of codes. There we have a problem. How to identify, which are sketches and which are blocks? Whatever defined by { } are blocks. A block begins with { and ends with }.
The basic form of a sketch looks like this.
void setup() {
//lines of codes
}
void loop(){
//lines of codes
}
We use void during the declaration of function. And void itself shows there won’t be any return value from the function.
The setup( ) block runs first when the entire sketch is uploaded to an Arduino board and it runs only once. So we do all the necessary arrangement parts of that particular sketch in void setup( ) block. When the setup( ) block reaches }, the sketch enters into loop( ). But in case of loop( ) it is upside down. Whenever the sketch reaches the } of loop( ) block it goes to the { of the loop( ) and runs again. This happens again and again until the Arduino is being powered.