Offline
Newbie
Karma: 0
Posts: 2
|
 |
« on: September 22, 2012, 02:12:26 pm » |
I tried compiling the following code with Arduino 1.0.1 into a Duemilanove (328) using AVRISP mkII; 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: Hello I'm after setup Hello I'm after setup Hello I'm after setup ...
I get 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
Edison Member
Karma: 15
Posts: 1006
Arduino rocks
|
 |
« Reply #1 on: September 22, 2012, 02:28:38 pm » |
If you remove the blatent syntax error (which somehow compiles...) it just gets confused by the multiple Serial.begin()s void setup ();
Is not how you call a function setup();
is how you call a function.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #2 on: September 22, 2012, 03:23:29 pm » |
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.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #3 on: September 22, 2012, 05:54:33 pm » |
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
Online
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: September 22, 2012, 08:24:20 pm » |
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.
|
|
|
|
USA, FL
Offline
God Member
Karma: 11
Posts: 584
A life? Where can I download one of those?
|
 |
« Reply #5 on: September 22, 2012, 09:56:15 pm » |
The setup() only runs once when you turn on or reset the board. Loop() runs over and over again non stop. So: 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: setup () { Serial.begin(9600); Serial.println("Hello"); }
void loop () { void setup (); Serial.println("I'm after setup"); } I get this error: 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
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #6 on: September 23, 2012, 12:04:36 am » |
Why would you want to call setup() inside loop() anyway ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 15
Posts: 1006
Arduino rocks
|
 |
« Reply #7 on: September 23, 2012, 12:06:44 am » |
If I try to compile this: setup () { Serial.begin(9600); Serial.println("Hello"); }
void loop () { void setup (); Serial.println("I'm after setup"); } I get this error: 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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #8 on: September 23, 2012, 12:21:16 am » |
This compiles. 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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #9 on: September 23, 2012, 12:23:07 am » |
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
God Member
Karma: 11
Posts: 584
A life? Where can I download one of those?
|
 |
« Reply #10 on: September 23, 2012, 09:43:39 am » |
If I try to compile this: setup () { Serial.begin(9600); Serial.println("Hello"); }
void loop () { void setup (); Serial.println("I'm after setup"); } I get this error: 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
Newbie
Karma: 0
Posts: 2
|
 |
« Reply #11 on: September 24, 2012, 12:08:38 am » |
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 void setup (); as a 'function prototype' instead of a function call which is typed as, 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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #12 on: September 24, 2012, 12:20:27 am » |
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
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #13 on: September 24, 2012, 01:30:22 am » |
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 
|
|
|
|
|
Logged
|
|
|
|
|
|