mBot using mBlock
Installing mBlock Software
https://www.mblock.cc/en/download/
- Install the latest version for PC (V5.4.x).
- You will need to install the driver when prompted by clicking INSTALL, as well as Allow access when you get a firewall warning.
Getting Started with mBlock Software
mBlock main page is here - https://support.makeblock.com/hc/en-us/sections/360001829013-mBlock-5.
Let us first gain some familiarity by playing with sprites before we start using mBlock for the actual device.
- https://support.makeblock.com/hc/en-us/articles/4410674227863-Make-a-Sprite-Move-on-mBlock-5
- https://support.makeblock.com/hc/en-us/articles/4410676168343-Add-Sounds-and-Costumes-on-mBlock-5
Now, it is time to play with the actual device (mBot/mCore). We will be using a USB cable (and not Bluetooth or a 2.4G module). The official tutorial is here - https://support.makeblock.com/hc/en-us/articles/1500003954802-Program-mBot-with-mBlock-5. However, you may wish to follow the slightly more detailed instructions below.
The default device after installation will be CyberPi and not mBot. You can delete the CyberPi and then click 'add'.
In the Device Library dialog, you can select mBot. Clicking the star button on the icon will cause the default to be set to mBot, so that you don't have to do it the next time.
Get familiarized with both Upload mode and Live mode - read the difference between modes here - https://support.makeblock.com/hc/en-us/articles/1500003954802-Program-mBot-with-mBlock-5#3.%20Set%20the%20programming%20mode.
Live mode
Select the mode (say, Live mode for now) and click Connect after ensuring that mBot/mCore is powered on. You will then be prompted to select the port.
Generally, mBlock will detect the correct port automatically. However, if you are not sure which port to select, in Windows, you can go to Device Manager (type 'Device Manager' in the Windows Start Menu), and check for the port number under Ports (COM & LPT). The correct port will be the one corresponding to 'USB-SERIAL CH340'.
In Live mode, you can change the behaviour very easily without re-uploading the code every time. It requires the firmware to uploaded/updated one time and then not again unless you switch to upload mode. You will be prompted to update after you click 'Connect' if the firmware is to be uploaded/updated. You can choose the Online firmware. Once the firmware is updated, you have to click Connect again.
=> =>
Now let us test the Live mode. The event block should already be there in the scripts area. If not, you can find it under Events, and drag it to script area as shown below.
Next, drag the block into the scrips area and connect it to the block.
Next, click the button. The two LEDs on mBot should turn RED for 1 second. Hurray, you have gotten started well!
Play with other blocks, and see the various inputs/outputs in action!
Interaction between Devices and Sprites
Enabling your mBot/mCore to interact with sprites is useful to make the sprite (say, Panda) read out sensor values. The official tutorial is found here - https://support.makeblock.com/hc/en-us/articles/4410698813463-Enable-a-Device-to-Interact-with-a-Sprite-on-mBlock-5. Note that here too, you can use instead of the 'when button is pressed' mentioned in that page (which is for CyberPi and not mBot). A more detailed description is given below.
Here we have an example of reading the light sensor on the Device (mBot)and making the Sprite (Panda) display the reading.
Create a new variable from the tab. Select 'For all sprites'
Create your program similar to the one shown below for the device (mBot).
Note that the broadcast block is available under . Here, we created a message with a meaningful name 'new_light_reading'.
For the sprite (Panda), create a program similar to the one below.
The variable 'light_reading' will be available for the sprite as well.
Now, click the flag and the sprite (Panda) will say the new reading when it is made available by the device (mBot), which will be once a second.
Upload mode
Note that in Upload mode, the only available is , which isn't available in Live mode.
In upload mode, you can see the corresponding Arduino C code by clicking the orange button on the right side of the window , which is pretty cool and helps you transition to Arduino C. A sample is given below.
More Examples
You can try out the various examples here using mCore:
Arduino C in mBlock
mBlock can also handle Arduino C directly. You can't switch between using blocks and using code seamlessly though. Make sure you include #include <Arduino.h>