Trying to Serial.print an array of analog pin names, getting 14 instead of A0

Hi, I'm making a small datalogging device using an Arduino Pro Mini that reads from a selection of analog pins and send the readings over serial. Then I have a python script to read the values into a CSV file on my PC.

In the setup function I want to send a list of the pins being read as a 'header' to the CSV file, so the data can be saved like this:

A0,A1,A2,
2.80,2.77,2.51,
2.45,2.43,2.39,
2.34,2.29,2.28,
2.27,2.21,2.20,
2.18,2.13,2.10,
etc

However instead of getting A0 I get 14 out of the serial monitor (likewise for the other pin names) - here is a dump of the data that comes through the serial monitor currently:

14,15,16,
2.80,2.77,2.51,
2.45,2.43,2.39,
2.34,2.29,2.28,
2.27,2.21,2.20,
2.18,2.13,2.10,

I am aware that as int types A0 == 14 for the purposes of using analogRead(), but I don't know how to send out the list in the 'A0' form.

const int inputpins[3] = {A0,A1,A2};                    // define the pins being measured
int pin_count = sizeof(inputpins)/sizeof(inputpins[0]); // Count the number of pins

float Vcc = 5.01;                                       // For scaling ADC measurements to voltage



void setup() {

  Serial.begin(9600);
  for (int pin = 0; pin < pin_count;pin++){             // set up serial                            
    Serial.print(inputpins[pin]);                       // send list of pins being measured!!
    Serial.print(",");
  }
Serial.println();
}

void loop() {
  String values;
  for (int pin = 0; pin < pin_count;pin++){                                    
    float V = analogRead(inputpins[pin])*Vcc/1024.0;    // read voltage from pin (have to convert the string entry into 'int' first by removing the 0)
    values += V;
    values += ",";                                      // append the readings to the string of values followed by a ","
  }

  Serial.println(values);                               // send values to serial
  delay(500);

}

I've tinkered a fair amount with Arduino over the years, but have never had to deal with anything other than int and float types for most of my projects, and only ever using Serial.print to print basic messages so have never had this kind of issue arise previously.

That's because A0, A1 and A2 are constants defined in Arduino.h

If you are just making a header, why not as text literals, "A1","A2", etc.

The arduino just uses the name "A0" to mean pin 14, "A1" to mean pin 15, etc. It makes it human readable, but does pose this issue if used incorrectly. Like the comment above mentions, use literal strings like "A0, A1, A2" if possible.

For example, set the first line as "A0, A1, A2", then the second line onwards is your data.

Technokid2000:
The arduino just uses the name "A0" to mean pin 14,

Note that this depends on the Arduino board.

In general A0 will have the next higher number after the digital pins on a board. The highest digital pin on an Uno is 13 so A0 is 14.

...R

Correct. My Arduino Mega reports those pins being pins 53, 54 and 55

As long as the analog pins increment sequentially from A0, you can print the Ax format easily by subtracting the value for A0 from the stored pin number:

  for (int pin = 0; pin < pin_count; pin++) {
    Serial.print("A");
    Serial.print(inputpins[pin] - A0);
    Serial.print(",");
  }

Also, there is no need to read the values for all the inputs and save to a String, just print each value as you get it, serial is so slow compared to the speed of the arduino that the computer on the receiving end will never detect any difference in timing, and even if it could, the receiving program should be looking for the complete line of data donated by the carriage return and linefeed characters.

Thanks for the responses, I ended up going with @david_2018 idea which worked well. Having the pins increment sequentially was a compromise since I would have liked it to work from an unsequenced list of pins too. That being said, it's simple and perfectly workable.

rorzor:
Thanks for the responses, I ended up going with @david_2018 idea which worked well. Having the pins increment sequentially was a compromise since I would have liked it to work from an unsequenced list of pins too. That being said, it's simple and perfectly workable.

The limitation is that the actual integer numbers that correspond to the analog pin designations have to be sequential, not that you have to use them in sequential order. Not all boards meet that limitation, the boards based on the atmega328 do, so that A0 = 14, A1 = 15, A2 = 16, etc, but for example on at least one ESP8266 variant A0 = 3, A1 = 1, A2 = 16, etc.