firmata / arduino

Firmata firmware for Arduino
GNU Lesser General Public License v2.1
1.55k stars 518 forks source link

Inconsistent state for additional analog pins (A6 - A11) on Arduino Leonardo #486

Open oliviercoma opened 2 years ago

oliviercoma commented 2 years ago

The Arduino Leonardo has 12 Analog pins. But A6 - A11 pins (D24 - D29) are actually other pins that are being re-used (D4, D6, D8, D9, D10, and D12). it means D4 and D24 are the same physical pin.

Now, StandardFirmata (and probably others) threats those pins as separate pins while Arduino threats them as the same. So, during systemResetCallback, those physical pins are actually initialized twice in Firmata. Once as Digital output (e.g. D4) and then later as analog pin (A6 - D24). This leads to an inconsistency between Firmata and the board's state:

Firmata state (internal state, and reported to the client application): D4 : OUTPUT D24 (A6): ANALOG INPUT Arduino (Board) state: D4 - D24 - A6: ANALOG INPUT

As Firmata thinks that D4 is OUTPUT while it's actually ANALOG INPUT, a digitalWrite (from SET_DIGITAL_PIN_VALUE or DIGITAL_MESSAGE) on D4 will not do anything.

I don't know if other Arduino boards also remap existing pins or if it's specific to Leonardo. So I'm not sure what's the best way to fix this issue.

Work around: By not using pins D4, D6, D8, D9, D10, and D12, but their equivalent pin number (D24 - D29), Firmata and Arduino states are consistent and work as expected.

pgrawehr commented 2 years ago

Thanks for reporting this. Do you have an idea on how this should be fixed? Since there are seemingly two ways of addressing the same physical pin, your workaround is probably the best you can do. We could remove the pins D4, D6, D8 etc from reporting alltogether, but that would possibly break code relying on these.