Mblock5 Arduino Uno Block Parameter bug


#1

There is a bug in mblock5.

If you:

  • use an Arduino Uno device (and I suspect all other arduino devices)
  • create any block/function
  • add a parameter to control the PIN selection called “param”
  • set the digital pin “param” in the block to HIGH/1
  • use the block in your program with a parameter of 8 for example
  • click Upload

mblock5 fails to initialize the pin as it simply scans code for “set digital pin blocks” and then tries to init the pin with that “parameter” so ends up doing a line like:

void setup()
{
pinMode(param,OUTPUT);
}

This is an error as the variable “param” does not exist outside of the defined function/block

See attached screenshot.

Instead it should store a table of all digital pins it has initialized at runtime and init those that are not already initialized on first use. Something like the proposed fix to the code generation:

//--------------------------------------------
char aDigiPinOutInit[64];
void digiPinInit()
{
memset(aDigiPinOutInit,0,64);
}
void digiPinWrite(int pin,int val)
{
if(aDigiPinOutInit[pin]==0)
{
aDigiPinOutInit[pin]=1;
pinMode(pin,OUTPUT);
}
digitalWrite(pin,val);
}

void setup()
{
digiPinInit();
}

void function_N(int param)
{
digiPinWrite(param,1);
}

void loop()
{
function_N(8);
}
//--------------------------------------------

I am happy to fix this if it is possible to have github access to the source code if this is a collaborative/community owned part of the source code. I need this fix for a kids class I am running on Friday(!)

Our current fix is to ALSO create a parameter called “param” globally and ALSO trigger digital pinwrites outside of the function for each pin in the setup so that the code generates the necessary pinMode lines for all our digital outs. But this leads to a mess of blocks that are confusing to beginners!


#2

Unzip the attached corrected index./js for Arduino Uno into this folder:
C:\Users\USERNAME\mblock\exts\arduino_uno

index.zip (34.9 KB)

And ti fixes the issue of using digital/analog read/write pins in blocks with variables for the PIN number, and also fixes servos with variables for the servo pin number.