<< Back to activities

Binary's UFO (micro:bit) kit - Alarm

This activity shows how to use Microsoft Make code - drag and drop blocks to make Binary's UFO alarm. This activity starts at an easy difficulty, moving into a medium difficulty when coding the micro:bit gyro.

Start >>

Step 1 - Wiring up Binary's UFO

Firstly wire up Binary's UFO to the BBC micro:bit as shown below:

Next >>

Step 2 - Access the BBC micro:bit editor to be able to "code" on the micro:bit

 

When you are coding using the BBC micro:bit, you need to do it through their online Editor. 

Click on this link http://microbit.org/ to access the BBC micro:bit website. 

When it loads, click on "Let's Code" shown in the image below:

 

Next >>

Step 3 - Start a new project using the 'JavaScript Blocks Editor (PXT)'

 There are lots of different types of editor you can use to program the BBC micro:bit. We are going to use the 'Makecode Editor' for this project. Click on the 'Let's Code' button shown in the image below.

 

Next >>

Step 4 - Set up your project

  If you have used the Makecode editor before, it will remember the previous code, clicking on the 'New Project' button will allow you to make more code.

When the new project loads you will see two blocks on the page, an 'on start' and 'forever' block, delete these block by draging them over the tabs to the left

  →  

Next we need to change the name of the script, click in the 'Untitled' box located next to the download button at the bottom of the page. This will allow you to change the name to something meaningful i.e. UFO Alarm

  →

 

Next >>

Step 5 - Start to Code

First we will create a ‘forever’ loop, this repeats any code inside it

Click on the ‘Basic’ tab, select the ‘forever’ block and drag it onto the blank page

    -->  

The forever block is useful as it runs the program again from the beginning once all of the steps have been completed.

 

Next >>

Step 6 – Adding a warning

Click on the ‘Basic’ tab, select the ‘show string’ block and drag it in-between the ‘forever’ block

   

Next we need to change the ‘Hello World!’ to ‘WARNING!!’, to do this, click on the “Hello World!” and type in ‘WARNING!!’

Next >>

Step 7 – Defining the axis

Click on the variables tab, and then select ‘make a variable’ then call it ‘Zmovement’.

Next drag the ‘set Zmovement to’ block into the forever block above the ‘show string’.

Click on the ‘Input’ tab, select the ‘acceleration (mg)’ block and drag it to the right of the ‘set Zmovement to’ block

     -->  

Next change the ‘X’ on the ‘acceleration (mg)’ block to ‘Z’

Drag the 'set Zmovement to acceleration (mg) z' block into the 'forever' block above the 'show string' block

Next >>

Step 8 – Adding an ‘If’ statement

Click on the ‘Logic’ tab, select the ‘if’ statement and drag it onto the blank page

   -->    

Again, click on the ‘Logic’ tab, select the ‘ or ‘ block and drag it onto the right of the ‘If’ statement

   -->    

Click on the 'Logic' tab, select the '0 < 0' block, drag and drop it into the first black space of the ' or ' block

   -->    

Click on the 'Logic' tab, select the '0 < 0' block, drag and drop it into the second black space of the ' or ' block and change the '<' to '>' on the second '0 <0'

   -->        -->

Click on the 'Variables' tab, select the 'Zmovement' variable, drag it and replace the first '0' in the '0 < 0' block

   -->    

Repeat the pervious stage but instead, replace the first '0' in the '0 > 0' block

Change the ‘0’ on the ‘Zmovement < 0’ statement to ‘-1100’ and the ‘0’ on the ‘Zmovement > 0’ to ‘-900’ – You may need to adjust these values.

Drag the previously made 'show string "WARNING!!"' and place it inside the do section of the 'if' statement

Drag the 'if' statement  into the 'forever' loop, making sure to place it underneath the 'set Zmovement to' block

Next >>

Step 9 – Adding the ‘X’ axis

So far you should have this:

Next we need to add different axis so if Binary’s UFO moves you will notice

Click on the ‘Variables’ tab, select the ‘set item to’ block and drag it underneath the previous ‘set Zmovement to’ block

   -->    

Click on the ‘item’ bit of the ‘set item to’ block and click on rename, change ‘item’ to ‘Xmovement’

   -->       -->

Click on the ‘Input’ tab, select the ‘acceleration (mg)’ block and drag it to the right of the ‘set Xmovement to’ block

   -->    

Click on the ‘Logic’ tab, select the ‘if’ statement and drag it underneath the previous ‘if’ statement, making sure it is still inside the ‘forever’ loop

Again, click on the ‘Logic’ tab, select the ‘ or ‘ block and drag it onto the right of the new ‘If’ statement

   -->    

Click on the 'Logic' tab, select the '0 < 0' block, drag and drop it into the first black space of the ' or ' block, then select the '0 < 0' block and drag and drop it into the second black space of the ' or ' block. Then change the '<' to '>' on the second '0 < 0' block

   -->          -->          -->    

 

Click on the 'Variables' tab, select the 'Xmovement' variable, drag it and replace the first '0' in the '0 < 0' block

    -->    

