Reconcile Analog

I start off with the usual Serial commands to show the versatility. They will be involved in adding this code when they see that it needs it (as the lessons progress). It’s a learning moment. How to get past the obstacle. I wasn’t happy with how long my code was! It needs to be as compact as possible yet accurate. Turns out, the 9 year old that I started a year ago is doing about as good as I am with coding, so it’s not asking too much. They eat this stuff up!

It was kind of surprising they left this out because this is the greatest platform ever for this stuff. Hopefully the crew that upgrades the IDE can add this capability in for the Analog Pins the way they did for Digital Pins.

I even thought of putting it in a library format. #include would make it so if you wanted it, fine, if you thought it was a colossal waste of time, fine too. Not only do I not know how to do that, but I don’t think my existing solution (post 1) is good enough. Probably slows things down.

Nope. Still not getting the need for this one.

So you don't get that a person who does a Serial.println (analogPin1) is confused when it returns 54? Okay...

What is my analog pin 1 set to? ummm why is it telling me its pin 54? Mine doesn't say 54, it says 14! Wait! They are the same only one is an Uno and the other is a Mega. I find it hard to believe we are having this conversation since all the Digital pins seem to know who they are. Why not the Analog side of the board too? But not to cry over spilled milk, I coded the solution in the first post. Its not good enough for prime time, but its more than what we had. Whenever I come here for help, the volunteers here like it when you TRY first. Check!

But then, you also have a new issue that seems to crop up for some reason that goes right over my head... The poor guy looking to fix things has to first justify why something that is clearly broke needs fixing! If you don't see it, why are you posting? How is that helping people who search for this very issue later and have to peruse 20 or more replies before they finally get an answer if they EVER get one? Help me understand why its so important to post when you don't care about the issue? I do not mean to be mean, I am just finding more obstacles and trying to learn how to process them.

It just seems to me that this is a non-issue; if it really were an issue, I think it would have been raised and solved before now.

