sandeepmistry/arduino-nRF5

analogRead() should be able to map inputs 0-to-x as A0-to-Ax

Open

#133 opened on Apr 3, 2017

View on GitHub
 (7 comments) (0 reactions) (0 assignees)C (288 forks)github user discovery
help wanted

Repository metrics

Stars
 (952 stars)
PR merge metrics
 (PR metrics pending)

Description

The default Arduino analogRead() can take 2 different values for each ADC input:

  1. The "analog pin" value, which goes from 0 to NUM_ANALOG_INPUTS, which in most Arduino Uno compatible boards would be 0 to 5:
    analogRead(0);
    analogRead(1);
    ...
    analogRead(5);
    
  2. The pin number, in it's raw "arduino pin" form or Ax global variable:
    analogRead(A0);
    analogRead(A1);
    ...
    analogRead(A5);
    
    Which in boards like the Uno would be the equivalent of:
    analogRead(14);
    analogRead(15);
    ...
    analogRead(19);
    

This is usually done by simply checking the range of the input argument, so in the case of the Uno the analogRead() function would have something along the lines of:

analogRead(uint8_t pin) {
  if (pin < A0) {
    pin += A0;
  }
}

That works well because these boards are configured to have the analog pins mapped immediately after the digital pins and in successive order.

This Arduino core has variants with analog pin mappings that do not follow this convention, and so only "method 2" listed above has been implemented.

This breaks compatibility with any arduino sketches using "method 1", so we should work out a method to map these analog pins in the variant files in a way that allows both methods to work as expected.

Contributor guide