Unlike the void setup which execute only once, the void loop execute itself infinitely. The void setup contains the initialization of the components such as an input or output of the arduino card and the initialization of the serial monitor while the void loop is used to control your component already initialized.
Because this loop will execute infinitely, you can control your components without re-lauch the program everytime.
void loop() {
// Your code
}
The function void loop is mandatory in all the Arduino programs, even if there isn’t anything inside. Forget to write will create an error during the verifying of the program.
The void loop contains all the functions to read the values of the sensors and display it on the serial monitor. You can also control many components, such as push button, stepper motor…
Here is a program which contains a void loop:
int pin_SWITCH=2;
int photoresistor=A0;
int LED =7;
void setup(){
pinMode(pin_SWITCH,OUTPUT);
pinMode(LED,OUTPUT);
}
void loop () {
boolean state_button = digitalRead(pin_SWITCH) ; // We retrieve the value of the state of the button
analogRead(photoresistor); // We retrieve the value from the photoresistor
digitalWrite(LED,HIGH); // turn on the led
delay(30); // Wait after retrieving another value
}
We can detail the above functions: