How can "A0, D7" etc be integers?

Hi guys,

Just a quick/stupid question for my better understanding of this:

// name the pins
int led1 = D7;

Isn’t int supposed to be integer? How is it storing a value that has a character “A” or “D” in it?

Also what is the best practice for referring to pins on the Spark Core? If i just say pinNumber = 3 (as I’ve seen in some examples) - how does the program know whether I’m referring to A3 or D3?

Thanks,
R

@Rehaan, interesting observation. You are correct in saying that “int” denotes an “integer” type. Did you notice, however, that D7 is not surrounded by double quotes? Those quotes would indeed make “D7” a string. Without the quotes, the compiler interprets D7 as constant, which is defined in the Spark and STM32 include files as GPIO pin 7.

So when you want to refer to a data pin, you would use Dn where n is the digital pin number (0-7). For analog pins, you would use An where n is the analog pin number (0-7). So you end up with pins D0-D7 and A0-A7. This is NOT the same convention as the Arduino. :slight_smile:

2 Likes

Thanks @peekay123! That makes perfect sense. Also explains another doubt i had elsewhere.

Since i don’t want to start another topic just for this Q, could you tell me what exactly this command does?

int pinNumber = (command.charAt(1) - '0') - 1;

…i understand command.charAt(1), but what is the - 0 and -1 for? (This if from the “Controlling LEDs over the 'net” example)

Thanks,
R

@Rehaan, I know this code well. In this case, command is of type String and command.charAt(1) will extract a character of the command String and subtract the ascii value of ‘0’ from it. So if the extracted character is ‘7’ which has a decimal value of 55 and you subtract the decimal value of ascii ‘0’ which is 48, you get integer value 7! For the Spark, the pinNumber corresponding to pin 7 is one less than that, which explains the “-1”. :smile:

1 Like

@Rehaan & @peekay123, to avoid confusion I’d like to point out that the correct conversion “D7” -> D7 -> 7 would be this

 int pinNumber = command.charAt(1) - '0';

I have seen in the docs that the annotated example “CONTROL LEDS OVER THE ‘NET’” (still) shows the code you posted

int pinNumber = (command.charAt(1) - '0') - 1;

There were some ancient threads here and here that - amongst other things - addressed the risk for confusion that LED1 is connected to D0 and LED2 to D1 and hence you’d need the extra -1, but for the translation “D7” -> D7 (=7) you don’t.
(Maybe it’s time to change the sample wiring and drop the -1 in the sample code :wink: )

[ if TL;DR you can stop here :wink: ]

In fact these are the defines of spark_wiring.h

/*
* Pin mapping. Borrowed from Wiring
*/

#define TOTAL_PINS 21
#define TOTAL_ANALOG_PINS 8
#define FIRST_ANALOG_PIN 10

#define D0 0
#define D1 1
#define D2 2
#define D3 3
#define D4 4
#define D5 5
#define D6 6
#define D7 7

#define LED1 LED_USER

#define A0 10
#define A1 11
#define A2 12
#define A3 13
#define A4 14
#define A5 15
#define A6 16
#define A7 17

#define RX 18
#define TX 19

#define BTN 20

So for "D7" take only '7' (=decimal 55) minus '0' (=decimal 48) which gives you already the required decimal 7 (=D7).

When you look at the defines for A0 to A7 you will notice that they are defined 10 greater than D0 to D7 which is quite convenient since you can use the same char->int conversion and then only add 10 if the first character is an A.


BTW: It’d be good practice to search the forum before re-raising a question. Everybody here is eager to help, so please help us helping by investigating first.
I know it’s not easy to guess what to search for, but LED1 or int pinNumber as search words led to the two old threads dealing with the very same question (granted - only under “show more” since these threads are so old).
On the other hand searching would be much easier with topic tagging ( :wink: @jgoggins )

2 Likes

@ScruffR, thanks for the clarification! :smile:

Thanks @ScruffR!

I did actually try searching before creating this, but found no relevant threads! Will look harder next time :wink:

One more Q: Why do we have to convert ACSII “7” to Decimal “7” like this? In arduino doesn’t it just work regardless? (Sorry, im not very clued in about this stuff).

EDIT:Would it be possible to use string.toInt() instead?

Thanks,
R

@Rehaan, your first and third question are addressed in the two threads I linked above

The two “here”'s are links (I’ve just checked if they actually worked)

And as for Arduino it’s no different. Pins are referenced by numbers, not by strings - even if it might look like it.

#define LOW 0
#define HIGH 1
#define SOMEPIN 5

digitalWrite(SOMEPIN, HIGH);
// is actually only a more human readable form of
digitalWrite(5, 1);

SOMEPIN and HIGH might look like strings but they are only preprocessor macros.
The construct #define MACRONAME (5+6) instructs the precompiler to substitute any appearance of MACRONAME with the “code snippet” (5+6). So when the compiler does its job it actually sees (5+6) and never ´MACRONAME´ - hence it’s also important that you don’t put a ; at the end of a macro (unless you explicitly want it to show in the preprocessed code :wink: )