arduino / arduino/Arduino

Request to add a check on the parameter of the function analogRead()

Open
#4,498 1 comment 0 reactions 0 assignees View on GitHub
Component: Core feature request
Dominant language
Java
Stars
14.6k
Forks
7k
PR merge metrics
No merged PRs in 30d

Description

The compiler doesn't throw any error if you try to compile a sketch where an incorrect parameter _pin_ is used in the function **analogRead(pin)**.
For example, the following codes in a sketch don't produces complier errors:

int vin = analogRead(-5);
int vin = analogRead(256);
int vin = analogRead(0.33);
int vin = analogRead(100000000000000000000000000000000000);

The pin parameter must be an _unsigned int_ >= 0 and < of _maximum number of the single-ended analog inputs_ allowed by the specific chip mounted on the Arduino board. This value depends on the microprocessor and, for example, corresponds to:
- 8 for AVR_ATmega328 (6 in the popular PDIP package, but there are 2 additional pins if considering the package 32-pin TQFP - QFN/MLF)
- 12 for AVR_ATmega32U4
- 16 for AVR_ATmega1280 and AVR_ATmega2560

It would be required to modify the library _wiring_analog.c_ to include the check on the pin parameter.

If the pin parameter is out of the range, then a possible compiler error message could be:
_The argument of the analogRead function is out of the definition range_
If the pin parameter is not unsigned int, then a possible compiler error message could be: _Incompatible type for the argument of the analogRead function_

Contributor guide

Open the contributing guide

Research direction

Start with wiring_analog.c and the analogRead(pin) entry point, then inspect how sketches are compiled for the listed AVR families. Determine how type and architecture-specific range diagnostics should be represented. Done means the invalid analogRead examples are rejected while valid pins for each supported chip remain accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp
Domain
compilers, embedded-iot
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.