Hi,
I used to use: int x = 2, kind of declaration, to assign the variable x to pin 2. I just got confused when I need assign a value 2 to x by : int x = 2, what really that means of 'int x = 2' or what should I do in both situations?
Thanks
Adam
Hi,
I used to use: int x = 2, kind of declaration, to assign the variable x to pin 2. I just got confused when I need assign a value 2 to x by : int x = 2, what really that means of 'int x = 2' or what should I do in both situations?
Thanks
Adam
I'm not sure I follow, what problem are you having?
"int x" creates (declares) a new integer variable named x.
" = 2" assigns it a value of 2.
"x = 2" assigns the value of 2 to a previously declared variable named x.
There are many good language tutorials on the web. Take advantage of them, they are free education!
There is something tricky you should be aware of called [u]scope[/u].
If you've already declared/defined a variable called 'X' you can declare another variable called 'X' inside a loop or a function and they can hold different values.
So generally, you can put int X; or int X = 2; in setup(). Then it's a global variable and if you want to assign or change the value later you leave out the 'int' and just write X = 3; or X++; , etc.
It also helps if the variable name means something like "PotReading" instead of "X". (It's common to use simple variables like 'i' or 'X' for temporary-local variables in a loop, etc.)
So generally, you can put int X; or int X = 2; in setup(). Then it's a global variable
Nope.
above setup()
Since everyone else is having a go....
NEVER name a variable x! Bad, bad OP! Except in cases where x is appropriate, of course, i-i-it's complicated.
What was the question, again?
I quite often use the name "x" for the x-coordinate variable, and that works pretty well.
"y" works for the y-coordinate variable, too, which is remarkably convenient.
I'll let you guess what I do with the z-coordinate.
A y? Preposterous!
I'm trying that, hang on.
Well I'll be a monkeys uncle...
Thanks for all replies.
Firstly let me clean my question please: that is when I see: int x = 2; should I think it is a value 2 assigned to variable x? or it is a variable x assigned to PIN 2, like LED = 13?
Then the x here is just represent a variable.
C and C++ predate Arduino. They're general purpose programming languages. You can use an integer variable for any purpose, there is no implication whatsoever that it refers to an Arduino pin. Of course, if you want to use it for that, you can, but it could just as easily have been a temperature or atmospheric pressure you got from somewhere.
Also, if it really is a pin number, use a byte, not an int
laoadam:
Thanks for all replies.
Firstly let me clean my question please: that is when I see: int x = 2; should I think it is a value 2 assigned to variable x? or it is a variable x assigned to PIN 2, like LED = 13?
Then the x here is just represent a variable.
x is a variable and its value is 2.
What you do with it matters: if you do
digitalWrite( x, HIGH );
then pin 2 will be set HIGH (assuming its pinMode() was set to OUTPUT.) It's not really "assigned" to pin 2 but if that's all you use it for then you could say it is "assigned".
There's nothing stopping you doing math on variable x that results in it being another value -- say, 9 -- and then doing a digitalWrite( x, ...) to change the state of pin 9.
If you want to ensure you can't accidentally do math on 'x' because you want it to always equal two set it as a const:
const int x = 2;
(It bears mentioning that using 'int' is generally frowned upon to represent pin numbers since the 'int' requires 2-bytes (16 bits) while the pin number can fit into a byte (8-bits.) You can same some RAM by specifying byte or uint8_t etc for pin constants.)
Blackfin:
x is a variable and its value is 2.What you do with it matters: if you do
digitalWrite( x, HIGH );
then pin 2 will be set HIGH (assuming its pinMode() was set to OUTPUT.) It's not really "assigned" to pin 2 but if that's all you use it for then you could say it is "assigned".
There's nothing stopping you doing math on variable x that results in it being another value -- say, 9 -- and then doing a digitalWrite( x, ...) to change the state of pin 9.
If you want to ensure you can't accidentally do math on 'x' because you want it to always equal two set it as a const:
const int x = 2;
(It bears mentioning that using 'int' is generally frowned upon to represent pin numbers since the 'int' requires 2-bytes (16 bits) while the pin number can fit into a byte (8-bits.) You can same some RAM by specifying byte or uint8_t etc for pin constants.)
Thanks.
You made my day.
In Arduino, int is 16 bits, byte is 8 bits.
Use byte for pin assignments, as they are always <255, the upper limit of a byte.
Even better, use
const byte pin2 = 2;
then it will not consume SRAM either; the compiler will hard code 2 anyplace it sees pin2 in the code.
In Arduino, int is 16 bits, byte is 8 bits.
In Arduino int and byte are keywords.
The declaration "int x;" tells the compiler to reserve 16-bit storage space (two 8-bit wide consecutive memory locations) for the variable x.
The declaration "byte y;" tells the compiler to reserve 8-bit storage space (one memory location) for the variable x.
Thanks.
I understand now.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.