setup() & loop()

This question is to just satisfy my curiosity.

Is there anything that happens out side of our sketch code inside the loop() that doesn't happen inside setup()?

Potentially could you write your own looping() code inside setup() and everything would be fine?

Would interrupts etc still work?

I'm just wondering if the base code generated by the IDE does anything more than

int main(int argc, char * argv [] ) {

     // anything going on here?

    setup();

     // anything going on here?
    while(1){
        loop();
     // anything going on here?
    }

}

Thanks?

Isn't there a "doEvents" or some such missing there?

Arduino is open-source. You can just check the source code:

Note that different cores have different structures, and different initialization requirements.
For example, the ESP32 Arduino Core runs on top of FreeRTOS:

Pieter

I'm just wondering if the base code generated by the IDE does anything more than

There is a little bit more going on in the hidden main() function. This is the main() function used by the Uno and similar boards

/*
  main.cpp - Main loop for Arduino sketches
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <Arduino.h>

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }

// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }

void setupUSB() __attribute__((weak));
void setupUSB() { }

int main(void)
{
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

In particular, note the call to init()

meltDown:
Isn't there a "doEvents" or some such missing there?

I don't know!

"doEvents" reminds me of Visual Basic 25 years ago :slight_smile: where 'young' programmers didn't know how to use threads, so a 40Mb FTP download was processed in a tight loop in the Windows Event loop meaning the clicks on the Cancel button didn't get processed until the download was completed.

I've been looking at some Arduino task/scheduling code and I've seen some instances where everything is programmed in setup() which leave the loop() function completely empty.

I just wondered if anything had been overlooked in this approach and perhaps this wasn't best practice.

UKHeliBob:
There is a little bit more going on in the hidden main() function. This is the main() function used by the Uno and similar boards

8< snipped 

int main(void)
{
init();

initVariant();

#if defined(USBCON)
USBDevice.attach();
#endif

setup();
   
for (;:wink: {
loop();
if (serialEventRun) serialEventRun();
}
       
return 0;
}

I see that, but it is all before setup().

 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }

The serialEventRun stuff was the kind of thing I was wondering about.

Any reasons for the "for(;;){...}" vs "while(1){...}"?

Thanks.

#define ever (;;)
...
....
for ever
{
}

The serialEventRun stuff was the kind of thing I was wondering about.

Whilst it looks useful you might just as well put

if (Serial.available())
  {
    //do something with the available data
  }

in loop()

On several occasions I have seem posters here assume that the call to SerialEvent() was interrupt driven, but it isn't. All that happens is that at the end of loop() the function is called (if it exists) if serial data is available.