Pages: [1]   Go Down
Author Topic: Setup and Loop??  (Read 5900 times)
0 Members and 1 Guest are viewing this topic.
Florida
Offline Offline
Newbie
*
Karma: 0
Posts: 32
It is what it is...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do I have to use setup and loop?

Could I leave out the loop function and do this?

void setup() {
  Serial.begin(9600);
  Serial.println("Bla bla bla");
  int a = 0;

  while (1) {
    Serial.println(a);
    a++;
  }
}

If not, could I just insert an empty loop function after this?
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4255
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do I have to use setup and loop?
Yes

Quote
Could I leave out the loop function and do this?
No

Quote
If not, could I just insert an empty loop function after this?
Yes

I believe that loop() accomplishes essentially the same thing as:   while (1) { ... }

Don
« Last Edit: January 19, 2011, 09:51:02 pm by floresta » Logged

Florida
Offline Offline
Newbie
*
Karma: 0
Posts: 32
It is what it is...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!
Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You do have to supply both functions. You could do everything in the setup and leave loop empty, and for some apps that would be all that is needed. For example if your app was just a timer that was to start when the reset button was pressed for example.

However, these functions are there as a reminder of how most embedded software is structured.

You set some things up once after reset, and then you have a 'tight' loop where you do things quickly like monitor for a key being pressed, or refresh an LCD display.

So the things in the main 'loop' will be run repeatedly as frequently as possible.

Si.
Logged

--
My New Arduino Book: http://www.arduinobook.com

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the file main.cpp the setup() and loop() are called this way:

Code:
#include <WProgram.h>

int main(void)
{
      init();

      setup();
    
      for (;;)
            loop();
        
      return 0;
}

main.cpp can be found at : ~arduino-0021\hardware\arduino\cores\arduino\main.cpp

there is interesting code in the dungeons smiley
« Last Edit: January 20, 2011, 12:01:31 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 1
Posts: 38
Turn it off!!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And actually, since the main() function is part of a library, you can overwrite in your code.  Just don't forget to call init() or delays and other built in functions wont work.
Logged

Pages: [1]   Go Up
Jump to: