6.2 How to declare void setup() and void loop() ?

void setup()

Before the setup function the declaration of any variable is done at the beginning of the program then the void setup() is followed.This is a declaration for a function called “setup” and this is the first function to run in the program. This exact line is required in every Arduino sketch. It will only run once when the Arduino is powered or tends to reset. This function is used to status of the pin so we use the pinMode to change the status of the pin and initialize serial communication.

pinMode is a command used to declare whether the Arduino’s I/O pins will be used as Input or Output.As you are already aware, Arduino Uno have 14 digital I/O pins,so you can use any of these pins as Input or Output but first you have to declare how you intend to use it.So, the pin will be used as Output pin when you need to give signal or current to the output devices like LED,buzzer and it will be used as Input pin when you need to read the signal or value from the Input devices like PIR sensor,Ultrasonic sensor.

Syntax for declaring pin as Input:

pinMode(5,INPUT); // pin5 of Arduino Uno is declared as input pin.

Syntax for declaring pin as Output:

pinMode(6,OUTPUT);  // pin6 of Arduino Uno is declared as output pin.

void loop()

This function runs after the setup() function.This is where the bulk of your Arduino sketch is executed.The program starts directly after the opening curly bracket ({), runs until it sees the closing curly bracket (}), and jumps back up to the first line in loop() and starts all over.The loop() function will run over-and-over-and-over until the Arduino is reset.In this function you can trigger outputs or you can read the input from the devices and it allows program to change,react and control the Arduino board.

Syntax:

void loop()

{

digitalWrite(pin,HIGH);

delay(1000);

digitalWrite(pin,LOW);

delay(1000);

}

digitalRead and digitalWrite commands are used for reading or writing on any digital pin of the Arduino Uno board.

HIGH and LOW 

These are the constants which is used to define the pin level as HIGH or LOW and it are used when reading or writing to digital pins.HIGH is defined as a logic level 1 or ON or +5V and LOW is defined as a logic level 0 or OFF or 0V.

delay (ms)

delay is used to pause the program for the amount of time and it is specified in milliseconds where 1000 milliseconds is equal to 1 second.