multiple voidloop()

hello, I have noticed reading through different coding that in some cases multiple loops are involved

the first one of course

void loop()

but later on there may be another like

void add_this()

and then another

void XXXXXX()

why is it used?

can someone explain how they function within the code. or point to a tutorial?

thank you!

Google "C++ functions"

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;
}

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:

}

[/code]

yes I agree, to confusing for a beginner to understand the inner workings.

thank you for ya'lls input, sooooo much to learn

Mr Bob, can you change your code above to be three times x? thanks, side by side comparisons are a big help to me.

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(timesThree(x));  //call the function and print the value that it returns
    delay(1000);
  }
}

int timesThree(int aNumber)  //this function will triple the number that it is passed and return the result
{
  return aNumber + aNumber + aNumber;
}

return aNumber + aNumber + aNumber; that’s what I missed.

thanks D

this would seem OK for (3) or (4) times a number, but “what if” they number was say 47 times X?

just curious.

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(timesThree(x));  //call the function and print the value that it returns
    delay(1000);
  }
}

int timesThree(int aNumber)  //this function will triple the number that it is passed and return the result
{
  return 47 * aNumber;
}

or even:

int multiplyTwoNumbers(int aNum1, int aNum2){
     return aNum1 * aNum2;

I think it is time to ask google. Go do a search for “C++ functions”. There are literally thousands of tutorials on the subject. There’s no need for us to waste time rewriting them here.

yea, I have a lot of reading to do. i'll look back on this one day and it will be funny. as for now, just a whole lotta confusing!

oh btw, my community college has an intro to c++ class, just waiting for them to post the sched.