Arduino Basics - Pin Number vs. Initial Value

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
User avatar
CP_LCV
 
Posts: 74
Joined: Wed Nov 14, 2012 7:35 pm

Arduino Basics - Pin Number vs. Initial Value

Post by CP_LCV »

Greetings!

So if I have the following code:

Code: Select all

int LEDPin = 9; //Pin 9 will be the pin connected to an LED
int MyVal = 10; //MyVal has an initial value of 10
What determines whether the number is the pin you want to connect to vs. the initial value you want the variable to start with?
Feel free to point me towards another thread that answers this. I searched a couple terms and couldn't find what I was looking for.

Thanks!

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino Basics - Pin Number vs. Initial Value

Post by adafruit_support_bill »

What determines whether the number is the pin you want to connect to vs. the initial value you want the variable to start with?
You do. Both statements are doing exactly the same thing: They are declaring an integer variable and assigning an initial value to it.

What you do with that variable in the rest of your code is up to you.

User avatar
johnpark
 
Posts: 985
Joined: Wed Mar 25, 2009 2:15 pm

Re: Arduino Basics - Pin Number vs. Initial Value

Post by johnpark »

Hello! If I follow your questions correctly, you're asking what determines the value you assign to these two (or any) variables?

Here's a way to look at it: an int (integer) variable you create is just a convenient name that you can use to refer to a whole number later on in your code.

In your code example you are creating a variable named LEDPin and assigning it a value of 9. That means later on in your code you can have lots of places where you do something like light up the LED with the code:

Code: Select all

digitalWrite(LEDPin, HIGH);
and then later on, turn it off with

Code: Select all

digitalWrite(LEDPin, LOW);
Why do this at all? Well, one reason is you can change your mind about which pin has the LED plugged into it on the microcontroller and you only need to change one piece of code to effectively update it everywhere else. Up at the top of your program, you would change it to:

Code: Select all

int LEDPin = 10; //Pin 10 will be the pin connected to an LED
Now, everywhere in your program that the LEDPin variable is used will refer to pin 10 instead of pin 9.

Your myVal variable could be used for something else, maybe the number of seconds to pause between LED blinks. So this isn't a pin number, but instead a timing value where you'd multiply myVal by 1000 milliseconds in this line:

Code: Select all

delay(myVal * 1000);
Hope this helps.

User avatar
CP_LCV
 
Posts: 74
Joined: Wed Nov 14, 2012 7:35 pm

Re: Arduino Basics - Pin Number vs. Initial Value

Post by CP_LCV »

Thanks for the responses!

So, if I have LEDpin=9 and myVAL=10, these two variables are just variables with values of 9 and 10. When I declare LEDpin is an output, the controller then looks at the value assigned to LEDpin (in this case, 9) and ties that variable to pin 9.

However, without doing that, both simply remain variables with initial values of 9 and 10.

Make sense?

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino Basics - Pin Number vs. Initial Value

Post by adafruit_support_bill »

When I declare LEDpin is an output, the controller then looks at the value assigned to LEDpin (in this case, 9) and ties that variable to pin 9.

However, without doing that, both simply remain variables with initial values of 9 and 10.
Yup. That's pretty much it. :)

User avatar
CP_LCV
 
Posts: 74
Joined: Wed Nov 14, 2012 7:35 pm

Re: Arduino Basics - Pin Number vs. Initial Value

Post by CP_LCV »

Excellent! Thank you! I just never made the connection between the PinMode command and the assigned value to the variable, mostly because I would read the notes and it would say things like:

int LEDPin=9; //LED to Pin 9

Or something similar. I never connected that the 'LED to Pin 9' doesn't actually happen until later in the code.

Cheers, fellows! Have a fine weekend!

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

Return to “Arduino”