Resources for the 'Picnic Protectors' Creative Computing Club.
During this workshop, you will design and code a micro:bit alarm system.
This page recaps what we discussed as a group.
To skip to the examples and ideas, click here.
Open the new Python editor by visiting python.microbit.org/v/beta. Your screen will look something like this:
Edit the code on the screen, then flash it to your micro:bit to see the changes.
Try:
You can find the accelerometer code by looking in the Reference menu for 'Accelerometer'.
The code you are shown includes another while True loop, so we should not drag-and-drop the whole snippet into our current code.
Unlike in Scratch, where you can have lots of forever loops all running at the same time, we can only use one main loop at once in Python - if you put another while True loop underneath the first one, this code will never happen, because the micro:bit is too busy doing the first loop!
To use the accelerometer, we use an if statement - if the accelerometer registers a 'shake' gesture, make something happen.
Edit you code so that it looks like this:
while True:
if accelerometer.was_gesture('shake'):
display.show(Image.HEART)
sleep(1000)
display.scroll('Hello')
You will need to indent the lines of code that you want to happen when the micro:bit has been shaken.
What kind of gestures would be useful for your security system?
An example video is show below - the micro:bit is happy when it is 'face up' , then angry when it is anything else!
while True:
if accelerometer.is_gesture('face up'):
display.show(Image.HAPPY)
else:
display.show(Image.ANGRY)
Sound is important to a security system - you can add music, beeps or sound effects as well as changes to the display.
Have a look through the Sounds section of the Reference menu, there are a few different options you can use.
Try the 'expressive sounds', which are at the bottom of the menu - you can drag-and-drop this directly into your code, as it is only one line.
Think about when you want your sound to happen - the micro:bit will do your code in order - should it play the sound before or after changing the image?
Experiment with the music code as well - the editor will automatically add the 'import music' line to the top of your code - you only need this once.
A pressure sensor works by having an electrical circuit, that is only complete when something heavy is put on it. If the heavy thing is moved, the circuit is broken (two wires do not touch any more).
If you put the micro:bit in the circuit, then it can tell whether the electricity is able to flow or not.
There are lots of ways that you could choose to set this up, but an example is described here for you to start with...
Try this code example:
while True:
if pin0.read_digital() == 1:
display.show(Image.HEART)
else:
display.show(Image.ANGRY)
We have shown you some ideas of how to create a security system for your picnic, which options do you think would work best?
Build your security system and test it out on your family - if they manage to steal your picnic, work out how they did it and improve your design.