sandeepmistry/arduino-nRF5

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

Open

#133 aperta il 3 apr 2017

Vedi su GitHub
 (7 commenti) (0 reazioni) (0 assegnatari)C (288 fork)github user discovery
help wanted

Metriche repository

Star
 (952 star)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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.

Guida contributor