Need Help ASAP

is it possible to call the function Void loop under void loop?

ex:

int outputPin = 13;

void setup()
{
pinMode(outputPin, OUTPUT);
}

void loop()
{
digitalWrite(outputPin, HIGH);
delay(3000);
void loop();
}

because we are trying to restart the void loop once the condition in our program is satisfied.

Thanks. =)

The name should give it away, it loops continuously anyway. However you can nest the calls to loop but you will need to have some condition to stop calling otherwise the stack will explode.

You can just use 'return' to exit the function where ever you want, it does not need to continue to the end of the function.

Your syntax is wrong (drop the "void"), but apart from that it's quite possible, fairly common and even has a name "recursion".

That said recursion is not for the faint of heart and I fail to see why you would need it if you are asking this question.

For example the code you have will blow the stack in a mS or so and crash the program.

we are trying to restart the void loop once the condition in our program is satisfied.

Then something like this

void loop()
{
  digitalWrite(outputPin, HIGH);
  delay(3000);
  wait_for_condition();
}

Rob

Graynomad:
Your syntax is wrong (drop the "void"), but apart from that it's quite possible, fairly common and even has a name "recursion".

That said recursion is not for the faint of heart and I fail to see why you would need it if you are asking this question.

For example the code you have will blow the stack in a mS or so and crash the program.


Rob

pYro_65:
The name should give it away, it loops continuously anyway. However you can nest the calls to loop but you will need to have some condition to stop calling otherwise the stack will explode.

You can just use 'return' to exit the function where ever you want, it does not need to continue to the end of the function.


excuse me sir. what do you mean by " (drop the "void")" in the sample program?
we're trying to consider void loop as a function.is it possible?

Thanks a lot for the response :smiley:

Our posts crossed

what do you mean by " (drop the "void")"

void loop()
{
  digitalWrite(outputPin, HIGH);
  delay(3000);
  loop();
}

However note my previous post, this will NOT work.


Rob

can we recall the "void loop" inside the loop while performing the void loop itself?

or in short restarting the loop itself without changing the values?

Thanks a lot. :smiley:

Graynomad:
Our posts crossed

what do you mean by " (drop the "void")"

void loop()

{
 digitalWrite(outputPin, HIGH);
 delay(3000);
 loop();
}




However note my previous post, this will NOT work.
______
Rob

what if we call the void loop under a function? example..

Void Reiland()
{Void loop();
}

Thanks! We really appreciate all the responses. :slight_smile:

edit: this is in response to:

Ponens:
can we recall the "void loop" inside the loop while performing the void loop itself?

or in short restarting the loop itself without changing the values?

Thanks a lot. :smiley:

"void" simply means that the function "loop()" has no return type.

this is for initializing the function:

void function() {
//some code goes here
}

when calling the function, you simply do this:

function();

The answers to your last two questions are already given by graynomad in his first response:

  • Yes, you can call a function from within a function, however this will lead to recursion and is most likely not what you intend.
  • The loop function restarts automatically without changing any values.

you should probably do some googling. hope this helps

Regards

p.

As soon as you finish the void loop() it starts the void loop() again.

You can terminate the loop early with the instruction "return", in which case it will immediately start from the beginning of void loop() again:

unsigned int i = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  i++;

  if(i>9)
    return;

  Serial.println(i);
}

That will print the numbers 0 to 9, then it will pause slightly while "i" counts up to 65535 (the limit of an unsigned int), then it will print 0 to 9 again, etc.

majenko:
As soon as you finish the void loop() it starts the void loop() again.

You can terminate the loop early with the instruction "return", in which case it will immediately start from the beginning of void loop() again:

unsigned int i = 0;

void setup()
{
 Serial.begin(9600);
}

void loop()
{
 i++;

if(i>9)
   return;

Serial.println(i);
}




That will print the numbers 0 to 9, then it will pause slightly while "i" counts up to 65535 (the limit of an unsigned int), then it will print 0 to 9 again, etc.

Thanks! We'll try this if it works :slight_smile:

And, what if we use this:

what if we call the void loop under a function? example..

