I'm new to Arduino and I spent two days trying to track down why outputs were sometimes randomly failing depending on the commented-out state of apparently random other things in the code.
I finally traced down the problem to this line:
pinMode(PIN_MEASURE, INPUT);
What's the problem? PIN_MEASURE was defined to A7.
Apparently if you try to set the pinMode of A6 or A7 on the Nano it responds by changing the pin modes of random other pins.
Once I finally located the problem and successfully confirmed that I wasn't going mad, it makes sense. A6 and A7 are analog-only pins, they have no "port register", and cannot be configured. So when you invoke pinMode on them, it calls digitalPinToBitMask and digitalPinToPort, which read their internal PROGMEM arrays out of bounds, retrieving undefined values from other bits of the flash memory. Multiple pins get reconfigured based on these essentially random values.
In my case, this resulted in nothing worse than two days of confusion about mysteriously failing outputs. But hypothetically, it could result in pin configurations that are electrically unsafe, damaging the Arduino or connected hardware.
I understand now that A6 and A7 are not reconfigurable, but to have pinMode respond to that by playing jiggery monkies with half the other pins on the board is downright hostile.
When pinMode is invoked on an existent but non-configurable pin it should silently do nothing, as a courtesy to fallible humans :)
Thanks!
IDE version: 1.8.13 (Linux x64).
Board: Arduino Nano.
I'm new to Arduino and I spent two days trying to track down why outputs were sometimes randomly failing depending on the commented-out state of apparently random other things in the code.
I finally traced down the problem to this line:
What's the problem? PIN_MEASURE was defined to A7.
Apparently if you try to set the pinMode of A6 or A7 on the Nano it responds by changing the pin modes of random other pins.
Once I finally located the problem and successfully confirmed that I wasn't going mad, it makes sense. A6 and A7 are analog-only pins, they have no "port register", and cannot be configured. So when you invoke pinMode on them, it calls
digitalPinToBitMask
anddigitalPinToPort
, which read their internal PROGMEM arrays out of bounds, retrieving undefined values from other bits of the flash memory. Multiple pins get reconfigured based on these essentially random values.In my case, this resulted in nothing worse than two days of confusion about mysteriously failing outputs. But hypothetically, it could result in pin configurations that are electrically unsafe, damaging the Arduino or connected hardware.
I understand now that A6 and A7 are not reconfigurable, but to have pinMode respond to that by playing jiggery monkies with half the other pins on the board is downright hostile.
When pinMode is invoked on an existent but non-configurable pin it should silently do nothing, as a courtesy to fallible humans :)
Thanks!
IDE version: 1.8.13 (Linux x64).
Board: Arduino Nano.