Sketches - What goes in Global, Setup and Loop?

In reading and watching different videos about sketches I'm a bit confused about what to put in Global, Setup and Loop.

I thought...

Global above the Setup section? Is for libraries The includes go here. (But why not setup?)

Setup for one time declarations such as if pins are input or output and define variables.

Loop is where the code/program that does things and runs over and over again.

But in one video I was watching the guy was saying to declare variables in Global above the setup. (But did not explain why.

Can someone offer a bit of clarification? What is best practices?

Thank you

C/C++ basics. Global - do you mean space above setup() function? It is for include, global variable declaration etc.
The setup() and loop() are normal functions called from another place. It doesn't matter now, but it is a place for your code you want to run. The setup is running just once at Arduino start then the loop is executed repeatedly.

Post some exact problem or code for discussion.

#includes must be outside of any function (should be at beginning of file)
Global variables must be defined outside of any function (should be at beginning of file).
Local variables must be defined inside of the function they are to be local to.

No function calls or code can be located outside of a function, and function definitions must be located outside of all functions.

When an Arduino sketch starts, it first calls an internal function to intialize the hardware (so you can do PWM and analogRead and all that stuff), then it calls your setup() function, and then calls loop() repeatedly (ie, while(1){loop();} ) - this usually makes a lot of sense for typical embedded projects, where you have to set up some stuff like pin modes, before the normal program flow starts - it’s entirely possible to have a sketch where loop() is empty (for example) and all the code is in setup, meant to run once and then not do anything else (I’ve certainly written sketches like that).

Thanks - I'm not a C programmer so C commands are of no help. I did lean BASIC, so I understand programming concepts.

I understand setup - run once.
Loop - loop through continually

These are both functions, right? Are there other functions?

What's the differcen betwee declaring a varible outside of setup (in global) and in setup?

Am I correct in that if a varible was declared in loop it would just be initalized over and over again?

Thanks again for the help.

These are both functions, right? Are there other functions?

What's the differcen betwee declaring a varible outside of setup (in global) and in setup?

There are thousands of functions including ones that you write yourself.

A global variable is visible/available to any code in any function including setup() and loop(). A variable defined in setup() is only visible/available in setup().


Ahhh, so it's highly unlikely one would ever declare a variable in setup... Correct?

One would either do it above setup so it can be used in all functions, (as in setup and loop)? And with libaries?

But most of the time one would declare a varible in loop. But this has the limatiation of only being able to be used in the loop function, correct?

In theory, (but not good practice) the varible X could be assiged a value of 1 in the loop funcation X=1. But in another function say setup X could be defined as 2, X=2. So in set-up X is two but in loop it's 1.
I know setup in only run once, but if it could be looped through X could be two different vaules depending on the function. Just asking to see if I understand the concept.

Can you give a couple of exampale of funcations other than setup and loop?
A libarary in not a funcation, correct? But doesn't it kind of act as one?

A variable declared in setup() is only accessible within setup() and when setup finishes, the variable goes too, freeing the memory it used.

If the variable is declared in loop, it works the same way. After each time loop() finishes, that variable is gone and it's memory freed. Then it will be again declared and initialized when you next call loop.

The exception to that is if you declare the variable inside a function with the static keyword. These will only be initialized once, but will stay in memory and keep their value between iterations of the function.

Anything called by a name followed by parenthesis is a function. For example, digitalWrite() is a function. You can define more functions if you wish (these definitions must be outside of any other function, as noted above), eg:

void blinkpin(int pin) { 

They can return a value too: 

int getDistance() {
   int d=analogRead(A0); //let's say A0 is connected to wiper a linear pot 100mm long, with ends of pot on 5v and gnd
   return map(d,0,1023,0,100); //map the 0~1023 to 0~100, giving you position of slider on the pot in mm

BASIC is an awful awful intro language - it is different from other languages in a lot of ways, and tends to teach bad habits

I was 12 years old when I learned BASIC. Needed to start somewhere. I think I still might have a couple of programs I wrote on paper tape. (Those were the days…)

I thought a function was between { }. But you are saying a function is between ( ).

What’s the term for code in barces? { }

No. A function is CALLED with parentheses which hold the parameters passed to it, if any. The parentheses are always needed even if there are no parameters.

The body of the function which does all the work is then the code between the the braces.