String variables in mBlock app?


How can I create a string variable in mBlock (3.3.7)? There doesn’t seem to be an option to specify the type in the “new variable” window, so if I create a variable named “message” it gets declared as:

double message;

…and of course, any attempt to assign a string value to this variable fails to compile. I can certainly go in and manually edit the generated code, but I’d like my kids to be able to do this sort of thing in the scratch UI.


While Scratch supports strings, the mBlock code generator does not. To use strings, you will need to run the program through the WiFi/Bluetooth interface. That is true of all versions from 2.x on (possibly earlier, but that’s when I started working with mBlock).


Thanks. I’m sad to hear about the lack of string support in the code generator. I’m not I follow the second part of your response, though. You said, “To use strings, you will need to run the program through the WiFi/Bluetooth interface”. I may be confused, but I thought that the bluetooth link simply manifested itself as another serial port, and would therefore be using the same code generator and have the same problem.


Ah, let me explain a bit more in-depth about how mBlock and the mBot work together. If you start your program with a ‘when [green flag] clicked’ block, you can run the program from within mBlock on your computer. mBlock will send codes over the Bluetooth or WiFi link to the mBot’s firmata (loaded when you do an Upgrade Firmware from the Connect menu). The mBlock and the mBot will communicate over this serial link to exchange information. However, the program is not uploaded to the mBot.

If you start your program with an ‘mBot Program’ block, you can invoke the Upload to mBot mode. This will cause the code generator to translate the Scratch blocks into an Arduino program (in C), then call the Arduino compiler / loader to compiler and load the program to the mBot. This approach allows the mBot to run in a stand-alone mode without being connect physically or wirelessly to the mBot. However, the code generator only generates numerical variables at this time.

This is really not that surprising given the relatively limited amount of available variable memory (SRAM) on the mBot. While there is 2K of SRAM available, the Makeblock library eats about half of that space, so you are really left with about 1K of SRAM. SRAM shortage is one of the common causes of program failure with the ATMega328P microcontroller (the basis of the Arduino Uno and the mBot) and strings can consume a great deal of memory, so that is likely why string support is lacking. You can always use strings if you are programming from the Arduino IDE, but even so, caution is advised.

I hope that helps.



Chuck, that clears things up. Thank you for the detailed reply!


Here is a kludge method to create string variables:
Use the block function and then under options add string input.
Your main program can pass a string to the block, and then you can use the string within the block.


I also faced the problem of lack of strings when writing interactive applications. After that I built the “strings” extension that adds strings variables and functions to work with them. But as written above, use the strings to gently because they take a lot of memory.