Makeblock-official / mBlock

base on scratch offline v2.0 by MIT, Learn more from Makeblock official website
http://www.makeblock.com
GNU General Public License v2.0
325 stars 232 forks source link

Not able to set pinMode for digitalread #31

Closed petervannes closed 7 years ago

petervannes commented 8 years ago

The default pinmode for digital input of arduino ports seems to be INPUT instead of INPUT_PULLUP. This leaves the input port in a floating state, requiring to add an pull-up or pull-down resistor to the project. By enabling the available internal pull-up resistor the need for adding resistors to a project can be prevented.

Also tried to change the baseboard_firmware.ino by changing

 case  DIGITAL:{
   pinMode(pin,INPUT);
   sendFloat(digitalRead(pin));
 }
 break;

into

 case  DIGITAL:{
   pinMode(pin,INPUT_PULLUP);
   sendFloat(digitalRead(pin));
 }
 break;

and in the Arduino.s2e

    ["B", "read digital pin %n","getDigital","9",
    {"encode":"{d0}","setup":"pinMode({0},INPUT);\n","inc":"","def":"","work":"digitalRead({0})","loop":""}],

to

    ["B", "read digital pin %n","getDigital","9",
    {"encode":"{d0}","setup":"pinMode({0},INPUT_PULLUP);\n","inc":"","def":"","work":"digitalRead({0})","loop":""}],

When running an application using a digital read with "When . Clicked" using the mBlock firmware, the pull-up resistor is not enabled and the voltage on the input port is still floating. If ran as an Arduino program the pinmode is set to INPUT_PULLUP and reading the input on the pin gives a stable signal.

void setup(){ pinMode(5,INPUT_PULLUP); digitalWrite(5,1); }