Micro:bit

Introduction

The micro:bit is a device with a 5x5 LED grid. We created programs for these. The projects are shown below.

Hello World

My first program created was one that displayed the phrase "Hello World" on the LEDs. It also showed my name when button A was pressed. Here is a snippet of the code:

input.onButtonPressed(Button.A, () => {
basic.showString("Nicholas")
})
basic.showString("Hello world!")

Emotions

We also did a project in which multiple gestures/inputs from the user would yield a different response with the LEDs. The code is here:

input.onButtonPressed(Button.B, () => {
basic.showLeds(`
# . . . #
. . . . .
. . . . .
# # # # #
# . . . #
`)
})
input.onButtonPressed(Button.AB, () => {
for (let i = 0; i < 5; i++) {
basic.showLeds(`
# . . . #
. . . . .
. . . . .
# . . . #
# # # # #
`)
basic.pause(1000)
basic.showLeds(`
# . . . #
. . . . .
. . . . .
# # # # #
# . . . #
`)
basic.pause(500)
}
basic.clearScreen()
})
input.onGesture(Gesture.Shake, () => {
basic.showLeds(`
# . . . #
. . . . .
. . . . .
. . . . .
# # # # #
`)
})
input.onButtonPressed(Button.A, () => {
basic.showLeds(`
# . . . #
. . . . .
. . . . .
# . . . #
# # # # #
`)
})

Light Sensor and Sounds

The purpose of this program was to finally utilize the on-board light sensor and pins to yield an output of audio. In this scenario, I created a program that would play an excerpt from the Star Spangled Banner when the light level would exceed a certain level. We used a flashlight as a light source, alligator clips, a speaker, and the provided battery pack. This is my code:

basic.forever(() => {
led.plotBarGraph(input.lightLevel(), 255)
if (input.lightLevel() > 155) {
music.setTempo(60)
music.playTone(392, music.beat(BeatFraction.Double))
music.rest(music.beat(BeatFraction.Double))
music.playTone(330, music.beat(BeatFraction.Double))
music.rest(music.beat(BeatFraction.Double))
music.playTone(262, music.beat(BeatFraction.Double))
music.rest(music.beat(BeatFraction.Double))
music.playTone(330, music.beat(BeatFraction.Double))
music.rest(music.beat(BeatFraction.Double))
music.playTone(392, music.beat(BeatFraction.Double))
music.rest(music.beat(BeatFraction.Double))
music.playTone(523, music.beat(BeatFraction.Whole))
music.rest(music.beat(BeatFraction.Double))
}
})

Final Project - Flappy Bird

I created a "Flappy Bird" project in the Python micro:bit editor. I chose to create this because out of our available options, this seemed the most demanding in terms of code. I also wanted to get a sense of how Python on the micro:bit is. Out of everything, the most difficult part of completing this project was getting used to how Python wants indentation to be like. It took me a long time to figure out what was wrong with my indentation, but after that, everything ran smoothly and without error. I based the game off of a tutorial, and I added a sound, menu, and high score system upon it.

from microbit import *
import random
import music
y = 50
speed = 0
mute = 0
hs = 0
d = 0
score = 0
menustatus = 0
frame = 0
status = 0
wintone = ["C5:1"]
losetone = ["D5:1", "A5:1"]
thrupipe = ["D5:1", "E5:1"]
def make_pipe():
    i = Image("00003:00003:00003:00003:00003")
    gap = random.randint(0,3)   
    i.set_pixel(4, gap, 0)      
    i.set_pixel(4, gap+1, 0)
    return i
i = make_pipe()
DELAY = 20                     
FRAMES_PER_WALL_SHIFT = 20      
FRAMES_PER_NEW_WALL = 100       
FRAMES_PER_SCORE = 50       
menuone = Image("00000:"
                "00000:"
                "90009:"
                "00090:"
                "00009:")
menu = Image("00000:"
             "00000:"
             "90009:"
             "00000:"
             "00000:")
loading1 = Image("00999:"
                 "00000:"
                 "00000:"
                 "00000:"
                 "00000:")
ss = Image("00099:"
           "00000:"
           "00000:"
           "00000:"
           "00000:")
a = Image("00009:"
          "00000:"
          "00000:"
          "00000:"
          "00000:")
while True:
    while (status == 0):
        display.show(menu)
        if button_a.was_pressed():
            display.show(loading1)
            sleep(1000)
            display.show(ss)
            sleep(1000)
            display.show(a)
            sleep(1000)
            status = 1
        if button_b.was_pressed():
            display.scroll(str(hs))
        while (status == 1):
            frame += 1
            display.show(i)
            if button_a.was_pressed():
                speed = -8
                music.play(wintone)
            speed += 1
            if speed > 2:
                speed = 2
            y += speed
            if y > 99:
                y = 99
            if y < 0:
                y = 0
            led_y = int(y / 20)
            display.set_pixel(1, led_y, 9)
            if i.get_pixel(1, led_y) != 0:
                music.play(losetone)
                sleep(500)
                display.scroll(str(score))
                if score > hs:
                  display.scroll("NEW HS:" + str(score))
                  hs = score
                  score = 0
                status = 0
            if(frame % FRAMES_PER_WALL_SHIFT == 0):
                i = i.shift_left(1)
            if(frame % FRAMES_PER_NEW_WALL == 0):
                i = make_pipe()
            if(frame % FRAMES_PER_SCORE == 0):
                score += 1
                music.play(thrupipe)
            sleep(20)


while (d == 1):
  sleep(100)

In this code, buttons A and B are used as the input sources. It can detect which phase you are in within the game through the while loops I have implemented, allowing the functions of A and B to correspond to the current phase in which the program is in. The output would be the bird flying up when a was pressed (while in-game). This is a video of it functioning:

Nicholas micro:bit

Reflection

In conclusion, I was really captivated by all of the possibilities the micro:bit possesses. I'm hoping to do more projects using Python, as it's a very powerful language for this computer. I liked everything having to do with the micro:bit. Doing projects with the micro:bit has allowed me to learn about coding even more, and how powerful it can be.