MakeCode blocks: Basic, Input and Music

Lesson 3 of 14 · micro:bit

In the MakeCode environment, the groups of blocks are divided into different categories that make creating programs easier. Here is a short overview of the main categories:

1. Basic – Contains the basic blocks for showing text, waiting, repeating and controlling basic logical operations.

2. Input – Includes blocks for interaction with the user, such as buttons, movements and sensors.

3. Music – Used for creating tones and melodies with the speaker.

4. LED – Gives control over the light-emitting diodes, such as showing images and animations.

5. Logic – Provides conditions and logical operations that make decisions in the program possible.

6. Loops – Contains blocks for repeating actions, such as for and while loops.

7. Math – Contains mathematical functions for addition, multiplication, random numbers and other calculations.

8. Variables – Blocks for defining and working with variables, keeping values that can be used later.

9. Functions – Makes it possible to create functions that can simplify and organise the code.

Each category makes it easier to find the blocks that let the user put the code together faster.

The basic command palette – Basic

This command palette has already been described in the previous article: Introduction to the micro:bit

The Input command palette

The Input command palette in the MakeCode environment
Picture 1: The Input command palette in the MakeCode environment

In the MakeCode environment, the command palette called Input contains blocks that let users register different kinds of interaction with the device. These blocks can react to button presses, temperature changes, movements, light and other sensors, which makes it possible to create rich interactive applications.

The commands in the Input palette and their descriptions:

  • on button pressed – Runs the block when a certain button is pressed, usually A, B, or both.
  • on shake – Runs the blocks when the device detects shaking, ideal for applications that react to movement.
  • light level – Returns the light level, so the device can react to the lighting around it.
  • temperature – Reads the current temperature of the surroundings, usually in degrees Celsius, so the program can react to temperature changes.
  • on logo touched – Runs the block when the logo on the device is touched, useful for direct interaction. It runs some code in the program when the micro:bit logo is pressed, touched or released.
  • on pin pressed – Runs the block when a certain pin is pressed, which gives additional customisable inputs for applications that use external components.
  • acceleration – Measures the acceleration of the device along a certain axis (x, y or z), which makes it possible to detect tilt and movement.
  • compass heading – Reads the device's compass value and makes it possible to detect the orientation of the device towards the points of the compass.
  • rotation – Returns the rotation angle of the device along a certain axis, which can be useful for applications that detect tilting.

These commands make it possible to create dynamic and intuitive interactions with the environment, giving rich possibilities for control and customisation.

  • button "A" is pressed – Starts a certain action when button "A" is pressed. Useful for simple interactions.
  • button "B" is pressed – Similar to the previous one, it starts an event when button "B" is pressed.
  • pin P0 is pressed – Starts an action when pin P0 (or another pin) is touched.
  • pin P0 is released – Starts an event when the touch on P0 is released.
  • running time (ms) – Returns the number of milliseconds since the start of the program, useful for measuring time.
  • light level – Gives the light level in the range from 0 to 255.
  • is shake gesture – Tests whether a movement has happened.
  • on loud sound – Runs some code when the microphone registers a sound.
  • logo is pressed – Checks whether the micro:bit logo is pressed right now.
  • sound level – Finds out the level of the sound heard by the microphone.

The Music command palette

The Music command palette in MakeCode
  1. play tone – Plays a tone of a certain frequency for a defined duration. Useful for creating notes and simple melodies.
  2. play melody – Starts a predefined melody using a sequence of tones, and lets you choose the speed and set up repetition.
  3. ring tone – Produces a continuous tone while it is active, without a defined duration; ideal for controlling sound during events.
  4. stop all sounds – Stops all active sounds, which is useful for ending melodies and stopping a tone.
  5. change tempo by – Increases or decreases the speed of the melody by a certain value, which makes it easy to adjust the tempo.
  6. set tempo to – Sets the tempo of the melody to a certain number of beats per minute, giving precise control of the rhythm.

The idea of a string in programming

In the next activity we will focus on the idea of a "string" in programming. A string is a sequence of characters inside quotes and it is key to working with text. On the micro:bit we can use only the ASCII codes from 32 to 126, which include letters, digits and basic symbols.

An example in Python (MakeCode):

message = "Hello, world!"   # a string is written between quotes
basic.show_string(message)  # the text scrolls across the LED screen

