What is a void loop? What instruction can I write in it?

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
}

Don't forget the void loop!

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.

What instructions can we write in the function void loop?

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:

  • digitalRead(pin_SWITCH) : To read the value from the digital pin
  • analogRead(LED,HIGH): To read the value from the LED on the Analog pin.
  • digitalWrite(LED, HIGH): To control your component on the digital pin.
  • delay(30): To make a delay of 30 milli-second in the program