In this project weâre going to focus on the code.
In part one of this series, we focused on the hardware.
If you donât have a tech background, you might feel a bit nervous about teaching coding to students. But we want to show you that code is nothing more than commands to make your micro:bit do what YOU want it to do.
You can watch the video of this project here:
Let's codeIf you open your browser and go to Microbit.org, youâll see a big button at the top inviting you to code. Letâs code! Microbit lets you code in two separate languages, Javascript, which weâll go over today, and Python, which weâll get into in the next project.
You can also control your micro:bit wirelessly from your phone, using the Android and IoS apps.
Javascript is a popular language most commonly used for web development. The Javascript editor for micro:bit includes âblocks.â
Weâll be focusing on using these in todayâs tutorial. If youâve used MITâs scratch or ardublock in the past, these will look familiar.
The reference guideMicro:bit provides a handy reference for what code does what. Right click the âReferenceâ button, under the header âJavascript blocks editorâ to open it in a new tab.
It's helpful to keep that open in a different tab while youâre coding so you can go back and look at it if needed.
The blocks editorLetâs start coding in JavaScript. Click the orange âLetâs codeâ button. This will open the javascript coding window where youâll see some pieces that look a bit like puzzle pieces.
These are micro:bit blocks. Blocks are a visual programming language, or VPL. These drag and drop pieces make it easy for students to quickly get started controlling their micro:bit. If you click on the Javascript tab at the top of the window youâll see the actual code behind your blocks.
This makes it easy for students to transition to coding in Javascript later on. Weâll be focusing on just the blocks in todayâs lesson.
The Getting Started tutorialIf you click âGetting Started,â thereâs a quick and easy tutorial that interactively shows you how to use the blocks. I recommend going through it before continuing with this lesson.
In the tutorial, you'll walk through making a âdieâ that selects a random number when you shake the micro:bit, and programming the micro:bit to show a smiley face when button B is pressed.
In this project, we'll walk you through a cute project that uses the onboard temperature sensor to make a "thermometer."
Create a new projectLetâs create a new project by clicking âProjectsâ and 'New Project.' We'll name it âthermometer,â since weâll be getting a temperature reading.
Click the word âuntitledâ and rename your project âthermometer.â
By default, the âon startâ and âforever blocksâ are our starting point.
These two blocks are called event handlers. They respond to an event. For instance, the âon startâ event handler responds once when the code is fired up on the micro:bit. It will look for and read through any blocks within the âon startâ event handler as soon as the program is started.
The 'forever' event handler behaves similarly, but instead of reading through any blocks once, it will read them over and over and over again until a new program is uploaded or the micro:bit is unplugged.
HelpIf you have questions about what a block does, you can hover your mouse over it to get more info. You can also right click and select âhelpâ to be taken to the reference page. Hide the reference by clicking the arrow tab. You can always get back to the reference page by clicking the reference tab on the side of the page.
We want to read the temperature using the micro:bitâs onboard temperature sensor and show the number on our LED screen.
The first thing we have to do is make a variable for the temperature value. A variable is basically a container that holds information. You can see the options for different kinds of blocks in the side panel:
Click on âvariableâ and choose âmake a variableâ Itâs best to label the variable with short but descriptive names. Weâll call our variable âtempâ, short for temperature. Click âOK.â Now if we go back to our variable menu, our variable will show up there.
Itâs best to label them with short but descriptive names. Weâll call our variable 'temp,' short for temperature. The temp variable will constantly update with data from our temperature sensor.
To make it do this:
- Select the âset variable toâ block from variables.
- Select the âtempâ variable from the variable dropdown to set âtempâ to the temperature.
- Go to the input blocks and select the temperature input.
- Replace the zero in the âset variable toâ block with the temperature input.
Inputs and outputs are what make the micro:bit fun, since they allow you to interact with the world around you. The micro:bit can read data from the physical world using its onboard light sensor, accelerometer, compass, buttons and of course, temperature sensor. These are all inputs. LEDs, Radio, Music, and all of the basic blocks are outputs.
- Now we want to add an output. Weâll start by going to âbasic blocksâ and grabbing the âshow numberâ output. Place it on on your editor.
- We want to show the number thatâs being saved in your âtempâ variable.
- Select your temp variable from the variables blocks.
- Drag and drop âtempâ to replace the â0â in the âshow numberâ block with our temperature.
- Drag and drop this to your âon startâ event handler.
- We want to show the number thatâs being saved in your temp variable, so add your variable as the number to show.
- Press the download button to download your code as a .hex file.
When you download the code, it gets compiled from javascript into ones and zeros that the micro:bit can store and execute.
You can also share your code by clicking âshareâ. This creates a link to a page where your code exists online. You can share this link with others if youâd like to share your code.
Program your deviceBefore uploading your code to your micro:bit, make sure you have plugged your micro:bit into your computer. Find the micro:bit on your computer. You can find it in the âdevices panelâ of your computer.
- Find the code in the download folder of your computer.
- Drag the downloaded code over to our micro:bit, just like dragging and dropping a file to an external drive or USB stick.
- You should see a yellow LED flashing next to your USB. This means the code is being transferred.
- As soon as the code uploads, you should see the temperature (in Celcius) scrolling across the LEDs continuously.
You might experience problems when using an old .hex file on new hardware. The remedy is to open the old .hex file in MakeCode and re-save the .hex file, then that file will work on both old and new hardware.
Using Logic BlocksWhat if we wanted to sound an alarm if the temperature gets too hot? We would use the logic blocks for that.
- Select the âIf thenâ logic block.
When you first drag it to the canvas, youâll notice that these blocks are greyish hued. If you remove your temperature code from the âon startâ event handler, it also turns greyish. That means that theyâll be excluded from the final code, since they must be within an event handler to run. Itâs fine if you leave them here when you download your code. The program will just ignore them when it compiles.
Code needs to be placed within an event handler so that it will run.
You can also delete code if you want to declutter your workspace. If you want to get rid of a block you can simply select it and click âdelete.â
If Then statements"If then" statements are a common type of logic used in code. For instance, If the temperature is greater than 30 degrees Celcius, Then alert us.
- Place your temperature variable setter within the forever event handler, so that it will read the temperature continuously. The blocks should show their true colors, meaning that this code will work if we upload it to our board.
- Place your âif thenâ block within the âforeverâ event handler as well.
- Open the logic blocks.
Weâll use another block from the logic blocks - The comparator block. This block can be confusing, since it looks very similar to the operator block from the math blocks. The difference is that the math block performs operations, like addition and multiplication, while the comparator block compares values. Youâll also notice the math block is purple and the comparator block is green.
- Choose the comparator block from the logic blocks.
- Drag the comparator block to replace the "true" in the 'if'>'then' statement.
- Drag your temp variable into the first space, replacing your zero.
- Change the equals sign to a âgreater than signâ from the dropdown.
- Change the zero to a 30.
Letâs show an alert on the LED grid on the front of the micro:bit if it gets too hot.
You can have the micro:bit show a number, letters, or even an image. Iâm going to show you how to use an image to show us when the temperature changes.
- Select âshow iconâ from the basic blocks.
- Drag the show icon block to 'then' part of the 'if then' statement
- You can choose a pre-defined icon from the list.
- Iâll use a scrunched up face since it looks like itâs too hot. Place it within the then block. Now download your code and run it.
What if we want to sound an alarm if our temperature is too cold as well?
- Click on the settings button on the âif thenâ statement.
- Arrange the âifâ, and âelse ifâ blocks on the white canvas and make sure they snap together.
- Place any extra blocks in the grey area.
Now do the same thing as before, but change the comparator to âless than.â Keep the number as zero. Select âbasic blocksâ and choose an icon. I added a surprised face as the alert if itâs too cold.
Itâs good practice to add comments to your code so you remember what itâs doing.
- Right click the icon that appears before the words on a block. A menu will pop up. Select âAdd Comment.â
- This will cause a question mark icon to appear to the left of the previous icon.
- Click on the question mark and a small yellow box will appear into which you can write your comment.
- Make it informative. âIf the temperature is too cold or too hot, show an led alertâ
If youâd like to show an alert if the temperature isnât too hot or just cold, you can add another âelseâ to our âif thenâ statement.
We use an else here, rather than an âelse ifâ, to instruct our micro:bit what to do if the temperature conditions donât fall within either of the previously defined ranges, greater than 30, or less than 0.
Do the same thing as before. You can copy and paste blocks using the ctrl-c and ctrl-v shortcuts.
If the temperature is just right, weâll show a smiley face. Select a smiley face icon from the icons list.
AlgorithmsOne of the most important things about teaching students to code is teaching them to think logically.
Youâve probably heard of algorithms. Algorithms are basically sets of instructions, or code, that takes some input, performs a series of actions, and spits out an output.
What you just created is an algorithm. Yup, you can go tell your friends that you wrote an algorithm today!
Testing your codeYou can test your code by changing the variables slightly. Change the high temperature to be one degree higher than the current temperature, and the low temperature to be one degree lower than the current temperature. You can only use whole numbers, or integers, as values.
You can use ice or a heater to test your code easily.
Code in the classroomAs you can see, this project could be useful not only for teaching computer science but also for teaching classes like chemisty, or measuring soil temperature in biology, or even creating an interactive art project.
Hopefully you feel more confident about teaching your students to code using the micro:bit. Micro:bit provides several lesson plans for teaching
Resources and going furtherIf you want more project ideas, you can click the âprojectsâ tab at the top of your coding window. Instead of clicking ânewâ click âprojectsâ. This has all of the code for makecode projects. Click on one and it will create the code for you in your window or walk you through a tutorial.
- For more resources, go back to microbit.org/code. Youâll see that micro:bit provides several lesson plans for teaching under âLessons.â
- If youâre looking to deepen your knowledge, the CS intro course is a great place to start.Additional piece about algorithms - use if possible, but cut if too long.
- And of course, if youâre just looking for some inspiration, Hackster has a page full of cool micro:bit projects with complete instructions at hackster.io/microbit.
Comments
Please log in or sign up to comment.