Code:

void loop()
{
.............

void Reiland();
}

void Reiland()
{
void loop();
}

Note: Void loop is the same as the main loop. We are also planning to try this to as our Plan B.

Thanks! :smiley:

what if we call the void loop under a function? example..

No.

This is roughly what your program looks like when compiled

main ()
{
  init ();
  setup ();
  while (1) {
    loop ();
  }
}

If you put your function in "loop", then you're still calling "loop " recursively.
It's a really bad idea, don't do it.

Why don't you tell us what you want to do, not how you think you should do it?

did you even read my post?

did you read graynomads post?

did you google recursion?

did you google function declaration? (I did it for you: http://arduino.cc/en/Reference/FunctionDeclaration)

and, finally, you can use the # symbol on the menu above, to make your posts easier to read.

When calling a function you do not include the function return type. If you include the function return type (the void) then it becomes a "forward declaration" of the function and not the calling of the function.

Yes, you can call "loop()" from anywhere - it is just a function like any other.

BUT

It is a really really really stupid thing to do.

Before you even consider the possibility of even thinking about the idea of this, you have to have a really good understanding of what recursion is.

Whenever a function calls itself, or calls a second function which calls the first function, or any combination of functions which result in any one of the functions being called by a child function of any of the other functions, that is recursion. If you don't have a way of stopping the recursion, then the recursion will continue for ever, and the microcontroller will have a hernia.

Take the following example - I have a number "n" that I want to keep dividing by 2 until it becomes less than 2. Now, I could use a loop:

float n = 83.134;

while(n>=2.0)
{
  n = n / 2.0;
}

which is all well and good, or I could be fancy and use recursion:

float n = 83.134;

void divide()
{
  n = n / 2.0;
  if(n > 2.0)
    divide();
}

divide();

They both give the same result, but work differently.

Note that in the second example the divide() function is only called from within the divide() function IF certain criteria are matched. This allows the recursive calls to come to an end when the criteria are reached and return from all the parent divide() function calls.

Every time you call a function two bytes are pushed on to the stack (temporary memory storage), and whenever a function is finished it removes those two bytes from the stack.

If you call a function which then itself calls a function you end up with 4 bytes on the stack - two from the function you called, and two from the function your function called.

So, if you use recursion and you exceed a certain "depth", then you can see that the stack is going to rapidly fill up. If you call a function that calls itself, then for each time it calls itself you are going to get an additional 2 bytes on the stack. 1,000 iterations and you have used up all the memory of the chip (for a 328P) and the program will crash.

Recursion on a microcontroller, therefore, should be avoided at all cost - it's a very expensive operation in terms of memory, and quite easy to get wrong.

Sometimes, though, it can make programming of certain mathematical operations somewhat simpler to understand and implement.

One other good way to think about recursion is if you were to replace the calls to functions with the contents of the functions themselves:

void loop()
{
  a = a + 1;
  loop();
}

Replacing loop in this example would give you:

void loop()
{
  a = a + 1;
  {
    a = a + 1;
    loop();
  }
}

But that has given us another loop. So, let's replace that one too:

void loop()
{
  a = a + 1;
  {
    a = a + 1;
   {
      a = a + 1;
      loop();
    }
  }
}

Oh look - another loop. Let's replace it then.

Or... maybe not.

You see where I'm going with this? You would literally be there forever and a day expanding the loop() calls, and the code would reach an infinite size.

unsigned long factorial (unsigned long n)
{
  return n ? n * factorial (n - 1) : 1; 
}

Good only up to 12!, not because of stack usage, but because of the size of 13!

"To iterate is human, to recurse, Divine"

AWOL:

unsigned long factorial (unsigned long n)

{
  return n ? n * factorial (n - 1) : 1;
}



Good only up to 12!, not because of stack usage, but because of the size of 13!

"To iterate is human, to recurse, Divine"

Maybe you should use 64-bit long longs... :wink:

"To understand recursion you must first understand recursion."

Maybe you should use 64-bit long longs... smiley-wink

I do, but you can't easily print 'em :wink: