Pages: [1] 2   Go Down
Author Topic: Setup called inside Loop- what is going on?  (Read 1436 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
Hello
I'm after setup
Hello
I'm after setup
Hello
I'm after setup
...

I get

Code:
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
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  void setup ();
Is not how you call a function
Code:
setup();
is how you call a function.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26487
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't see a syntax error, I see a function prototype where a function prototype isn't needed.
Logged

"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.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 633
Posts: 50203
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26487
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

USA, FL
Offline Offline
God Member
*****
Karma: 14
Posts: 689
A life? Where can I download one of those?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The setup() only runs once when you turn on or reset the board.

Loop() runs over and over again non stop.

So:
Code:
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:
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()'
« Last Edit: September 22, 2012, 10:00:07 pm by codlink » Logged

//LiNK

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I try to compile this:

Code:
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?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 497
Posts: 19055
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This compiles.

Code:
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.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 497
Posts: 19055
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


USA, FL
Offline Offline
God Member
*****
Karma: 14
Posts: 689
A life? Where can I download one of those?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I try to compile this:

Code:
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
Logged

//LiNK

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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:
void setup ();
as a 'function prototype' instead of a function call which is typed as,
Code:
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.

 
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 497
Posts: 19055
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

 smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: August 20, 2014, 12:52:29 pm by mdimiou » Logged

Pages: [1] 2   Go Up
Jump to: