What goes in setup, what comes before?

It is likely a simple question with an obvious answer, but can't find it in the tutorial and reference books I have read on C. If someone has a pointer to where I can find this basic bit of information, I would appreciate it! Roddy

Not surprising since “setup()” is not a C thing. I am not sure what you mean by “before.” The only thing that can be “before” setup() are global variables and constants, no code. Any variables declared and defined in setup() are only visible in setup() and cannot be referenced in loop() or any functions called from it.

So, the basic rules are:
Variables and constants you want visible to all functions go “before” setup().
Code, such as initializations, that should execute only once before anything else happens goes into setup().
Loop() contains code that will execute over and over since loop() is re-called continually each time it is exited.

Quite a lot of things happen before setup() is called.

Many people think that main() is the first bit of code called, but that is not correct.

The first bit of code is called "crt0" (C Run*Time stage **0*). This performs:

  1. Copying of pre-set variable data from flash to RAM
  2. Configure basic system settings
  3. Executing of global object constructors
  4. Execute main()

On the Arduino main() then does a number of functions, including setting up the timers for millis(), PWM, etc, and configuring such things as interrupts and ADC. It basically gets the system ready for you - all things you would normally be doing manually in a less abstracted system. (check out the init() function in wiring.c)

Then and only then does setup() get called.

Point 3 in the list above is the one that catches most people out. If you have a global object with code in the constructor that does things with peripherals that aren't configured until main() has run then you can have all sorts of strange things happening or just plain not working right. That is why most classes have a .begin() member function to do all the configuration instead of doing it in the constructor.

Declaration of functions could come before setup(), although traditionally they would go in a .h file. Definition of functions could come before setup().

Put in setup() the code that you want to be executed once, when the sketch starts.

#include statements should be put at the top of the file. Global variables, types, preprocessor definitions and so on are conventionally put at the top of the file after the #includes. Your function definitions such as setup() and loop(), and any other functions you want to define, can follow below the global definitions. There are no restrictions about which order you put the functions in, but it’s often helpful to put abstract functions towards the top of the file and primitive functions towards the bottom of the file - but you can arrange them however you want.

Good information folks. I appreciate it! I now have much better grasp of the requirements. Before this I was limited to finding a sketck similar to what I Needed and modifying it to suit my needs. Now I can start from scratch and work on my own. Thanks, Roddy :)

vaj4088: Declaration of functions could come before setup(), although traditionally they would go in a .h file. Definition of functions could come before setup().

You confuse lexically before (ie before in the file) and temporally before (what happens at runtime).

Functions are already in existence at runtime, they are effectively all declared already.

The Arduino software adds function prototypes so you don't have to worry about lexical ordering either.

It seems that this should be pointed out in the documentation somewhere, especially when the beginner such as me, is starting to code. local VS global variables.

"scope" of variables isn't discussed somewhere already?

CrossRoads: "scope" of variables isn't discussed somewhere already?

It is. https://www.arduino.cc/en/Tutorial/Variables