D - If Hot
β
β
In this section, we are taking our code to a new level. As humans, we see, smell, touch, taste and hear the world around us, and we are going to use some of those senses to help the micro:bit do something similar, by utilizing different sensors built into it.
The first sensor we'll work with is the ability that the micro:bit has to detect temperature and along with it, we'll learn the concept of if statements in coding.
If statements provide computers with the ability to choose between different options or paths. It opens many different possibilities and makes our programs more interesting and diverse.
We don't need a physical micro:bit to test the temperature option. The emulator provides a way for us to see and test different temperature levels.
βTemperature
Getting a temperature reading from the micro:bit is very simple. We use the provided block temperature (C), wrapped by the show number block.
Don't forget to test your progress using the micro:bit emulator, or a micro:bit, if you have one available.
βIf Statements - Conditionals
If-else statements are used to control what the code will execute when a particular condition is fulfilled. In a way, they provide options for our code to make choices.
Add the if block to our existing code

Add the comparison block located in the Logic section

Complete the if block by:
Adding the temperature block
Change the < (less than) sign in the middle to be > (greater than)
Add a number that would indicate a hot temperature

By now you know the routine: test the application in the emulator or on an actual micro:bit if you have one available!
These projects βββwill help you when working on the main Activities as they all use micro:bits and MakeCode as the brains controlling the visualizations and servo motors. It is time to continue, learning about the other tools. See you in the next section 03 - Servo Motor.
πTeacher's Corner - If statements
The if statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement can be executed.. MDN
More about if statements from MakeCode
π Teacher's Corner - Battery pack
his and many other applications created for the micro:bit can be better explored using a battery pack. It is not necessary but gives mobility to the micro:bit.
micro:bit battery pack info
Last updated
Was this helpful?