16.07.2015 Views

MicroBit-Quick-Start-Teacher-Guide

MicroBit-Quick-Start-Teacher-Guide

MicroBit-Quick-Start-Teacher-Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Challenge 2: Digital petCreating a while loopPREVIEWWe want each state of your digital pet to continue playing until a button is pressed.We therefore need to add a while loop. A while loop will continue running a pieceof code until a certain condition is met.08Before you continue, click on script. Underneathcode you will see your main program. Main is likeyour ‘home’ code; all the other functions you createwill run from this. You should see all the functionsyou have created underneath main. Click main toselect it.09Select do nothing within the main function to startwriting your code. Select while from the keyboard.10Your while loop will appear as shown right.This will make our program loop indefinitely.11Running this program at this stage would burn thebattery. To fix this, add a pause instruction at theend of the loop. Click do nothing in the code, thenon the code keyboard click micro:bit, more, thenpause. Any pause duration will work; in this case,we can even pick 0.Test your program and debugIt’s important that we test our programs regularly. This allows us to debug theprogram and fix any errors. Emphasise this point with students.17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!