I don't seem to follow you... I agree that 'int' usually refers to numbers, but the reason I'm using int is to assign the number 1 to that variable called micSwitch.
I guess I'll try to rephrase my question. What is the difference between assigning a pin and assigning a value to a variable?
One of the things that the Arduino libraries do is assign each pin an integer value, to uniquely identify it. (This is instead several possible alternatives like "PORTB, PB5", "PORTB.PB5", "0x25, 0x10", etc.)
Since the pin is "named" with a number, you can refer to it refer to it with any variable or constant that holds a number.
You can't "assign a pin"; I'm not even sure what that phrase would mean. You can read a pin, or you can write a pin...
card9inal:
I have a very basic question that I haven't found an answer to.
I'm going through and attempting to write a sketch, but I'm confused as to how Arduino sees the 'int' as a pin.
For example, in the Blink sketch:
int led = 13;
How does it know that it's supposed to be pin 13 that's going to be blinking?
It doesn't know, it's just defining an integer variable and setting it's value to the number 13. You the programmer knew there was a led wired to output pin 13, so you made this assignment so later you could use the value in a digitaWrite() function.
If my code was:
int micSwitch = 1;
Does that also assume that the micSwitch is on pin 1, instead of assigning the value of 1 to the int?
No, again just setting a variable called micSwitch to the number 1. The programmer (you) knew that you were going to wire a physical switch contact to arduino pin number 1, so you wrote this assignment based on that knowledge, so that later you could use the variable name in a digitalRead() function
I'm confused!
The arduino function digitalWrite(led, HIGH); takes the value contained in the variable led and uses that number to figure out which I/O pin to use and set that pin's value to HIGH, thus turning on that pin to +5vdc. Likewise digitalRead(micSwitch); uses the value contained in the variable micSwitch and uses that to figure out which input pin to read and return it's present value of either 1 or 0. Note that you could have simply used the number 1 directly in the digitalRead function as in digitalRead(1); and accomplish the same thing. However it is a better practice to define and assign such things as pin numbers at the start of your sketch so that if you ever have to change the physical pin number to use, you only have to change it at one place, at the beginning assignment, rather searching out every place you used a digitalRead(1) function throughout your program. Does that help? Lefty
card9inal:
I have a very basic question that I haven't found an answer to.
I'm going through and attempting to write a sketch, but I'm confused as to how Arduino sees the 'int' as a pin.
For example, in the Blink sketch:
int led = 13;
How does it know that it's supposed to be pin 13 that's going to be blinking?
It doesn't know, it's just defining an integer variable and setting it's value to the number 13. You the programmer knew there was a led wired to output pin 13, so you made this assignment so later you could use the value in a digitaWrite() function.
If my code was:
int micSwitch = 1;
Does that also assume that the micSwitch is on pin 1, instead of assigning the value of 1 to the int?
No, again just setting a variable called micSwitch to the number 1. The programmer (you) knew that you were going to wire a physical switch contact to arduino pin number 1, so you wrote this assignment based on that knowledge, so that later you could use the variable name in a digitalRead() function
[/b]
Lefty...that's where I'm confused, because I'm not attempting to read this pin. I'm strictly using this as a variable, but Arduino is assuming that I want to either read or write, depending if I set it as an INPUT or OUTPUT, to that pin. I don't! I'm not necessarily planning on using digitalRead(micSwitch).
This likely isn't the best way to do this, so maybe that's where I'm getting messed up, but in my sketch, I'm going to do a few things:
I'm going to set the value of micSwitch to 1.
Then I'll check to see if micSwitch is set to 1 (which will mean on in my world), and if it is, set pin 5 (for illustration purposes) HIGH.
If micSwitch is 0, then set pin 5 LOW.
If I press coughButton, I want to check the status of micSwitch. If micSwitch is 1, I want to set micSwitch to 0, and set coughOutput on pin 6 HIGH.
Does that make sense?
I'm just using this as a variable that will have it's values changed during the program. I don't really want it to start reading or writing to that pin...
Lefty...that's where I'm confused, because I'm not attempting to read this pin. I'm strictly using this as a variable, but Arduino is assuming that I want to either read or write, depending if I set it as an INPUT or OUTPUT, to that pin. I don't! I'm not necessarily planning on using digitalRead(micSwitch).
This likely isn't the best way to do this, so maybe that's where I'm getting messed up, but in my sketch, I'm going to do a few things:
I'm going to set the value of micSwitch to 1.
Then I'll check to see if micSwitch is set to 1 (which will mean on in my world), and if it is, set pin 5 (for illustration purposes) HIGH.
If micSwitch is 0, then set pin 5 LOW.
If I press coughButton, I want to check the status of micSwitch. If micSwitch is 1, I want to set micSwitch to 0, and set coughOutput on pin 6 HIGH.
Does that make sense?
I'm just using this as a variable that will have it's values changed during the program. I don't really want it to start reading or writing to that pin...
You question is a little confusing. But as a general statement, with an integer variable such as int myVariable = 7; you are free to change it's value to any value (that will fit in a 16 bit integer) you want anywhere within your program and as many times as you wish. That's why they call them a variable because it's value is subject to change by other programming statements. You are NOT restricted to assigning a value to a variable only once.
After stewing on this some more, I'm thinking that maybe it just doesn't matter until you go to use digitalRead() or another function with that variable that it will matter...
card9inal:
After stewing on this some more, I'm thinking that maybe it just doesn't matter until you go to use digitalRead() or another function with that variable that it will matter...
I believe that to be a true statement, it's not a actual real pin number until it's used in a digitalRead , digitalWrite, analogRead, analogWrite, or a pin mode statement.