0

Im trying out the code found here , let me quote the section of code that bothers me.

// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 2;      // switch to turn on and off mouse control
const int mouseButton = 3;    // input pin for the mouse pushButton
const int xAxis = A0;         // joystick X axis
const int yAxis = A1;         // joystick Y axis
const int ledPin = 5;         // Mouse control LED

xAxis is a variable that is defined as an integer , however, it is being assigned a character(A0), How is this possible? I tried to compile and run the code and everything works well. I'm quite surprised!

0

2 Answers 2

0

A0 and A1 are not characters or what you mean probably: strings. Because it would be written as "A0" and "A1". The types of A0 and A1 are probably integers or another numeric type.

However, these are probably defined as (constant) integers, e.g.:

const int A0 = 100;
const int A1 = 101;

(note 100 and 101 are random numbers)

or defined by a macro. e.g.:

#define A0  100
#define A1  101

So the variables A0 and A1 are actually integers (or assigned as numeric values by a define).

1
0

The clue is this comment:

// set pin numbers for switch, joystick axes, and LED:

and the fact that xAxis is declared a const int. Because the declaration promises that this variable will not (must not) be changed once it has been initialized, it will not be assigned any memory at run time. The compiler will treat it much like the macro:

#define xAxis A0

and do a compile-time replacement of xAxis with A0, wherever xAxis appears.

A0 is an analog channel, defined in the board-specific Arduino libraries to the Arduino pin# corresponding to it.

It is a somewhat recent idiom in C and C++ - since the inclusion of "const" in the language - to use "const int" declarations for this purpose instead of "#define" statements. The intent is the same.

Not the answer you're looking for? Browse other questions tagged or ask your own question.