I’m trying to learn the coding for Sketches and while it’s beginning to make sense I have not found a good explanation on what goes before the voidsetup() function and what goes inside that function. Since it only goes through voidsetup() once why not put all the initialization and other functions in there and not before it?
Probably a very simple answer but I haven’t seen it yet.
Hi there, You initialize variables outside of setup() when you want them to be global. That is available to all parts of your sketch. You can then use them in setup to perform steps BEFORE the main loop.
it is
//Visible everywhere GLOBAL
int a = 1;
int b = 2;
int c = 0;
void setup() {
//Accessing GLOBAL variables inside setup
c = a+b;
//A LOCAL variable
int d = b-a;
}
void loop() {
// c = a * d; This will error because d does not exist outside of setup it is LOCAL to setup.
c = a * b; //This is fine because a,b and c are GLOBAL
}
First, it's called the setup function. Void is simply the return type.
Outside of the setup function is the global scope, so any global variables should be declared there (and initialized, if necessary). The setup function should contain things like object begin or start methods, pinMode or anything that requires the hardware to have been initialized.
Executable code must go in a function, e.g. the setup() function, the loop() function, or functions you write yourself. Global variables can be declared outside of functions, e.g. before setup().
tas99:
I’m trying to learn the coding for Sketches and while it’s beginning to make sense I have not found a good explanation on what goes before the voidsetup() function and what goes inside that function.
Hey tas,
It's the C programming language. I don't know why they decided to use the term "sketch", because it's a C program. Maybe they decided on that name so as to not "turn off" new experimenters - I don't know.
But if you get a basic C book or tutorial online it will be what you need to learn.
I don't know why they decided to use the term "sketch", because it's a C program.
No, because a C program would have a "main".
A sketch also has a main() function, it is written for us and calls the setup() function once and repeatedly it calls the loop() function. It's the IDE that juggles with our C/C++ files, feeds them to the linker (including that main() function) and voila.
No, it doesn't.
The IDE adds the main to the sketch that you write.
A sketch is like a sketch of a picture - an outline, and incomplete.
So? What difference does it make if it adds main() automatically, whether you see it or not? If it's there it's there and that makes it a C program like we all know it is. Instead of saying "write a sketch" they could have said "add functions to the system-created main() to make the program complete. Same thing and more accurate.
It matters not what is added automatically to make the system run. They should have just used the common names and called it what it is - a C program. It's not a sketch it's a program - a C program. Why they decided to "hide" that fact is odd, except if they wanted to avoid the term "C program" to avoid scaring prospective new experimenters.
Instead of saying "write a sketch" they could have said "add functions to the system-created main() to make the program complete. Same thing and more accurate.
...but longer.
Excuse me, but I have to embark in my hydrocarbon-fuelled road vehicle and guide it to my place of domicile.
Instead of saying "write a sketch" they could have said "add functions to the system-created main() to make the program complete. Same thing and more accurate.
...but longer.
Excuse me, but I have to embark in my hydrocarbon-fuelled road vehicle and guide it to my place of domicile.
Longer? A few more words? Another sentence - maybe two? Yea, technically it's longer. IMHO it's way better to call it what it is if it merely takes a few more words or a couple of sentences. I'm just freaky-weird like that.