mBlock Arduino Multi Function Shield

My kids like easy coding with Scratch or mBlock. I found a nice Arduino shield for them to play with, only it could only be programmed with the Arduino IDE. Luckily mBlock provided a way to extent the mBlock environment.

The Arduino Multi Function Shield is a low cost shield provided by several suppliers.
It contains a lot of basic IO for fun experiments. Typical the stuff you put normally first on a breadboard.The shield contains:

  • 3x push switches
  • 4x leds
  • 1x buzzer
  • 1x potentiometer
  • 1x 4 segments 3461 BS display 
  • And some interfaces for optional io/sensors

You can code it your self in the Arduino IDE or use an existing library like the arduino-multi-function-shield library

mBlock

mBlock  is a Scratch like IDE for making computer program, but it also supports several hardware platforms like the mBot, but also Arduino. My kids use this very accessible environment for the mBot, but is also support other hardware like the Arduino Uno.

mBlock environment web or app

mBlock Extension

To make the it able to use the Arduino shield in mBlock, a so called extension should be created for it in the mBlock online extension builder.
When a extension is finished it can be published for approval. In the will so up in the Extension store.

In the extension center you create for a certain category a number of blocks.

new block in the extension builder

Each block can be hooked to matching C code for :

  • declare
  • setup
  • code
set led block configuration

For use it needs to be added to you mBlock environment. Unless you want to hack the extension yourself, you can use the MF Shield from the extension center.

MF Shield extendsion in the extension center

MFS Blocks

Below are the new blocks created for use with the MF Shield and are available in mBlock:

Overview of the MF Shield blocks in mBlock

The MFS library use a Timer in the background to update the leds, display segments and read the switches.The MFS library has a very nice way to read the switches; it puts every event in a round robin queue.The function MFS.getButton get the first one from the queue. Using this function several times in the main loop is a bad idea.
That’s why the functionality is splitted in two;

  1. get first entry in the queue – (get button state block)
  2. eval if a certain key is pressed – (is button [x] [pressed] in [button state] block)

Read the switches

If you only have one button test you can use the get button state block directly in the eval block. If you have multiple you first need to store the button state into a variable and use that variable in the eval:

Only one switch test in your program:

Use multiple switches in your program:

Leds

Potentiometer

Display

Try-out extension

Try out the extension before it is published (before is applied to and approved by the mBlock Des):

  1. Click in the extension editor download
  2. Save it own disk
  3. Open mBlock 
  4. Open an explorer to the downloaded extension
  5. Drag and drop it onto mBlock
  6. Restart mBlock
  7. Chose add extension
  8. Add the MFS extension to your project (project device has to be an Arduino, only tested with the Uno)

If you to update the extension repeat step 1-6.

Resources:

You can find the extension on:

Other handy links:

If display segments are used with raw data (instead of display test use set display segment) you can enable each led of the segment. A-DP are the bits of the segment.

Leave a Reply

Your email address will not be published.