CODING: MakeCode Blocks

      If you haven't take a look at the Micro:bit website/coding page on the blog, please take a look now.  Understanding the basic structure of the MakeCode environment will make understanding the following discussion much easier.   Since this is really about the command blocks, I'll try to color code each command, matching the color in the programming environment.

     Let's start by building a simple program - Dice.  This is just a summary of the excellent description (video, text, coding) provided by Micro:bit here: https://microbit.org/projects/make-it-code-it/dice/  

  • What this program should do: when I shake the Micro:bit, a random number between 1 and 6 should appear on the led screen.  Each time I shake the bit, a new random number should appear.
     To do, this I need 3 things: a way to get a movement of the bit to start a program that selects a random number and shows it on the screen.  What blocks will do that for me?  The movement of the bit is the input, so under the Input commands I see a block with On ShakeI'll drag that on to the editing screen.  Next, I need a random number - sounds like Math, and I see pick random 0 to 10.  I'll drag that onto the editing screen and put it inside the On Shake block.  
     Ooops, it won't go inside the On Shake block.  I forgot I need the number to appear on the led screen, so I need to find a block that will show the number.  After looking around I find a block called Show Number under the Basic tab.  That will go into the On Shake block, and then I can put the pick random block inside the show number.  I also see that the 0 and the 10 look like they can be edited, so I highlight them and change them to 1 and 6.  Here's the program:


     I can test this program on the virtual bit.  Notice that it has a white dot for SHAKE (it's a virtual shake that appears when you select on shake from input.)  I press the SHAKE button, and it works.  Each time I press the button, a new random number appears.

     There are several more example programs, some very simple, some very complex.  But it should be clear that to be able to program in MakeCode, you really need to know which block does what.  You can find this out in a variety of ways, including reading the manual and playing around.  I'd suggest looking at lots of sample programs - there are hundreds - and find out how the programmer got the program to do what they wanted.  You can also play around with simple programs, adding something new and seeing what happens.  You can't break anything this way so don't worry about that happening!
     Here's an example:  I wanted to add some music and graphics to my Dice program.  Try it and see what happens.  The music is from the Music block and the graphic from the Basic block.


     Notice that when I added the music block, the virtual machine added a virtual speaker, connected to two of the GPIO pins.  Also, don't worry about the two blue blocks at the top of the editing screen.  They don't do anything, I forgot to delete them from an earlier program.  
     You can also find out more about the blocks and what they do here:  https://makecode.microbit.org/reference


     


Comments

Popular posts from this blog

INTERACTIVE ROBOT DESIGN: AN INTRODUCTORY COURSE