Confusing variables

by hardada on Fri Sep 30, 2011 2:48 pm

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
hardada
 
Posts: 17
Joined: Sat Sep 10, 2011 12:50 pm

Re: Confusing variables

by adafruit_support_bill on Fri Sep 30, 2011 3:31 pm

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);
User avatar
adafruit_support_bill
 
Posts: 25601
Joined: Sat Feb 07, 2009 9:11 am

Re: Confusing variables

by JamesC4S on Tue Dec 06, 2011 12:19 pm

hardada wrote:Im reading Brian Evan's programming notebook ???
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
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);


Instead of:
Code: Select all
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.
User avatar
JamesC4S
 
Posts: 115
Joined: Sun Sep 26, 2010 10:49 pm
Location: Austin, TX