firmata / arduino

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

Setting defined output states after upload #306

Open andizota opened 7 years ago

andizota commented 7 years ago

Problem: After loading StandardFirmata into the Arduino, the GPIOs are in undefined state, destroying turnouts of my model railway. I despartely need a quick & dirty solution: After loading Firmata, all pins must be on digital.Output and LOW.

Can somebody please tell me which lines of code do I need to insert and where?

thank you!

soundanalogous commented 7 years ago

Which Arduino board are you using?

StandardFirmata sets all pins to OUTPUT and LOW on startup.

systemResetCallback() is called at the end of the setup() function. Within systemResetCallback all the non-analog pins are set to digital output. When setPinModeCallback sets a pin to OUTPUT, that pin value is set to LOW.

However, if you are using an ATMega-based board, Arduino defaults all pins to INPUT mode so on power up, all pins will switch from INPUT to OUTPUT and this may be causing the issue you are experiencing. Or if you are using an non ATMega-based board it could be that the GPIO pins are actually in an undefined state. Firmata defines a state for them, but this very short delay on initial boot may be enough to cause an issue.

gkzsolt commented 7 years ago

Yes, StandardFirmata sets all pins to OUTPUT and LOW on startup, what came as a shock for me, because my circuit has also pins connected as INPUT to other controllers. When I changed my sketch to StandardFirmata, some of them got short-circuited by colliding with HIGH logic level from the other part, so it was a miracle that the circuits did not get damaged. All pins should default to INPUT mode on startup, maybe with pull-ups connected. This is the only mode which is safe in every hardware situation. Who's idea was to set all pins to OUTPUT mode?

(Btw, is there a forum for discussion, or I should open an issue for any question I have)

gkzsolt commented 7 years ago

andreiasz, I use the following to set up pin modes and values: #define PIN_RELAY_4 17 setPinModeCallback( PIN_RELAY_4, OUTPUT ); setPinValueCallback( PIN_RELAY_4, HIGH ); for example to set pin 17 as OUTPUT and to HIGH. (You said you need OUTPUT LOW, which really is what StandardFirmata currently provides...) I don't know if this is correct or there is a simpler way. The Firmata class also has setPinMode and setPinState functions, but I'm afraid they are not enough for StandardFirmata to function correctly (?)

soundanalogous commented 7 years ago

Please see my proposed solutions here: https://github.com/firmata/arduino/issues/308.