Quick question...can I call my setup() function?

I can obviously call functions but is there a way to call my setup function? Reason: supose in my setup I've populated peramiters which will be used in my loop. If I wish to change them (and there are many of them) without restarting my system, is there a way that I can go back to setup? This is what I've tried and it did not work.

void loop()
{
 if (Serial.available() > 0)
  {
   int incomingByte = Serial.read();
   delay(2);
   if (incomingByte == 86) //shift V should satisfy the if statement and call the function
     {
      setup();
     }
   } 
//other stuff in the loop
}

Any help would be great. I can live without this but it sure would make things easier if I could figure it out. :~
Thanks

I suppose it "should work" but it's kind of yucky.

Why not put the initialization you need to reuse into its own function and call it both from setup() and wherever else you need it?

-br

Just create a separate function that sets things the way you want them and call it from setup() or loop() as required.

i.e.:

setup()
{
   //setupstuff
   domyinit();
  //
}

loop()
{
    // do stuff
    domyinit();
    // do more stuff
}

void domyinit(void)
{
    //do my init stuff
}

Thanks guys. I should have thought of that. Tunnel vision, I guess. :slight_smile:

calling setup() is completely legal, it is just another function. However be sure it only has the effects you want

You can also call loop(), even recursively if you want, why not

int x = 0;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  x++;
  Serial.println(x);   
  if (x < (analogRead(A0)/10) )   // recursion always need some end condition,
  {
    loop();
  }
  Serial.println(x); 
  x--;
  delay(10); // 
}

what does it do?


edit: updated code so it won't "fly through the stack"

You can also call loop(), even recursively if you want, why not

Because recursive functions are supposed to have end end, so you don't run out of stack space. Your example may run 1023 levels deep. I doubt that there is sufficient stack space for that.

I guess one could make the case that needing to call the setup() function in your main loop() function means you are doing stuff in the setup function that should rather be done is a user written function that can be called anywhere in the sketch required. Kind of by definition stuff in the setup function should be stuff that only requires one time initialization or action. Having to call setup() within loop() just sounds like unorganized logic to me, but then maybe I'm just use to reading too many 'normal' sketches. :wink:

Lefty

Your example may run 1023 levels deep. I doubt that there is sufficient stack space for that.

You're right, however it depends heavily on the what is connected to A0 :wink:

I have updated the example with a division by 10.

Im-just-Rusty:
I can obviously call functions but is there a way to call my setup function?

It's a function. You can call it.

is there a way to call my setup function

setup ();