Tutorial: How to assign a value to a reporter block in Extension Builder (upload mode only)


#1

Hi all -

I spent two weeks trying to figure out how to access the acceleration data in the Me 3-Axis Accelerometer, and one of the big stumbling blocks for me was how to assign the acceleration value that is obtained in the Arduino code to the reporter block that is used in mBlock. In fairness, the answer -is- actually on these forums, but it wasn’t at all easy to find – I figured out the method myself before I found it here! :slight_smile:

At any rate, I thought a step-by-step tutorial would be useful, so here it is. I’m using my accelerometer extension as an example.

  1. In Extension Builder, create your extension then add your source files for any libraries that you need. All files need to be in the same folder, because you can only upload entire folders not individual files In this case, I’ve added the accelerometer library that I wrote in ArduinoC (see my other topic for the code if you want it).

  1. Add a block as shown. You need to make it a STRING block. Notice that I have added a dropdown box to select the accelerometer axis. To add parameters, just type “@” in the Content box and you will get a list of parameter types you can add.

  1. If you added a dropdown box (as in this case), set the items you want in the list. The first column is what is displayed to the user, the second column is what is sent to the code when the user makes a choice. Note that you can enter in all the items at once by clicking the “+” button and entering your list in the form of:

item1name,item1value
item2name,item2value
etc.

When you do this, you’ll need to delete the first entry, which will be blank.

In the Include section, you should be able to click on the blank and choose your .h file (MeAccelGyro.h in this case) from your library. I have often found that this doesn’t work. If it doesn’t, it’s not a big deal, just type it manually in the Lib section (see next image).

  1. In the Declare section, you need to declare any variables or objects you will be using. If you have any initialization, that goes in the Setup section. Because I wanted the user to be able to use multiple blocks from the extension, I created an initialization Command block that is -required- to be called at the beginning of your mBlock code. I believe you could put this in the Common Code section (found under the Advanced Configuration tab using the default Arduino template) and then you wouldn’t need the separate command block. Because I’m trying to teach my students to always explicitly initialize their code, I elected not to do this.

The Code section is where the magic happens and is where I spent so much time stumbling. In Code, you put the value that you want your reporter block to take. The key here is that you must NOT put a semicolon after your statement. I’ve been programming for 35 years, so I simply put the semicolon in out of habit, not realizing that the Extension Builder’s interpreter was going to do something unique with this section. As you can see in the image, this statement sets the value of the block to the return of accel.gerAccel(), which is the accleration the device is measuring in g’s. Notice that this is how you access the parameters in your dropdown box. Place the parameter name (AXIS in this case) inside /*{ }*/. In this case, that was /*{AXIS}*/ .When the ArduinoC code is generated, this variable will be replaced with the user’s runtime selection from the dropdown box.

Finally, in the _Loop section goes any code that you want to be called continuously. In this case, we are obtaining the current value of the acceleration from the accelerometer by calling the appropriate method in the library. Notice that this statement DOES have a semicolon!

  1. For completeness, the OnRun section is where you would put any code that you want executed in Live Mode. Unfortunately, according to tech_support (in 2020, at least), Makeblock has removed are ability to access the value of the block in Live Mode. This means that ALL of our extensions that need this capability must be run in Upload Mode. Sucks, but there it is. Hopefully they will change their minds.

  1. In Extension Builder, press the Download button in the upper right corner and the builder will pack your extension for use. Open mBlock5, drag the extension onto main window, and it will be added to mBlock. If you are updating your extension, you’ll need to exit mBlock and restart it in order for the updated version to take effect. When you restart, you can add the extension from the “+ Extension” button at the bottom center of the screen as normal.

  2. In order to use your extension, you will need to add the Upload Mode Broadcast extension to BOTH the device and the sprite. As you can below, I now have two extensions loaded: Upload Mode and Me Accelerometer (my extension). When you build the code from blocks, the key is to use the “send upload message with value” block. The message doesn’t matter, but it has to be the same message on both the device and the sprite side. The value of the message is the reporter block you just made. Remember that you must be in Upload Mode, so you will be starting with the event "when starts up>.

DON’T FORGET TO HIT THE UPLOAD BUTTON TO SEND YOUR CODE TO THE ROBOT!

This is … embarrassing … when you realize that’s what’s wrong with your code. :slight_smile:

  1. On the sprite side, the “upload message value” reporter block now contains the value from your device. Just make sure the message name matches and it will work automatically. In this example, I’ve also added the Data Chart extension, as my students will be working with graphs of the acceleration. You can also simply use a “say” block to report the value.

  1. And finally, here is a sample data chart showing the z-axis acceleration in g’s as I tilt the robot forward and backward. There is no perceptible lag when using the USB cable. My next task is to make sure the 2.4g dongle will still send data in Upload Mode (the Bluetooth dongle does not seem to).

Good luck!

Dr. Keith “Kilo” Watt
PRofessor of Engineering Physics
Department of Physical Sciences
Glendale College, Arizona