So I’m running out of digital inputs and decide to use the analogs. I have a brain cramp and start using them from the end of the board inward (shorter jumpers to my breadboard) and start at A6 working down but only need to borrow 4 analog pins.
The resulting sketch compiles fine but A6 always returns zero. I put a logic analyzer on and the breadboard is behaving correctly, driving A6 to zero and one as designed.
To everybody but me the problem is obvious - the Uno only has 6 analog pins, known as A0 to A5. To me the question is, why does the IDE even define A6 (and A7) IF THEY DON"T EXIST? Why allow this coding error to compile? I can’t use A8 unless I compile for a board that has 16 analog pins but I can use A6 on the classic Arduino Uno.
Turning on the verbose option, I dig down and discover that avr/variants/standard/pins_arduino.h contains the following lines:
#define NUM_DIGITAL_PINS 20
#define NUM_ANALOG_INPUTS 6
#define analogInputToDigitalPin(p) ((p < 6) ? (p) + 14 : -1)
#define PIN_A0 (14)
#define PIN_A1 (15)
#define PIN_A2 (16)
#define PIN_A3 (17)
#define PIN_A4 (18)
#define PIN_A5 (19)
#define PIN_A6 (20)
#define PIN_A7 (21)
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;
static const uint8_t A7 = PIN_A7;
Why explicitly #define the number of analog pins as 6, then #define PIN_A6 (and 7) as well as the associated static constants, all in the same header file? Other device-specific pins_arduino.h are internally consistent in this respect, just not the Uno.