The "voids" that you refer to are functions in the same way that pinMode() and digitalWrite() are functions. The difference is that they have been written by the programmer as part of the program as opposed to being part of the Arduino software itself. The word void indicates that the function does not return a value but they can be written to accept value(s) and return the result of calculations in which case the function name is preceded by the type of value that it returns.
Functions are used either to execute the same code at several points in the program and/or to separate sections of the code to make writing, understanding and debugging the code easier.
Do some research on C functions but in the meantime here is and example to experiment with.
void setup()
{
Serial.begin(115200);
}
void loop()
{
for (int x = 0; x < 10; x++) //the value oof x will go from 0 to 9
{
Serial.print("Two times ");
Serial.print(x);
Serial.print(" is ");
Serial.println(timesTwo(x)); //call the function and print the value that it returns
delay(1000);
}
}
int timesTwo(int aNumber) //this function will double the number that it is passed and return the result
{
return aNumber + aNumber;
}
Whandall:
All of them are only functions, no loops.
The void loop() function is called by the main program that the IDE supplies, over and over.
If you look at main.cpp, which holds the 'real' program running on an Arduino, you will see the mechanism.
(and some other special functions)
int main(void)
{
init();
initVariant();
#if defined(USBCON)
USBDevice.attach(); #endif
setup();
for (; {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
\
And when you combine the above ( Excellent! ) and others explanations given here with this gem (attached) from " examples bare minimum " - with "run once / run repeatedly" explanation(!) you should get the 'rest of the story".
I always though that function names should be descriptive , but the looping is NOT the work of "loop" function -the for( ; ; ) IS!
Yes, standard Arduino team excuse applies here (again) - it would be too confusing for beginner to know the actual "main" function inner works.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly: