Pages: [1] 2   Go Down
Author Topic: How to only require setup()  (Read 1414 times)
0 Members and 1 Guest are viewing this topic.
Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I just discovered something I think is wonderful. smiley

[edit]See: Bens post with correct code.

I've posted wrong code below.  :-[[/edit]

Change %arduino%\hardware\cores\arduino\main.cxx to:
Code:
int main(void)
{
  init();
    
  setup();

  for (;;){
    #ifdef loop
      loop();
    #endif
  }
  return 0;
}

Now I can write:
Quote

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

This is probably an old hack. But still useful smiley

Why not implement it in the official release? Are there any usecases that this may cause errors?

I could only think of this:
Quote

boolean setup = false;
void setup(){
  Serial.begin(9600);
  Serial.println("Hello World!");
}
 
But it causes this: (Which actually is descriptive smiley )
Quote
error: 'boolean setup' redeclared as different kind of symbol
« Last Edit: April 29, 2009, 05:46:14 pm by AlphaBeta » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12291
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This may raise some interesting possibilities.  

For example...

Code:
int main(void)
{
  init();
    
  setup();

  for (;;){
    #ifdef loop
      loop();
    #else
      // Put sleep code here
    #endif
  }
  return 0;
}

The Arduino would run the code in setup and go to sleep.  Perfect for an application that needs to run once and can be triggered by a reset.

The same technique works with setup.

- Brian
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So, in that first piece of code, where does one #define loop?  I'm not sure I understand how this is supposed to work.
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Brian, why not simply put code for an application that needs to run once in setup. If execution needs to block then something like:
   while(true)
      ;
« Last Edit: April 29, 2009, 06:33:28 am by mem » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So, in that first piece of code, where does one #define loop?  I'm not sure I understand how this is supposed to work.

One does not define the void loop(), that's the beauty of it smiley

The piece of code enables one to write sketches that does not need the loop.

Such as my imaginary Hello World. (Which compiles)
Quote

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



Without the #ifdef one will get an error message if the sketch has no loop.
« Last Edit: April 29, 2009, 10:00:25 am by AlphaBeta » Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, still don't understand!  How does defining or not defining a 'loop()' function affect the '#ifdef' which is looking for a '#define loop'?
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When we write:
Code:
void setup(){
}

void loop(){
}

Both setup and loop will be defined for the preprocessor, as in: 'It knows of setup and loop'

My code ask the preprocessor:
Quote
Do you know about loop?
If you do, execute it. But if you do no know about loop, do not try to execute it.

This way, we can omit the void loop() altogether in the Arduino IDE, and the compiler won't complain.

(Without the #ifdef the compiler would expect a function called loop to be defined and available.)
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Both setup and loop will be defined for the preprocessor,

Is that something that the Arduino IDE does, behind the scenes?
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Quote
Both setup and loop will be defined for the preprocessor,

Is that something that the Arduino IDE does, behind the scenes?

Kind of.

The arduino IDE makes function prototypes, and declares the functions that are to be used/called.
By doing this it actually makes a variable that points to a function code block. Which we define in out sketch.

Quote
And, becuase the #ifdef / #ifndef preprocessor directives works on variable names as well as the standard #define, it works on function names.
I might have to take this back.  :-[
[edit]Hmmm. Wait a minute.

Can't get it to work anymore  >smiley-sad[/edit]
« Last Edit: April 29, 2009, 01:04:05 pm by AlphaBeta » Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am wondering why it matters.  What do you want to do that you can't do in setup as it is ?

Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why it matters?

Well. Simplicity matters.

A lot of concepts could be thought without the loop, so I think it should.

Sets the focus in the subject at hand, without the 'distraction' of an empty loop.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 100
Frell, Frack, Smeg
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm still confused, why not just

void loop(){;}
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm still confused, why not just

void loop(){;}


Because then someone could ask: "Why do you create a function that does nothing?"
Or: "What does the void loop(){;} do?"


This is a detail, and writing an empty function definition woks. I just think it's not conform with the arduino simplicity.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 332
Posts: 16568
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, this sure looks a lot like a solution looking for a problem.  smiley-wink

Seems I've been shown that if you want setup to do nothing you can just leave it in but empty and if you want loop to not loop you just put a blocking while statement at the end of it. Is there something your method does in addition other then just confuse us hardware guys  smiley-wink

Lefty
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well. Right now this confuses me too.  smiley-sad


I just think that the opportunity to omit loop() would've been a useful one.
Logged

Pages: [1] 2   Go Up
Jump to: