Go Down

Topic: Setup called inside Loop- what is going on? (Read 1 time) previous topic - next topic

Bryan_Hatton

I tried compiling the following code with Arduino 1.0.1 into a Duemilanove (328) using AVRISP mkII;

Code: [Select]
void setup ()
{
  Serial.begin(9600);
  Serial.println("Hello");
}

void loop ()
{
  void setup ();
  Serial.println("I'm after setup");
}


Surprisingly the Arduino accepted it; however, it behaves oddly. Rather than receiving on the serial monitor something like:

Code: [Select]

Hello
I'm after setup
Hello
I'm after setup
Hello
I'm after setup
...


I get

Code: [Select]

Hello
I'm after setup
I'm after setup
I'm after setup
...


It's as if the Arduino is deliberately ignoring the call to setup. I've tried searching the forums and reference with little success, though I may have missed something. My question is four fold:

1) What exactly is the Arduino doing as it executes this code?
2) Why is it doing that?
3) Is the behavior of this code well defined or is it implementation dependent?
4) If the behavior is well defined, where is it specified?

Thank you for your time

WizenedEE

If you remove the blatent syntax error (which somehow compiles...) it just gets confused by the multiple Serial.begin()s

Code: [Select]

  void setup ();

Is not how you call a function
Code: [Select]

setup();

is how you call a function.

AWOL

I don't see a syntax error, I see a function prototype where a function prototype isn't needed.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
I don't see a syntax error, I see a function prototype where a function prototype isn't needed supposed to be.

I fixed that for you...

AWOL

I figure that if the compiler doesn't complain too hard, "isn't needed" is sufficient.
If it isn't "supposed" to be there, gcc would bitch about it, big time.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

codlink

#5
Sep 23, 2012, 04:56 am Last Edit: Sep 23, 2012, 05:00 am by codlink Reason: 1
The setup() only runs once when you turn on or reset the board.

Loop() runs over and over again non stop.

So:
Code: [Select]
Hello
I'm after setup
I'm after setup
I'm after setup


Is exactly what you told it to do.

If I try to compile this:

Code: [Select]
setup ()
{
  Serial.begin(9600);
  Serial.println("Hello");
}

void loop ()
{
  void setup ();
  Serial.println("I'm after setup");
}


I get this error:

Quote
sketch_sep22a:0: error: ISO C++ forbids declaration of 'setup' with no type
sketch_sep22a.cpp: In function 'int setup()':
sketch_sep22a:0: error: new declaration 'int setup()'
F:\Applications\Arduino\arduino-1.0.1\hardware\arduino\cores\arduino/Arduino.h:117: error: ambiguates old declaration 'void setup()'
//LiNK

tuxduino

Why would you want to call setup() inside loop() anyway ?

WizenedEE


If I try to compile this:

Code: [Select]
setup ()
{
  Serial.begin(9600);
  Serial.println("Hello");
}

void loop ()
{
  void setup ();
  Serial.println("I'm after setup");
}


I get this error:

Quote
sketch_sep22a:0: error: ISO C++ forbids declaration of 'setup' with no type
sketch_sep22a.cpp: In function 'int setup()':
sketch_sep22a:0: error: new declaration 'int setup()'
F:\Applications\Arduino\arduino-1.0.1\hardware\arduino\cores\arduino/Arduino.h:117: error: ambiguates old declaration 'void setup()'



Because you're missing a "void" at the beginning...

Is that windows 8?

Nick Gammon

This compiles.

Code: [Select]

void setup ()
{
  Serial.begin(9600);
  Serial.println("Hello");
}

void loop ()
{
  Serial.println("I'm after setup");
}


Compare carefully to what you had. You should not even be attempting to call setup from inside loop.

Nick Gammon

It doesn't really make sense to print "I'm after setup" - you will see that many times. The Arduino core first calls setup once, and then loop repeatedly. So loop is "after setup" but it is called multiple times.

codlink



If I try to compile this:

Code: [Select]
setup ()
{
  Serial.begin(9600);
  Serial.println("Hello");
}

void loop ()
{
  void setup ();
  Serial.println("I'm after setup");
}


I get this error:

Quote
sketch_sep22a:0: error: ISO C++ forbids declaration of 'setup' with no type
sketch_sep22a.cpp: In function 'int setup()':
sketch_sep22a:0: error: new declaration 'int setup()'
F:\Applications\Arduino\arduino-1.0.1\hardware\arduino\cores\arduino/Arduino.h:117: error: ambiguates old declaration 'void setup()'



Because you're missing a "void" at the beginning...

Is that windows 8?


lol, I didn't notice "void setup()" inside of "void loop()"  lol
//LiNK

Bryan_Hatton

I want to thank you guys for time you've taken to post here, it has been very helpful so far. There's been a small amount of confusion on a point which I'm sorry for failing to clarify. I'm not calling setup inside loop to actually accomplish anything, rather I'm trying to mess with the Arduino in order to understand it better. If I throw a wrench into an engine and explain why the engine broke the way it did, then I think I will be closer to understanding the engine itself.

With that in mind, I'd like to paraphrase what I've learned to make sure I've got things straight. The Arduino will blissfully call setup inside loop repeatedly just like any other function, though telling it to do so is not advisable. However, I get the behavior I see because Arduino reads
Code: [Select]
void setup (); as a 'function prototype' instead of a function call which is typed as,
Code: [Select]
setup();

Function prototypes act as variable declaration for functions. In effect loop says, "Hey Arduino, there's this function called setup which doesn't return anything, just so you know." Loop then prints a message to terminal and repeats.


Nick Gammon

Yes, sort of. However the function prototype doesn't "belong" to loop. It's confusing and misleading to have it there. Normally you put them near the start of the program.

tuxduino

Quote
I'm not calling setup inside loop to actually accomplish anything, rather I'm trying to mess with the Arduino in order to understand it better.


To better understand the Arduino internals I suggest you also take a look at the "system" source code, too. A good starting point is hardware/arduino/cores/arduino/main.cpp

:)

mdimiou

#14
Aug 20, 2014, 07:50 pm Last Edit: Aug 20, 2014, 07:52 pm by mdimiou Reason: 1
1st)If i call setup() inside loop() i will reset Arduino one more time with my decision?

2nd) When i am opening a new Serial Port fo a connection between my Arduino and my pc Arduino is going to reset???

For example..... i am opening and closing two times a Serial Port..... Arduino will reset two times?

Go Up