Confusing variables

Im reading Brian Evan's programming notebook ???

Page 9
int x = 13; //declares variable x as the integer 13 .... OK I think I understand ??

Page 10
int inputVariable = 0; // declares variable assigns value on 0 ...OK..
inputVariable = analogRead(2); // set variable to value of analog pin 2 ....Ok...

Page 22
int led = 13; //connect LED to pin 13 ?????
?? //why does this not just declare a variable called led and assign it the value of 13 as was on page 9.
int pin = 7; // connect pushbutton to pin 7 ????

int value = 0; //variable to store data....... this actually is a variable not a pin number ??

Basically.........
If I declare say int pin1=6; does it mean a variable assigned the value of 6 .... or does it mean pin1 uses the pin number6.

or int switchA=8; is it value 8 or pin 8.

Thanks

Posts: 28
Joined: Sat Sep 10, 2011 1:50 pm

Re: Confusing variables

He is just declaring variables and assigning them values so he can use symbolic names to represent pin numbers.

For example:

digitalWrite(led, HIGH);

is more explicit than

digitalWrite(13, HIGH);

Posts: 39296
Joined: Sat Feb 07, 2009 10:11 am

Re: Confusing variables

int led = 13; //connect LED to pin 13 ?????
?? //why does this not just declare a variable called led and assign it the value of 13 as was on page 9.
int pin = 7; // connect pushbutton to pin 7 ????

Your confusion is a great example of writing good comments... (Which these are not.) The comment suggests that the LED is physically connected to pin 13. This may be what happens in the hardware, but the software does not make any such connection for you. All that happens is that the variable "led" has a value of 13.

This means later on in the code you can do this:
Code: Select all | TOGGLE FULL SIZE
`pinMode(led, OUTPUT);digitalWrite(led, HIGH);`

Code: Select all | TOGGLE FULL SIZE
`pinMode(13, OUTPUT);digitalWrite(13, HIGH);`

Both of these code examples do the same thing (assuming led has a value of 13): setup pin 13 as an output pin and set its output to HIGH.

hardada wrote:int value = 0; //variable to store data....... this actually is a variable not a pin number ??

Variables, by themself, have nothing to do with pin numbers. Variables store data. That's all.

hardada wrote:If I declare say int pin1=6; does it mean a variable assigned the value of 6 .... or does it mean pin1 uses the pin number6.

You created a variable called "pin1" which has the value of 6 (an integer.) Again, nothing explicitly to do with pins.

jamesc4s

Posts: 115
Joined: Sun Sep 26, 2010 11:49 pm
Location: Austin, TX