Repeat the pervious stage but instead, replace the first '0' in the '0 > 0' block

Change the ‘0’ on the ‘Xmovement < 0’ statement to ‘-100’ and the ‘0’ on the ‘Xmovement > 0’ to ‘100’ – You may need to adjust these values.

Click on the ‘Basic’ tab, select the ‘show string’ block and drag it into the ‘do’ section of the ‘if’ statement

   -->    

Click on the ‘Hello World!’ and change it to ‘WARNING!!’

Next >>

Step 10 – Adding the ‘Y’ axis

So far, this is what you should have:

Next we need to add the ‘Y’ axis.

Click on the ‘Variables’ tab, select the ‘set item to’ block and drag it underneath the previous ‘set Xmovement to’ block

Click on the ‘item’ bit of the ‘set item to’ block and click on rename, change ‘item’ to ‘Ymovement’

   -->    

Click on the ‘Input’ tab, select the ‘acceleration (mg)’ block and drag it to the right of the ‘set Ymovement to’ block. Then change the 'X' to 'Y'

   -->          -->    

Click on the ‘Logic’ tab, select the ‘if’ statement and drag it underneath the second ‘if’ statement, making sure it is still inside the ‘forever’ loop

Again, click on the ‘Logic’ tab, select the ‘ or ‘ block and drag it onto the right of the third ‘If’ statement

   -->    

Click on the 'Logic' tab, select the '0 < 0' block, drag and drop it into the first black space of the ' or ' block, then select the '0 < 0' block, drag and drop it into the second black space of the ' or ' block. Then change the second '0 < 0' statement to '0 > 0'

   -->          -->          -->    

 

Click on the 'Variables' tab, select the 'Ymovement' variable, drag it and replace the first '0' in the '0 < 0' block

   -->    

Repeat the pervious stage but instead, replace the first '0' in the '0 > 0' block

Change the ‘0’ on the ‘Ymovement < 0’ statement to ‘-100’ and the ‘0’ on the ‘Ymovement > 0’ to ‘100’ – You may need to adjust these values.

Click on the ‘Basic’ tab, select the ‘show string’ block and drag it into the ‘do’ section of the third ‘if’ statement

   -->    

Click on the ‘Hello World!’ and change it to ‘WARNING!!’

Next >>

Step 11 – Making the LEDS flash

In this step you will make Binarys UFO flash by turning the LED pins on and off

Click on the ‘Pins’ tab, select the ‘digital write’ block, drag and drop it underneath the ‘show string “WARNING!!”’ block on the first ‘if’ statement and change the pin to ‘P1’ and the '0' to '1'

   -->          -->    

 

Click on the ‘Basic’ tab, select the ‘pause’ block, drag it underneath the ‘digital write (0,1) block and change the value to ‘500’ – this changes the pause to 0.5 seconds

   -->          -->    

Duplicate the 'digital write' block two times and place them underneath the 'pause (ms) 500' block

On the second 'digital write' block, change the '1' to '0' and on the third 'digital write' block, change the pin from 'P1' to 'P2'

    -->    

Duplicate the 'pause (ms) 500' block and drag into place underneath the 'digital write 1 to pin P2' block

Duplicate the 'digital write 1 to pin P2' block and place underneath the second 'pause (ms) 500' block, then change the ‘1’ to ‘0’

   -->    

Duplicate the previous stages in this step and place them in each do section, of the second and third ‘if’ block

   -->    

Next >>

Step 12 - Ready to test!

 You are ready to test your code !! You can do this before you put it on Dimm. On the left side is a 'Virtual' micro:bit - its a version of the real thing that you can use to test. 

This will automatically update it self. 

 

 

Next >>

Step 13 - Does it work?

Now, on the virtual micro:bit, move the mouse around the virtual micro:bit and the text "WARNING!!" should show, if the text shows up, you will need to fully test the program by downloading it due to not seeing if 'P1' and 'P2' work.

     -->          -->     

Next >>

Step 14 - Download the code ready for Binary's UFO

It is now time to see it work on Binary's UFO. 

On the bottom left of the editor is a button that says 'Download' - shown below in a red box

Click on it - the page will download a file to your computer

Next >>

Step 15 - Find your downloaded code file

Click on the downloaded file at the bottom of the browser - select 'Show in Folder'

This will open the folder containing all your downloaded files - this will include a file called something like 'microbit-Binary-s-UFO-Alarm.hex'

Right click on the file and choose to copy it (this is ready for the next step)

Next >>

Step 16 - Plug in the micro:bit

Now you need to plug the micro:bit on Binary's UFO, into your computer - using the USB cable that came with the micro:bit.

When you plug it in, a window should pop up on screen that looks a bit like this:

 

Next >>

Step 17 - Paste the code file to the micro:bit

Right click on a blank space within the micro:bit folder and choose 'Paste' - it will display a window a bit like the below as it copies the file to the micro:bit.

Next >>

Step 18 - See if Binary's UFO Alarm Works!

Next >>