Sketch format convention: why?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Balloondoggle
 
Posts: 8
Joined: Tue Dec 06, 2011 4:40 pm

Sketch format convention: why?

Post by Balloondoggle »

I'm just beginning to learn how to code this stuff, and I have noticed something that seems odd to me. There must be a reason for it though, so I'm asking for some clarity.

If the Setup section of a sketch is where you establish pinMode and such, why are variables set up ahead of this section? It seems to me that the logical way would be to put in all your explanatory or descriptive comments in the header, then define the variables within the Setup () section where you define the other functions of the sketch.

Example:

int ledPin = 10;

void setup() {
pinMode(ledPin, OUTPUT);
}

Can't the "int ledPin = 10" go in void setup()? Why would anything go ahead of setup()? For that matter, why even assign pin 10 as "ledPin"? pinmode(10, OUTPUT) should work just as well I think.

User avatar
jasonwebb
 
Posts: 111
Joined: Sat Sep 10, 2011 2:15 pm

Re: Sketch format convention: why?

Post by jasonwebb »

This is done because of a little thing called scope. In other words, every time you do something between a set of brackets (like { and }), the data that is created is not accessible to things outside those brackets.

Let's expand your example to see why it wouldn't be a good idea to put everything inside of setup.

Code: Select all

void setup() {
  int ledPin = 10;
  
  pinMode( ledPin, OUTPUT );
}

void loop() {
  digitalWrite( ledPin, HIGH ); 
}
Copy that into Arduino and hit compile, you'll see an error about what I mentioned. Now try to move the "int ledPin = 10;" line outside of the setup method and compile again. Now that the variable ledPin exists outside of both the setup and loop methods, it can be accessed by them both later.

Hope this help :)

Balloondoggle
 
Posts: 8
Joined: Tue Dec 06, 2011 4:40 pm

Re: Sketch format convention: why?

Post by Balloondoggle »

That does help, thanks.

I didn't understand that the brackets insulated the code between them from other parts of the sketch. Because of this, any universal variables or definitions must be outside any set of brackets in order for subsequent subroutines to access them. Got it. :)

To continue with the thought, why then are pinMode commands always in setup ()? Does the ATMega store these settings internally, making them relatively independent of the rest of the sketch? I kind of picture these types as "physical" settings on the chip itself - maybe that's the difference?

I realize these are things I could play with on my own, but I can't put the software on my work computer, and once I get home I can't get the kids off the home computer!

User avatar
baldengineer
 
Posts: 127
Joined: Sun Sep 26, 2010 11:49 pm

Re: Sketch format convention: why?

Post by baldengineer »

Balloondoggle wrote:To continue with the thought, why then are pinMode commands always in setup ()? Does the ATMega store these settings internally, making them relatively independent of the rest of the sketch?
That is exactly why. There are registers in the ATmega that define the I/O direction of the port. These are physical registers, not "variables." You only need to set them once. (You can change them whenever you want, however.)

Balloondoggle
 
Posts: 8
Joined: Tue Dec 06, 2011 4:40 pm

Re: Sketch format convention: why?

Post by Balloondoggle »

Great! I learned something today!

Thanks folks!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”