Pages: [1]   Go Down
Author Topic: Trying to learn how to code Sketches  (Read 928 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Online Online
Faraday Member
**
Karma: 88
Posts: 3365
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 93
Posts: 3967
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

TX
Offline Offline
Jr. Member
**
Karma: 2
Posts: 99
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".
Logged

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

Netherlands
Offline Offline
Full Member
***
Karma: 3
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Netherlands
Offline Offline
Full Member
***
Karma: 3
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

TX
Offline Offline
Jr. Member
**
Karma: 2
Posts: 99
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

TX
Offline Offline
Jr. Member
**
Karma: 2
Posts: 99
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley
Logged

California
Online Online
Faraday Member
**
Karma: 88
Posts: 3365
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Everyone's a critic...
Logged

Pages: [1]   Go Up
Jump to: