Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible off-by-one error in analogRead? #117

Open
diyelectromusic opened this issue Dec 16, 2021 · 0 comments
Open

Possible off-by-one error in analogRead? #117

diyelectromusic opened this issue Dec 16, 2021 · 0 comments

Comments

@diyelectromusic
Copy link

For the Nano Every, NUM_ANALOG_INPUTS is set to 14, presumably to allow for AIN12 and AIN13 to be used for A4 and A5. For the Wifi Ref 2 NUM_ANALOG_INPUTS is set to 6.

In analogRead there is a test of the pin number returned from digitalPinToAnalogInput() to check that it is within a sensible range for the hardware, but I think there may be an off-by-one error here. I think the code:
if(pin > NUM_ANALOG_INPUTS) return NOT_A_PIN;

Should be:
if(pin >= NUM_ANALOG_INPUTS) return NOT_A_PIN;

In practice this probably doesn't cause any problems unless NUM_ANALOG_INPUTS is updated in the future to 16 for the Nano (or 8 for the Uno).

As an aside, setting NUM_ANALOG_INPUTS to 14 in pins_arduino.h for the Nano Every does seem slightly misleading, as the number of supported analog inputs is still just 8, but having it mapped onto channels up to 12 and 13 seems to have forced the higher number in NUM_ANALOG_INPUTS, in which case I'm not quite sure why this isn't set to the actual number of analog channels - i.e. 16?

Kevin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant