Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: analogInputToDigitalPin() on Teensy 4.1

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Location
    San Diego
    Posts
    1

    analogInputToDigitalPin() on Teensy 4.1

    The analogInputToDigitalPin macro for the Teensy 4.1 in teensy4\pins_arduino.h seems to be broken. It does not properly handle A10-A17.

    #elif defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41)
    #define analogInputToDigitalPin(p) (((p) <= 9) ? (p) + 14 : (( ((p) >= 14 && (p) <= 27)) || ((p) >= 38 && (p) <= 41) ? (p) : -1))

    can be replaced with:

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

    Code:
    #define newanalogInputToDigitalPin(p) (((p) <= 13) ? (p) + 14 : (( ((p) >= 14 && (p) <= 17)) ? (p) + 24 : -1))
    
    void setup() {
      
      Serial.begin(115200);
      delay(300);
      
      //  Test analogInputToDigitalPin macro
      for (int i = 0; i < NUM_ANALOG_INPUTS; i++)
        Serial.printf("analogInputToDigitalPin(%d)=%d\n", i, analogInputToDigitalPin(i));
    
      for (int i = 0; i < NUM_ANALOG_INPUTS; i++)
        Serial.printf("newanalogInputToDigitalPin(%d)=%d\n", i, newanalogInputToDigitalPin(i));
    }
    Generates the following output:
    analogInputToDigitalPin(0)=14
    analogInputToDigitalPin(1)=15
    analogInputToDigitalPin(2)=16
    analogInputToDigitalPin(3)=17
    analogInputToDigitalPin(4)=18
    analogInputToDigitalPin(5)=19
    analogInputToDigitalPin(6)=20
    analogInputToDigitalPin(7)=21
    analogInputToDigitalPin(8)=22
    analogInputToDigitalPin(9)=23
    analogInputToDigitalPin(10)=-1
    analogInputToDigitalPin(11)=-1
    analogInputToDigitalPin(12)=-1
    analogInputToDigitalPin(13)=-1
    analogInputToDigitalPin(14)=14
    analogInputToDigitalPin(15)=15
    analogInputToDigitalPin(16)=16
    analogInputToDigitalPin(17)=17
    newanalogInputToDigitalPin(0)=14
    newanalogInputToDigitalPin(1)=15
    newanalogInputToDigitalPin(2)=16
    newanalogInputToDigitalPin(3)=17
    newanalogInputToDigitalPin(4)=18
    newanalogInputToDigitalPin(5)=19
    newanalogInputToDigitalPin(6)=20
    newanalogInputToDigitalPin(7)=21
    newanalogInputToDigitalPin(8)=22
    newanalogInputToDigitalPin(9)=23
    newanalogInputToDigitalPin(10)=24
    newanalogInputToDigitalPin(11)=25
    newanalogInputToDigitalPin(12)=26
    newanalogInputToDigitalPin(13)=27
    newanalogInputToDigitalPin(14)=38
    newanalogInputToDigitalPin(15)=39
    newanalogInputToDigitalPin(16)=40
    newanalogInputToDigitalPin(17)=41

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,217
    I could be wrong here, but I think this Macro is setup to handle the funky stuff on how analogRead works...

    That is if you do something like: analogRead(0); It will read pin 14... This is true for 0-9 to match back to early Arduino boards.
    But above 9 you pass in the actual pin number. And analogRead(17) reads pin 17 not A17.

    If you wish to use A17 you pass it in to analogRead(A17).

    Where A17 is defined in the sources as:
    Code:
    #ifdef ARDUINO_TEENSY41
    #define PIN_A14 (38)
    #define PIN_A15 (39)
    #define PIN_A16 (40)
    #define PIN_A17 (41)
    const static uint8_t A14 = PIN_A14;
    const static uint8_t A15 = PIN_A15;
    const static uint8_t A16 = PIN_A16;
    const static uint8_t A17 = PIN_A17;
    #endif

Posting Permissions

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