Bett 2019 micro:bit hacks
On Friday 25 January I shall be on the micro:bit stall at Bett 2019 and I will be running a short 30 minute workshop on how to use Mu with the micro:bit.
Of course I shall be using #pounderland tat...I mean tech to show that learning to code is not just about "Hello World" and more about interesting ways to capture the imagination and stealthly teach the core concepts.
So my 30 minute slot is a simple intro to Mu, and taking a small amount of Python code with some Poundland LEDs and making three projects that use gestures to trigger certain events.
Quick wiring diagram
Get some Poundland LED lights, snip the battery box off (keep it!) strip the about 1cm of sheath from the wires. Get some croc clips and connect them to the micro:bit.
Checking the polarity
The simplesy way to determine the polarity of the LEDs is to remove the croc clip from 0, and connect it 3V. Do the LEDs light up? If so then all is good, return the croc clip to 0 and move on.
If not then remove the croc clip from GND and connect it to 3V, and then remove the croc clip from 0 and connect to GND. Do they light up? If so then take the crop clip off 3V and return it to 0.
If the LEDs still do not light up, then you may have a faulty batch.Always check your hardware for faults before moving on. It is easier to debug this issue before we start writing code to control the LEDs
And here are each of the code snippets
Project 1 - React to a shake
A simple shake the micro:bit and LEDs turn on for 1/10th second, then off for the same time. It then repeats three times.
from microbit import *
while True:
if accelerometer.was_gesture("shake"):
for i in range(3):
pin0.write_digital(1)
sleep(100)
pin0.write_digital(0)
sleep(100)
Project 2 - Flash the LEDs randomly
Here we use a random integer to set the duration of a sleep, effectively letting the micro:bit decide how long the LEDs should be on / off for. Oh and we use a random integer to select a number between 0 and 10 and use that as the number of times the loop will repeat.
from microbit import *
from random import randint
while True:
if accelerometer.was_gesture("shake"):
for i in range(randint(0, 10)):
pin0.write_digital(1)
sleep(randint(1, 1000))
pin0.write_digital(0)
sleep(randint(1, 1000))
Project 3 - React to different gestures
Shake the micro:bit and it does something, but place it face down and it will do something else all thanks to the power of a conditional statement!
from microbit import *
while True:
if accelerometer.was_gesture("shake"):
for i in range(3):
pin0.write_digital(1)
sleep(100)
pin0.write_digital(0)
sleep(100)
elif accelerometer.was_gesture("face down"):
for i in range(6):
pin0.write_digital(1)
sleep(100)
pin0.write_digital(0)
sleep(100)