Forum Rule: Always post complete source code & details to reproduce any issue!

1. ## Bug in Teensyduino 1.58 for analogInputToDigitalPin()

Hello

...\hardware\teensy\avr\cores\teensy4\pins_arduino .h

defines:

#if defined(ARDUINO_TEENSY40)
#define NUM_ANALOG_INPUTS 14
#elif defined(ARDUINO_TEENSY41)
#define NUM_ANALOG_INPUTS 18

I would expect that a for() loop from 0 to 13 calling analogInputToDigitalPin() should return the following result for Teensy 4.0:

An0 = Digi 14
An1 = Digi 15
An2 = Digi 16
An3 = Digi 17
An4 = Digi 18
An5 = Digi 19
An6 = Digi 20
An7 = Digi 21
An8 = Digi 22
An9 = Digi 23
An10 = Digi 24
An11 = Digi 25
An12 = Digi 26
An13 = Digi 27

But this does not work because the following define buggy:

#define analogInputToDigitalPin(p) (((p) <= 9) ? (p) + 14 : (( ((p) >= 14 && (p) <= 27)) ? (p) : -1))

The correct define is this:

#define analogInputToDigitalPin(p) (((p) <= 13) ? (p) + 14 : -1)

And the define for Teensy 4.1 is also wrong.
It should be:

#define analogInputToDigitalPin(p) (((p) <= 13) ? (p) + 14 : (( (p) >= 14 && (p) <= 17) ? (p + 24) : -1))

2. Excerpted with renames from ...\arduino-1.8.19\hardware\teensy\avr\cores\teensy4\pins_ardu ino.h

wrote a sketch to verify changes here - seems they match updated p#1

assuming T_MM linear the same based on the 'card here.

Code:
```// #if defined(__IMXRT1062__) && defined(ARDUINO_TEENSY40)
#define T40NUM_ANALOG_INPUTS 14
#define T40analogInputToDigitalPin(p) (((p) <= 13) ? (p) + 14 :  -1)

// #elif defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41)
#define T41NUM_ANALOG_INPUTS 18
#define T41analogInputToDigitalPin(p) (((p) <= 13) ? (p) + 14 : (( ((p) >= 14 && (p) <= 17))  ? (p) + 24 : -1))

// #elif defined(__IMXRT1062__) && defined(ARDUINO_TEENSY_MICROMOD)
#define TMMNUM_ANALOG_INPUTS 14
#define TMManalogInputToDigitalPin(p) (((p) <= 13) ? (p) + 14 :  -1)

void setup() {
// put your setup code here, to run once:
while ( !Serial ) delay(1);
Serial.println("\n\t" __FILE__ " " __DATE__ " " __TIME__);
Serial.println("\n T_4.0 \n ");
for ( int ii = 0; ii < T40NUM_ANALOG_INPUTS; ii++ ) {
Serial.printf("\t AnalogPin# %d is DigPin# %d\n", ii, T40analogInputToDigitalPin(ii) );
}
Serial.println("\n T_4.1 \n ");
for ( int ii = 0; ii < T41NUM_ANALOG_INPUTS; ii++ ) {
Serial.printf("\t AnalogPin# %d is DigPin# %d\n", ii, T41analogInputToDigitalPin(ii) );
}
Serial.println("\n T_MM \n ");
for ( int ii = 0; ii < TMMNUM_ANALOG_INPUTS; ii++ ) {
Serial.printf("\t AnalogPin# %d is DigPin# %d\n", ii, TMManalogInputToDigitalPin(ii) );
}

}

void loop() {
// put your main code here, to run repeatedly:

}```
Results:
Code:
```T:\TEMP\arduino_modified_sketch_431788\sketch_sep14a.ino Sep 14 2023 20:30:15

T_4.0

AnalogPin# 0 is DigPin# 14
AnalogPin# 1 is DigPin# 15
AnalogPin# 2 is DigPin# 16
AnalogPin# 3 is DigPin# 17
AnalogPin# 4 is DigPin# 18
AnalogPin# 5 is DigPin# 19
AnalogPin# 6 is DigPin# 20
AnalogPin# 7 is DigPin# 21
AnalogPin# 8 is DigPin# 22
AnalogPin# 9 is DigPin# 23
AnalogPin# 10 is DigPin# 24
AnalogPin# 11 is DigPin# 25
AnalogPin# 12 is DigPin# 26
AnalogPin# 13 is DigPin# 27

T_4.1

AnalogPin# 0 is DigPin# 14
AnalogPin# 1 is DigPin# 15
AnalogPin# 2 is DigPin# 16
AnalogPin# 3 is DigPin# 17
AnalogPin# 4 is DigPin# 18
AnalogPin# 5 is DigPin# 19
AnalogPin# 6 is DigPin# 20
AnalogPin# 7 is DigPin# 21
AnalogPin# 8 is DigPin# 22
AnalogPin# 9 is DigPin# 23
AnalogPin# 10 is DigPin# 24
AnalogPin# 11 is DigPin# 25
AnalogPin# 12 is DigPin# 26
AnalogPin# 13 is DigPin# 27
AnalogPin# 14 is DigPin# 38
AnalogPin# 15 is DigPin# 39
AnalogPin# 16 is DigPin# 40
AnalogPin# 17 is DigPin# 41

T_MM

AnalogPin# 0 is DigPin# 14
AnalogPin# 1 is DigPin# 15
AnalogPin# 2 is DigPin# 16
AnalogPin# 3 is DigPin# 17
AnalogPin# 4 is DigPin# 18
AnalogPin# 5 is DigPin# 19
AnalogPin# 6 is DigPin# 20
AnalogPin# 7 is DigPin# 21
AnalogPin# 8 is DigPin# 22
AnalogPin# 9 is DigPin# 23
AnalogPin# 10 is DigPin# 24
AnalogPin# 11 is DigPin# 25
AnalogPin# 12 is DigPin# 26
AnalogPin# 13 is DigPin# 27```

3. I've just glanced at this, so take this reaction with a grain of salt, but:

A0 is 14. The return value should be 14. And with the original macro, it would be 14. But with the updated one, it seems -1 would be returned.

4. Hello Silverlock

Please have a look how this macro works for older Teensy and Arduino boards.
It was never made to enter A0 into this macro!
For no other board this will work.

And this would be nonsense.
Look how these are defined:

#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)
#define PIN_A8 (22)
#define PIN_A9 (23)
#define PIN_A10 (24)
#define PIN_A11 (25)
#define PIN_A12 (26)
#define PIN_A13 (27)

So what do you need a macro for if you already have the pin number?

int PinNumber = (int)PIN_A0;

Therefore you do not need any macro!

5. And with that response, I'm done.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•