Reconcile Analog

It's a known problem that the analog ports to not return their proper pin names when you do this:

servoPin = A0;

Serial.println (servoPin);

This is such a basic problem that I can't believe that someone at my level (almost no programming experience) is dealing with it, but here I am.

If you do the above on an Uno, it returns a 14. For A0-A5 (6 analog ports) its 14,15,16,17,18,19. If you do it on the Mega, it returns a 54 to start through 69 for A0-A15.

This makes it rather difficult to tell the user what port they are supposed to be using for the Analog input:

Serial.print ("This is controlled with the Potentiometer at Pin ");

So, here is my solution. The problem is, I am not a programmer so I am HOPING someone who is can streamline this code a bit. The following is only TWO pin solutions!!! Since I run a Mega, I need this expanded by another 13!! What about using the Library functions for this?

void reconcileAnalogPin1() {

/*  This is how to get around the fact that the Analog Pins do not report back the
 *  proper numbers. A0 is not what comes back when you choose for example, 
 *  analogpin1 = A0; On the Uno, A0 = Pin 14. On the Mega 2560, A0 = Pin 54. Since 
 *  the Mega has 16 Analog pins and the Uno has 6, lets resolve that issue here: */

  if (analogPin1 == 54 || analogPin1 == 14){
    Serial.print ("A0");        
  }
  else if (analogPin1 == 55 || analogPin1 == 15){
    Serial.print ("A1");
  }
  else if (analogPin1 == 56 || analogPin1 == 16){
    Serial.print ("A2");
  }
  else if (analogPin1 == 57 || analogPin1 == 17){
    Serial.print ("A3");
  }
  else if (analogPin1 == 58 || analogPin1 == 18){
    Serial.print ("A4");
  }
  else if (analogPin1 == 59 || analogPin1 == 19){
    Serial.print ("A5");
  }
  else if (analogPin1 == 60) {
    Serial.print ("A6");
  }
  else if (analogPin1 == 61) {
    Serial.print ("A7");
  }
  else if (analogPin1 == 62) {
    Serial.print ("A8");
  }
  else if (analogPin1 == 63) {
    Serial.print ("A9");
  }
  else if (analogPin1 == 64) {
    Serial.print ("A10");
  }
  else if (analogPin1 == 65) {
    Serial.print ("A11");
  }
  else if (analogPin1 == 66) {
    Serial.print ("A12");
  }
  else if (analogPin1 == 67) {
    Serial.print ("A13");
  }
  else if (analogPin1 == 68) {
    Serial.print ("A14");
  }
  else if (analogPin1 == 69) {
    Serial.print ("A15");
  }  
}
void reconcileAnalogPin2() {
  if (analogPin2 == 54 || analogPin2 == 14){
    Serial.print ("A0");        
  }
  else if (analogPin2 == 55 || analogPin2 == 15){
    Serial.print ("A1");
  }
  else if (analogPin2 == 56 || analogPin2 == 16){
    Serial.print ("A2");
  }
  else if (analogPin2 == 57 || analogPin2 == 17){
    Serial.print ("A3");
  }
  else if (analogPin2 == 58 || analogPin2 == 18){
    Serial.print ("A4");
  }
  else if (analogPin2 == 59 || analogPin2 == 19){
    Serial.print ("A5");
  }
  else if (analogPin2 == 60) {
    Serial.print ("A6");
  }
  else if (analogPin2 == 61) {
    Serial.print ("A7");
  }
  else if (analogPin2 == 62) {
    Serial.print ("A8");
  }
  else if (analogPin2 == 63) {
    Serial.print ("A9");
  }
  else if (analogPin2 == 64) {
    Serial.print ("A10");
  }
  else if (analogPin2 == 65) {
    Serial.print ("A11");
  }
  else if (analogPin2 == 66) {
    Serial.print ("A12");
  }
  else if (analogPin2 == 67) {
    Serial.print ("A13");
  }
  else if (analogPin2 == 68) {
    Serial.print ("A14");
  }
  else if (analogPin2 == 69) {
    Serial.print ("A15");
  }
}

for every board variant NUM_DIGITAL_PINS, NUM_ANALOG_INPUTS, analogInputToDigitalPin(p) are defined. use them

Have you tried it? Each board variant does it one way but we need it to go the OTHER way. They are not defined. Please try to recall the analog pin number from whatever board you have. You will NOT get A0-whatever.

If I am missing what you are saying, please provide a code solution snippet so it makes sense.

This is such a basic problem that I can't believe that someone at my level (almost no programming experience) is dealing with it, but here I am.

This is usually no problem as I never heard about printing out the pin number to tell the user something. Just write the correct value to your documentation, that's it. Wasting memory for such prints is definitely not suggested.

