One of the main features of the
new v0.82 version is that it allows to make text coded programs. And not just in C/C++ and Arduino-compatible syntax, but also in Python and, if the community adds more targets, in nearly any imperative/OOP language (such as JavaScript, Java, Ruby, etc.). Although in future versions this will be simpler and I'm improving the user experience as much as I can, right now it's fully functional. And it has a feature which I really like: the possibility of switching between a text and a graphical program at anytime during the development. This way, you can make fast test with the blocks, while programming the core of your application with the full power of C/C++ (or whatever language your hardware is using with miniBloq). Let's see how to create an example text coded
"echo" program for an Arduino board:
1. Open miniBloq (we will be using Arduino Mega 2560 for this example, but you can select any other Arduino-compatible board from miniBloq's hardware list):
2. Since we will create the whole code for the project, we don't want to initialize the board automatically. So we will change the first call to initBoard, for a call to the function
go():
What does this mean? It means that at the very beginning of your program, it will do nothing but calling the
go() function. And what is the
go() function? Is a function that you will write by your own, and where your program will start. It's like the
main() function in C/C++, but we can not name it
main() because that name is already used internally by the Arduino kernel (I will post more about this soon). In "Arduino terms", the
go() function is equivalent to the
setup() function.
3. Add a new
cpp file (created just as a text file). First, go to the
File->Add menu:
save the
component (miniBloq's jargon for your
program):
And inside the component's folder, create the text file:
We will name it echo.cpp but you can use any (valid) name of your preference:
You will see this message box when you change the file extension from
txt to
cpp. Of course say
Yes:
Now, you will see the echo.cpp open in the file editor in miniBloq:
You can hide the blocks editor to gain space in the screen to work with your text file:
4. Let's add some code there. You will need to include the
mbq.h file, which gives you access to the libraries for the motors, the sensors, the
Arduino API functions, etc.:
5. If you compile that, it should compile just fine, but it's an empty program. Now, please try the following piece of code, which is our final echo program:
#include <mbq.h>
void
go()
{
serial0.begin(
115200);
while(
true)
{
if (
serial0.available())
{
int inByte = serial0.
read();
serial0.print((char)inByte);
}
}
}
6. Run it and you will see how it works by using miniBloq's terminal:
Finally, if you want to use blocks to test, you just need to select
initBoard() in the Start block, and start adding your blocks there. The files that you have added will remain there (unless you close them). Once you want to return to the text coding, just select
go() again in your start block.
In a future post, I want to show you how to work with multiple file projects. But probably you can just try to experiment a bit by yourself using the
Add file feature.
Enjoy!