Buttons, Display & Sound

Use Button Events to control LED Display and play Sound

  • Concepts:
    • Events
    • Tone/Note
    • Sequence

Duration: 30 - 45 minutes

Materials

A micro:bit, battery pack and 2 x AAA batteries

battery pack and micro:bit

2 to 4 crocodile clips

crocodile clips

Headphones

earbud headphones

Blocks

    basic.showLeds(`
        . # . # .
        . . . . .
        . # # # .
        . # . # .
        . # # # .
        `);
input.onButtonPressed(Button.A, function () {})
music.playTone(Note.C, music.beat(BeatFraction.Quarter))
music.rest(music.beat(BeatFraction.Whole))
music.beat(BeatFraction.Quarter)

Step 1: Make a Smiley

Open https://makecode.microbit.org/ in your web browser

    basic.showLeds(`
        . # . # .
        . . . . .
        . # # # .
        . # . # .
        . # # # .
        `)

From Basics, drag a show LEDs block into the coding area

  • Create a face with LEDs

micro:bit USB connection Connect your micro:bit to your computer via USB and click Download. Follow the instructions to move the code to your micro:bit.

Step 2: Add Smiley LED Button Events

input.onButtonPressed(Button.A, function () {
    basic.showLeds(`
        . # . # .
        . . . . .
        . # # # .
        . # . # .
        . # # # .
        `)
})
input.onButtonPressed(Button.B, function () {
    basic.showLeds(`
        . # . # .
        . . . . .
        . . . . .
        # . . . #
        . # # # .
        `)
})

From Input, drag an on button ‘A’ pressed block into the coding area

  • Snap the LED face into the block

  • Create a ‘B’ button block with a different LED face

  • Download the code to your micro:bit and try the A & B buttons

Step 3: Add Headphone Speakers using Crocodile clips

crocodile clips attached to pins 0 and GND

crocodile clips attached to headphone jack Connect GND to the base of the headphone jack using a second crocodile clip (usually black)

Connect pin 0 to the tip of the headphone jack with a crocodile clip

attaching batteries and micro:bit

Attach the micro:bit & battery-pack to the guitar body

connecting headphone speaker

Connect the headphones with crocodile clips

The micro:bit can play music

The play tone block allows a range letter note tones from C to B5. Songs are played using sequences notes. Like the beginning of a birthday song (C, C, D, C, F, E).

input.onButtonPressed(Button.A, function () {
    music.playTone(Note.C, music.beat(BeatFraction.Quarter))
    music.rest(music.beat(BeatFraction.Whole))
    music.playTone(Note.C, music.beat(BeatFraction.Quarter))
    music.rest(music.beat(BeatFraction.Whole))
    music.playTone(Note.D, music.beat(BeatFraction.Quarter))
    music.rest(music.beat(BeatFraction.Whole))
    music.playTone(Note.C, music.beat(BeatFraction.Quarter))
    music.rest(music.beat(BeatFraction.Whole))
    music.rest(music.beat(BeatFraction.Whole))
    music.playTone(Note.F, music.beat(BeatFraction.Half))
    music.rest(music.beat(BeatFraction.Whole))
    music.playTone(Note.E, music.beat(BeatFraction.Whole))
})

Step 4: Add Tone Playing Events for Buttons A & B

input.onButtonPressed(Button.A, function () {
    basic.showLeds(`
        . # . # .
        . . . . .
        . # # # .
        . # . # .
        . # # # .
        `)
    music.playTone(Note.A, music.beat(BeatFraction.Whole))
})
input.onButtonPressed(Button.B, function () {
    basic.showLeds(`
        . # . # .
        . . . . .
        . . . . .
        # . . . #
        . # # # .
        `)
    music.playTone(Note.G, music.beat(BeatFraction.Whole))
})

From Music, drag play tone C for 1 beat block under the show leds in Button A Pressed

  • modify tone by choosing a note (letter) and experiment with high and low pitches
  • set beat to 1

Repeat for Button B event

Download the code to the micro:bit

Try the A & B buttons with headphones and power connected

Congratulations on completing the basic guitar!

Challenge: Create samples of longer music to play for each button instead of the single tone

  • Tip: Search for “ABC music notation” or “Easy Music Notes” + the name of a song

Extra

NEXT: Light Sensor Tone Control