Go Down

Topic: int confusion (Read 942 times) previous topic - next topic


My only knowledge of this programming is through the tutorials on the website and I'm trying to write a program using lots of variables. Basically, I'm confused by int. It seems to determine pins as well as provide a variable i could use in some mathematical functions i need to perform.

int AX = 23

does that mean that I now have a variable called AX, or have I assigned AX to pin 23 (which is my intention here). Also I need other variables I can use throughout the code such as ACcounter. How do I make myself a variable without saying

int ACcounter = 0

without making it sound like I want to call pin 0 a name?

much thanks :)


Dec 09, 2010, 07:11 pm Last Edit: Dec 09, 2010, 07:12 pm by Korman Reason: 1
In your example, you just assigned at AX the value 23. The link to pins come only later on, when you call functions that expect a pin number like
Code: [Select]
digitalWrite(AX, LOW); In this case, you could also write 23 instead of AX, but it might become a mess if you decide to change the pin to 22. In the first case you just change the value of AX in one place and everything else falls in place. If you'd have sprinkled 23s across your code, you'd have to change them all and at the same time make sure you don't change any 23 that aren't used as references to the pin.

Another common point of confusion is that in the Arduino library, the anaolgue pins and digital pins are both numbered from 0. So
Code: [Select]
analogRead (2) reads date from another pin as
Code: [Select]



int AX = 23; means, in computer speak "There shall be a variable named AX and it shall be an Integer.  AX shall be equal to 23 at first, unless we change it later."

This is equivalent:
Code: [Select]

int AX;
AX = 23;

"int" itself doesn't really have anything to do with setting pins.  It is merely a variable type for C (and other languages).

But, since the pin-setting functions take integer values as a parameter, and since variables are handy  in allowing you to setup your pins at the top of the code instead of in the middle, then most of the time when you set a variable to represent a pin number, you will want to make it an int variable.


thanks for the help, but how do i now say that I want pin 23 to always be called AX, for instance, so i only need to say analogRead (AX) ie, the pin called AX?


You just did.  Providing you don't change the value of AX, it will keep the value 23.

If you really want to make sure (and save a bit of RAM) you could use
Code: [Select]
#define AX 23

At the beginning of your sketch, in this case AX is hardwired with the value 23 and can't be changed by the sketch.


does that mean pin 23 then or just an arbitary 23?


It's an arbitrary 23.  It's the digitalWrite() function that knows how to interpret the value of 23 stored in AX as an actual hardware port and pin on the microcontroller itself.

And maybe that's also worth pointing out here.  The Arduino pin# is unknown to the actual Atmel chip.  There is a mapping that occurs to translate "Arduino pin #10" to "Atmega port B, pin 4" for example.


how can i get in there and edit the code, any time that i try to open it with gedit it tells me that i do not have the proper character coding and to be sure that i am not opening a binary file and if i open it with a text editor its mostly symbols and such.


I like Notepad++ for windows... but are you suggesting you want to edit the Arduino core code?  I would suggest that you do *not* want to edit the Arduino code.  I think you only think you need to edit it to achieve what you want, but there are probably more appropriate ways to do it.

Go Up