If you just have to support the Mega2560 and the UNO, this short function may help:

#ifdef __AVR_ATmega2560__
 #define ABASE 54
#else
 #define ABASE 14
#endif
void print_AVR_analog_pin(uint8_t pin) {
  Serial.print("A");
  Serial.print(pin - ABASE);
}

we need it to go the OTHER way

Why ?

If you really, really, really, really, really, really need to do it then test the pin number being printed and print the required form instead.

Huh? Bob, I think was very explicit in my first post what I wanted to do. Please provide a code snippet solution and stop the drive-by! I even provided what I knew. What is with making these threads 10x longer than they need to be? (Bob has been VERY helpful in the past, so give his defense a rest plz.)

Pylon... thanks for that, but can you tell me how I would incorporate that in my code? Right now, I just call that function reconcileAnalog() in the code. So..

if (topMenuChoiceB == true) {        
            Serial.print ("This is controlled with the Potentiometer at Pin ");
            reconcileAnalogPin1();
            Serial.print (".\n");
          }

Yours looks more elegant. I just do not know how it fits in the code. Thanks!

I don’t see the problem, either. The “A0” name has a value associated with it. By the time the compiler runs, the name has been replaced with a value. By the time the compiler gets done, there are nothing but memory addresses and instructions.

OMG! Ok, just re-read the first post. I am writing a Calibration program and the user needs to be informed of the pin they chose for an analog pin. It is NOT up to anyone here to debate the lack of wisdom of the end user. I am merely trying to report back the dang proper pin! Its a simple thing and a HUGE oversight by whoever wrote the IDE to not catch this very VERY basic problem. Unless you actually read and comprehend the issue, supply a solution, what are we doing here?

Its a simple thing and a HUGE oversight by whoever wrote the IDE to not catch this very VERY basic problem.

No. The problem is you.

The Ax names are for using the analog pins as digital pins. If you are using the pin as an analog pin, use the numbers from 0 to however many analog pins you have.

Please read the first post before embarrassing yourself. I don't care about how the pins are used. I want to report back to the user what pin they DID use. Since you can do that all day with Digital Pins, you SHOULD be able to do the same with Analog pins. You know, the pins that are labeled A0-A5 on an Uno for example.

You can't. You get pin 14 when you asked for A0. The user looks at the board and cant find a pin 14 anywhere! For an 8 year old trying to learn this stuff, he gets confused and stops caring. I thought this platform was for teaching electronics, robotics, etc.

Can we get back to an actual solution? You know, it's where a problem is presented and then someone who cares presents a code based solution. Why code based? Because of the Title of this Forum. Please Help? If you don't want to acknowledge this very basic flaw in the system, you don't have to post. You can move right along!!

    Serial.print('A');
    Serial.println(pin - (NUM_DIGITAL_PINS - NUM_ANALOG_INPUTS));

only for 8-bit Atmegas!

I’m lost here, can’t see any circumstance where this is any sort of issue . You set the analog input in your code , then you tell the user which pin to use that corresponds to it . If you use different boards, then the “Mapping “ will be different , because the boards are er different . If you want a user to select which pin he want to use , read it in and then select the appropriate input pin in the software .

Please don’t be aggressive with your responses, no one gets paid for this advice .

I want to report back to the user what pin they DID use.

You could write a help manual into the code, too. Great way to spend your time!

YES! YES! YES!

NOW THAT IS WHAT I AM TALKING ABOUT!!! Thanks very much Juraj. I am trying to teach a small group of kids the wonders of Arduino. They are having a blast with this, but my programming expertise is very limited. I wrote ALL of that reconcileAnalog() stuff and all I had to do was a few lines. I felt that was a good possibility, but couldn't figure it out.

Thanks again!

(Proves that you just have to keep hammering the issue until someone who belongs here helping others with SOLUTIONS comes along. You know, someone who GETS why people come here? My faith in humanity has been restored.)

I don’t really see the point of the code, but I’d test the pin number for a range (">=X && <= Y"), subtract X and print an ‘A’ in front of it.

Juraj:     Serial.print('A');     Serial.println(pin - (NUM_DIGITAL_PINS - NUM_ANALOG_INPUTS));

only for 8-bit Atmegas!

So, if the use used pin 9, on a Uno, this would print "A-5". Now, the poor user is going to be REALLY confused.

Deve - is it intended that this code run on more than one type of Arduino board ?

I needs to work on Uno, Nano and Mega 2560.

I've deleted a couple of posts here - they were adding nothing to the discussion

Keep it civil, everyone

How is the code going to get onto the Arduino ? Will it be compiled and uploaded by the 8 year old trying to learn this stuff ?