Hi
Is there anyone who could advise if there is an official alternative method for addressing analog pins on the Arduino?
The pins are not used as analog but as digital pins.
I need to pass the pin number in a variable and A0 to A5 will not resolve without re-defining them.
On the mega328 Arduinos, A0-A5 are pins 14-19.
Thank you very much!
Sorry - just to confirm this would apply to the Leonardo as well?
I find that surprising. What exactly are you trying to do and exactly what type of variable is the function expecting to be passed ? You are not by any chance trying to pass "A0" or 'A0' are you ?
According to this pinout, yes.
Post the code that is giving you trouble. I agree with @UKHeliBob that something is not right. Something like digitalWrite(A0, LOW);
should work just fine.
Hi Thanks for the info and the drawing.
I am calling a common debounce function by passing the pin number.
And yes, the normal description of A0 would not work.
The normal digitalWrite and pinMode do work with the naming convention of A0 to A5.
Please provide a detailed description of what you mean by "would not work".
Post the code. Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
Sorry obviously not being clear.
I defined the pins like this:
#define WatchdogLed 13 //A1
#define PttInput 5
#define SupPttInput 18
#define SupervisorLed 7
and then used them like this:
pinMode(PttInput,INPUT_PULLUP);
pinMode(SupPttInput,INPUT_PULLUP);
pinMode(Footswitch,INPUT_PULLUP);
pinMode(Hook,INPUT_PULLUP);
and pass them to:
uint16_t CheckInputState(uint8_t input,uint8_t debouncectr)
and realized there would be an issue with the analog pins if I named them as A0 - A5.
So changing the definitions to 14 -19, the problem would be solved.
#define WatchdogLed 13 //A1
Which is correct, the code or the comment ?
uint16_t CheckInputState(uint8_t input,uint8_t debouncectr)
What do you expect the problem to be as A0 to A5 are #defined as 14 to 19 as has been pointed out. Did you actually try it ?
Please post the full code of the CheckInputState() function
No, A0 through A5 on the Arduino Leonardo are NOT pins 14-19:
#define PIN_A0 (18)
#define PIN_A1 (19)
#define PIN_A2 (20)
#define PIN_A3 (21)
#define PIN_A4 (22)
#define PIN_A5 (23)
#define PIN_A6 (24)
#define PIN_A7 (25)
#define PIN_A8 (26)
#define PIN_A9 (27)
#define PIN_A10 (28)
#define PIN_A11 (29)
My apologies for the tardy reply - had to take the wife shopping
Sorry - did not remove the comment on the watchdog led - the board default is 13 and I was intending to change that to A1.
This is just a temporary commenting out of the definition to "A1" to use the onboard led for testing.
The code for the debounce function does work - I just expected a problem where it was defined as "A1" to "A5" as this obviously is not going to resolve to a number.
Suspecting that there was another method to refer to the analog pins - I asked the forum.
Thank you everyone for your prompt answers.
@johnwasser - the diagram posted by groundFungus - shows the pins as 14 -19 - so is the drawing incorrect?
As a matter of interest the code for reading the inputs and debouncing follows:
//---------------------------------------------------------------------------------------
// Function to Check Inputs - called from main loop
//---------------------------------------------------------------------------------------
void CheckInputs()
{
uint16_t tmp;
uint8_t state;
if(DebounceTick)
{
DebounceTick = 0;
NewInputStates = LastInputStates;
tmp = CheckInputState(Hook,HookCtr);
HookCtr = (uint8_t)tmp;
tmp = (tmp & 0xff00) >>8;
state = (uint8_t)tmp;
if(state !=0xff) ServiceHook(state);
tmp = CheckInputState(PttInput,PttCtr);
PttCtr = (uint8_t)tmp;
tmp = (tmp & 0xff00) >>8;
state = (uint8_t)tmp;
if(state !=0xff) ServicePttInput(state);
tmp = CheckInputState(SupPttInput,SupPttCtr);
SupPttCtr = (uint8_t)tmp;
tmp = (tmp & 0xff00) >>8;
state = (uint8_t)tmp;
if(state !=0xff) ServiceSupPttInput(state);
tmp = CheckInputState(Footswitch,FootCtr);
FootCtr = (uint8_t)tmp;
tmp = (tmp & 0xff00) >>8;
state = (uint8_t)tmp;
if(state !=0xff) ServiceFootswitch(state);
LastInputStates = NewInputStates;
}
}
//---------------------------------------------------------------------------------------
// Function to scan and debounce inputs
//---------------------------------------------------------------------------------------
uint16_t CheckInputState(uint8_t input,uint8_t debouncectr)
{
uint8_t state,edge;
state = digitalRead(input); // Read the current state of the input
edge = 0xff;
if(state) // If input is high (inactive)
{
if(debouncectr) // Check if the cnt is already zero (mask flags)
{
debouncectr--;
if(debouncectr == 0)
{
edge = 0;
}
}
}
else
{ // Input Low - Active
if(debouncectr < MaxDebounceCnt)
{
debouncectr++;
if(debouncectr == MaxDebounceCnt)
{
edge = 1;
}
}
}
if(edge == 0) NewInputStates = NewInputStates & ~(1<<input);
else if(edge == 1) NewInputStates = NewInputStates | 1<<input;
return debouncectr | edge <<8;
}
Still learning - sorry should have formatted
"A1" to "A5" may not resolve to a number, or at least not one that you expect, but A1 to A5 will, as has been explained
Perhaps a peek at the code that defines the AN
pins will make things clear:
https://github.com/arduino/ArduinoCore-avr/blob/1.8.3/variants/leonardo/pins_arduino.h#L123-L147
#define PIN_A0 (18)
#define PIN_A1 (19)
#define PIN_A2 (20)
#define PIN_A3 (21)
#define PIN_A4 (22)
#define PIN_A5 (23)
#define PIN_A6 (24)
#define PIN_A7 (25)
#define PIN_A8 (26)
#define PIN_A9 (27)
#define PIN_A10 (28)
#define PIN_A11 (29)
static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2;
static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5;
static const uint8_t A6 = PIN_A6; // D4
static const uint8_t A7 = PIN_A7; // D6
static const uint8_t A8 = PIN_A8; // D8
static const uint8_t A9 = PIN_A9; // D9
static const uint8_t A10 = PIN_A10; // D10
static const uint8_t A11 = PIN_A11; // D12
So these are just variable names.
My advice is to always use the AN
pin names even when you are not using the ADC capability of the pin. The reason is because this is how they are marked on the board, so you don't need to spend any time thinking of which integer pin number happens to correspond to that pin.
Another reason is that the integer pin numbers corresponding to A0-A5 won't map the way you expect on a non-AVR architecture.
It doesn't match the numbers in the pins_arduino.h file for hardware/avr/1.8.3/variant/leonardo so I think it is wrong. It should be easy enough to run a sketch to print out the values of A0 through A11.
As is often the the case topic has wandered away from the original subject
I can see no reason why using the #defined names of the A* pins should not work just as well as the integers that they represent and I have seen no evidence to the contrary
Thank you all for the help.
You are correct in that the compiler would resolve the designations A0 to A5 into the actual pins.
There was something else wrong with the particular input pin A4 that I used, and immediately jumped to the conclusion that it was the "A" reference.
As suggested by UKhelibob I tested as follows:
void TestResultOfAPins()
{
PrintPinVal(A0);
PrintPinVal(A1);
PrintPinVal(A2);
PrintPinVal(A3);
PrintPinVal(A4);
PrintPinVal(A5);
}
void PrintPinVal(uint8_t pin)
{
Serial.print("Hex: "); PrtHex(pin); Serial.print(" Dec: "); Serial.print(pin); PrtChr(Cr);
}
The result:
Hex: 12 Dec: 18
Hex: 13 Dec: 19
Hex: 14 Dec: 20
Hex: 15 Dec: 21
Hex: 16 Dec: 22
Hex: 17 Dec: 23
The above results do not agree with the picture for the Leonardo which indicates A0 to A5 is 14 to 19 and in fact the above shows that the pins are 18 to 23 (as indicated by johnwasser).
Testing the inputs proved that 18 to 23 is the correct numbering as well.
Of course I am just testing the range of analog pins as officially indicated on the Leonardo board.
Thanks again