Note: the print statement works in Python on a computer; on the micro:bit text is shown with the basic.show_string statement. The screen shows only letters without accents (letters such as č, ć, š, ž, đ are not supported).

A linear program

Looking at the previous example, notice that the program runs line by line, in order from top to bottom, and that is why we can call it a linear program.

Here is a more complex example that uses commands from the basic, music and input palettes:

// we keep a string (text) in a variable
let message = "A musical melody!"
basic.showString(message)   // the message scrolls across the screen

// pressing button A plays the note C (262 Hz) for a quarter of a beat
input.onButtonPressed(Button.A, function () {
    music.playTone(262, music.beat(BeatFraction.Quarter))
})

Task: Make a melody that will be played on the micro:bit

Test number 2

Look at the pictures connected with the questions in test 2, and then start the test! Use the tones from the music command palette to play notes and make a melody from them. For example, you can create the tones in this order:

c, d, e, e

c, d, e, e

c, d, e

c, d, e, e

c, d, e

e, e, d, d

c

Test 2 – question 1Test 2 – question 2

Test 2: The Basic, Input and Music palettes

Look at the pictures connected with the following test, and then click the Start button to begin the test!Test 2 – question 3Test 2 – question 4Test 2 – question 5

The Basic, Input and Music palettes. Test 2 – question 1

Picture 1: The Basic, Input and Music palettes. Test 2 – question 1

The Basic, Input and Music palettes. Test 2 – question 2

Picture 2: The Basic, Input and Music palettes. Test 2 – question 2

The Basic, Input and Music palettes. Test 2 – question 3

Picture 3: The Basic, Input and Music palettes. Test 2 – question 3

The Basic, Input and Music palettes. Test 2 – question 4

Picture 4: The Basic, Input and Music palettes. Test 2 – question 4

The Basic, Input and Music palettes. Test 2 – question 5

Picture 5: The Basic, Input and Music palettes. Test 2 – question 5

The quiz above is in Serbian.

Writing programs with commands from the "Basic" and "Input" palettes

In this part of the tutorial we extend the basic knowledge through exercises that use blocks from the Basic and Input categories. These activities make it possible to create programs that react to different inputs, such as pressing buttons and moving the device. Through these examples you will practise the basic commands further and develop the skills needed for making creative, interactive projects.

Task 1: Showing the message "Hello" on the screen

Task: Program the micro:bit so that the message "Hello" appears on the screen when it is switched on.

Instructions: Use the block show string from the Basic category and type the text "Hello". Put the block in the on start function.

Task 2: Showing a heart on the screen

Task: Let the micro:bit show a heart icon that stays on the screen all the time.

Instructions: Drag the block forever from the Basic category and add the block show icon with the heart icon inside it.

Task 3: Showing the letter A when button A is pressed

Task: Show the letter "A" on the screen when button A is pressed.

Instructions: Use the block on button A pressed from the Input category, then add the block show string with the letter "A".

Task 4: Showing the letter B when button B is pressed

Task: Show the letter "B" when button B is pressed.

Instructions: As in the previous task, use on button B pressed and the block show string with the letter "B".

Task 5: Showing a note icon when buttons A and B are pressed together

Task: Show a note icon when both buttons A and B are pressed.

Instructions: Use the on button A+B pressed block, and then show icon with the note icon.

Task 6: Showing the first letter of your name when the device is shaken

Task: When the micro:bit is shaken, let it show the first letter of the student's name.

Instructions: Use on shake from the Input category, then show leds and switch on the LEDs you want to show the letter.

MakeCode – showing the first letter of a name when the micro:bit is shaken

The same program in JavaScript (MakeCode):

input.onGesture(Gesture.Shake, function () {
    basic.showLeds(`
        . # # # .
        . # . . #
        . # . . #
        . # . . #
        . # # # .
        `)
})
Picture 6: MakeCode – showing the first letter of a name when the micro:bit is shaken

Task 7: Showing a number and a "scissors" picture

Task: Show the number 7 on the micro:bit, and then the "scissors" picture! Correct the code so that the scissors icon is shown 3 seconds after the number 7 is shown.

Check your knowledge

Open the quiz

The Basic, Input and Music blocks

1. Which block shows text that scrolls across the screen?




2. Which palette is the block on button A pressed in?




3. When does the block on shake run?




4. What is a string?