(

What is my analog pin 1 set to? ummm why is it telling me its pin 54? Mine doesn't say 54, it says 14!

and if analogue pin 1 reports back as 14 or 54, then I think you've got much bigger problems ;) )

Dude.. try it. Yours will report those numbers too.

Sketch this:

int analogPin1 = A0; bool runOnce = true;

void setup() { Serial.begin (115200); }

void loop() { if (runOnce == true) { Serial.print ("This is controlled with the Potentiometer at Pin "); Serial.println (analogPin1); runOnce = false; } }

I mean REALLY? I thought I was careful in Post Number ONE to clearly and concisely explain this very real problem that I am also kind of surprised nobody else has said anything about.

OK, so what happens when I use digital pin 14 on my Uno? What does that report as?

Does A4 show as "A4", digital pin 18, or "SDA"? Does pin 11 show as MOSI?

You're surely not going to tell the kids that they can only use pins 14 to 19 as analogue inputs? Won't that be limiting for them?

Thank you for making my point. All the other pins on the board are clearly labeled and their labels clearly reflect what they are. Digital OR Analog. Once they understand there really isn't that clearly defined line between them, much later, they can move on. Right now, A0 isn't A0. Its pin 14, or 54 or whatever board you have. To answer your question..

There IS NO Pin 14 on an Uno!. There is Digital Pins 1-13 and Analog Pins A0-A5. And, to be consistent in the way they are reported back to the user is just a good idea. This is a platform that is supposed to welcome beginners. The powers that be can decide how they want things reported back, just be consistent about it, and label the board accordingly.

So, to cut down on the confusion, why not write some code that clears that up until the IDE programmers fix this apparent oversight?

There IS NO Pin 14 on an Uno!. There is Digital Pins 1-13 and Analog Pins A0-A5.

There is a digital pin 0, too.

By your logic, when the user chooses to use pin 6, the Arduino should tell him/her that is D6. Nonsense. The analog pins can be used as digital pins. That is clearly documented. The next pin after 13 shares the same physical space as analog pin 0. Why should there be any mystery about the fact that pin is numbered 14?

I don't get what the problem is either.

Just explain that A0 to A15 can be analog inputs, or digital pins like D0 to D53.

Paul, because it's not labeled pin 14. It's labeled A0. The Arduino also knows those pins as A0-A5. Then to top it off, without initiating the variable, run the same sketch above. but this time have the serial port (user input) write the variable. if you do that, what is reported back is 0-5. I will have a sketch for you to try tomorrow. its late here.

I am not trying to be a pain or anything guys, but this isn't right the way it's reporting. Does it REALLY REALLY matter? NO. I can teach the kids however it is, but when you are trying to teach consistency, accuracy and all those things that are hard for kids to completely grasp, this doesn't help. It reports pin 14 one way you do it, then it reports pin 0 the other way. Yet with digital pins, they report perfectly. Why not come here on this forum and ask for help making a bit of code that makes everything consistent?

If you’re trying to return the status (state) of a pin, that is simpler even:

for (x=0; x<54; x=x+1){
Serial.print ("D");
Serial.print (x);
Serial print (" is ");
Serial.println (digitalRead(x));
}
for (x=0; x<16; x=x+1){
Serial.print ("A");
Serial.print (x);
Serial print (" is ");
Serial.print (analogRead(x));
Serial.print ("( may be used as D)");
Serial;println (x+54);
}

Digital pins will return 0 or 1.
Analog pins will read close to 0 and 1023 when connected to digital things, and 0 to 1023 when connected to analog things.
Any user can look at the results and determine if he has an analog or digital input. If digital, then code to use digitalRead instead of analogRead.

I can teach the kids

Well, good, because that is what you need to do.

Make a large drawing of an Arduino. Label each of the pins with it's number as a digital pin. Label each pin that also serves as an analog pin with it's number. Analog pin 0's number as an analog pin is 0, NOT A0.

A0 is an alias for it's number as a digital pin. Those aliases were introduced ONLY as a way of writing code that could be used on a Uno and a Mega without having to make changes, as long as one understood that the first analog pin had an alias that allowed it to be used as a digital pin, and that the compiler (the preprocessor, actually, would take care of substituting the proper NUMBER in place of the name.

Add labels for all the special uses that some pins have - PWM, SPI, I2C, etc.

You'd be doing the kids a much bigger favor than arguing that printing A0 should result in "A0", not 14.

Its not about that. It's about it reporting all over the place instead of being consistent. If the Gods decided that A0 and Pin 14 are the same, fine.. but WAIT.. it is NOT fine because what happens when I go from an Uno to a Mega and now pin 14 is not what was intended, pin 54 was! I can't understand why you aren't seeing a problem here. A0 on a mega is 54. A0 on an uno is 14. Yet, A0 IS A0 no matter what board you have. Now with my new revelation, 0, A0, Pin 14 are the same on an Uno. 0, A0 and 54 are the same on a Mega. They all share that A0/A0 and 0/0 are the same. Its not hard to see a situation where someone gets into their head to call an analog pin 14-19. Then, when someone with a Mega tries it, they cant use Pins 14-19 like they thought they could.

What am I missing here? I am not one to argue an unarguable issue. Tell me where I am going wrong?

What's wrong with the solution in reply #3? Seems a simple little function to add.

Or were you too busy telling all these nice folks off to notice it?

If you read, you will notice I asked him HOW to implement it, but never heard back. It doesn't seem straightforward to me with my novice level of programming expertise. I was hoping for clarification.

Guys, if you READ, I was the one having my intelligence insulted in the first place. I just don't approve of being treated that way like others might. Please keep the conversation on point so there are no further misunderstandings.

Well, like anything that is engineering, there are multiple ways to get there. There’s no one perfect solution. You just gotta deal with it. I think the first assumption to make is that the programmer knows what microcontroller is being used and can code the very basic stuff and wire the project up appropriately. Say your project was accessing a slave device via I2C. And something happened and the decision was made to move from an Uno to a Mega, perhaps more than 4 analog inputs were needed. Much of the wiring could move from pin to pin directly, but the I2C pins are in a different location, so one might have to move the I2C wires from A4/A5 to D20/D21. The code may not have to change, but the wiring does.

Yes. I agree. That is why I was hoping for a coding solution for this. The idea is to announce to the user the actual pin they are using so there is no doubt they know what pins are being used. Its a calibration program for setting Servo's so there are no overcurrent problems. In calibration mode, I am telling them the Servo Pins and the Analog Pins for the controlling pots the servos are using. For new students, this is a double check for them to make sure their wiring is correct and also that they know for sure the variable declarations are correct. Once the servos are calibrated, and the variables are set, later we will be adding more analog items and want to keep track of them. We also want to use Uno's Nano's or Megas depending on how involved, how small, or how functional something needs to be.

I ran across this problem in doing that. I thought others might run into it so hoped someone had found this by now. Afterall, how many years has it been? So, I was surprised to find out there IS room to improve things. My hope is someone sees enough need to help solve this issue. There will always be changes that need to be made to wiring and even the software, but those are the cherished challenges many of us enjoy. Take this problem for example, who is having more fun than me right now? ;-/

I promised this last evening. Hopefully it will clarify the issue. Pin 14 on a Mega is an actual Digital Pin and is not the same as Pin 14 on the Uno (which is an Analog Pin). Not having the board internals telling the pins their names for Analog, but it working for Digital is a pretty obvious shortcoming. My hope in bringing this up is that it can be put in the Queue for the next IDE update.

int analogPin1 = A0;
bool runOnce = true;

void setup() {
  Serial.begin (115200);
}

void loop() {
  if (runOnce == true) {
  Serial.println("This is with the Analog Pin variable set to A0:");
  Serial.print ("This is controlled with the Potentiometer at Pin ");
  Serial.println (analogPin1);
  runOnce = false;
  }
  runOnce = true;
  Serial.println("\nThis is using the Serial Monitor to user input the Pin");
  Serial.print ("Enter desired Analog Pin for Controlling the Pan Servo:");
  while(Serial.available()==0) {}
  analogPin1 = Serial.parseInt();      
  Serial.print (" You chose Analog Pin ");
  Serial.print (analogPin1);
  Serial.println (".\n");
  runOnce = false;
}

It is simple enough to add a short function to do that in your code. I doubt they're going to add to the size of the core for something like that. Most of the rest of us would rather keep the core as small as possible to leave room for the things we want to do in our code. So far you seem to be the only one that this bothers.

Which is why I came here, succinctly explained in detail the problem and asked for a code solution. We have now come full circle. So, does anyone have any ideas on how to make THIS shorter and more streamlined? A solution that a novice programmer could understand and put inside existing code as a function (like below). Thank you!

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");
  }
}