Go Down

Topic: Trying to learn how to code Sketches (Read 1 time) previous topic - next topic

tas99

Help this newbie.

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.

Thanks,

Tom

ahref

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
Code: [Select]

//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
}

Arrch

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.

Jack Christensen

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().

Try http://arduino.cc/en/Tutorial/Foundations
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

modeller


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.

ahref

I think the word sketch has something to do with the way the code is used on the device. I too dislike its usage though.

AWOL

Quote
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".
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

JosAH


Quote
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.

kind regards,

Jos

AWOL

Quote
A sketch also has a main() function,

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

JosAH


Quote
A sketch also has a main() function,

No, it doesn't.
The IDE adds the main to the sketch that you write.


That's what I wrote in my previous reply ;-)

kind regards,

Jos

modeller


Quote
A sketch also has a main() function,

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.

AWOL

Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

modeller


Quote
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.  :)

Arrch


Go Up