温度のアクティビティー

このアクティビティーではデバイスセンサーを入力として使う方法を学びます。

  • Get started
  • MakeCode
  • Inputs
  • Variables
  • Sensors
Print lesson
  • 年齢 8+
  • 30 分
  • MakeCode Editor

はじめに

This project uses one of the on-board sensors and a gesture control to display the real world temperature.

Teacher Guide

Open Open teacher resources

Activity

4 steps

ステップ 1

In the MakeCode editor select the Input option from the block menu and find the “On shake” block and drag onto the workspace.

Drag the 'on shake' block to the workspace

『~とき』ブロックはワークスペース上で、色々なジェスチャーに変更ができます。例えば、『右に傾けたとき』とか『左に傾けたとき』などです。このプログラムでは、『ゆさぶられたとき』にします。

Click the arrow to change the gesture

input.onGesture(Gesture.Shake, function () {

})
          

ステップ 2

Now the micro:bit is programmed so it will respond to a shake gesture the next part is to create a variable that the temperature will be stored in. From the block menu select the “Variable” and click “Make a Variable”.

A popup dialogue box will then ask you to name your variable. In this example name it “Temp”.

Click 'ok' to change the variable name

The block “Temp” now appears in the Variable option menu. When the micro:bit is shaken we would like the temperature to be stored in the variable “Temp”. To achieve this, we select the “Set item to” block and drag into the workspace.

Drag the 'set item' block to the workspace

Click the arrow next to 'item' and select 'Temp'.

Now when the micro:bit is shaken the variable “Temp” will be set to 0.


let temp = 0
input.onGesture(Gesture.Shake, function () {
    temp = 0
})
          

ステップ 3

So that the variable “Temp” is set to the actual temperature we need to use the temperature input from the “Input” option from the block menu.

Drag the 'temperature' block to the workspace

Drag this onto the workspace and set it to replace the “0” so that when the micro:bit is shaken the “Temp” variable is set to the Temperature input from the micro:bit sensor.


let temp = 0
input.onGesture(Gesture.Shake, function () {
    temp = input.temperature()
})
          

ステップ 4

Now that the variable “Temp” has been set to the actual temperature using the micro:bit sensor, it now must be displayed so the user can see the value. Select the “Show number” block from the basic menu and drag it onto the workspace.

Drag the 'show number' block to the workspace

Place the “show number” block under the set Temp block in the shake input block.


let temp = 0
input.onGesture(Gesture.Shake, function () {
    temp = input.temperature()
    basic.showNumber(0)
})
          

So that the temperature is shown on the LEDs the “Temp” variable is needed again. This is found from the Variable option from the block menu.

Drag the 'temp' block from the variables section

Place the “Temp” variable in the show number block so the value of the temperature is displayed on the LEDs.


let temp = 0
input.onGesture(Gesture.Shake, function () {
    temp = input.temperature()
    basic.showNumber(temp)
})
          

Save the project and download to the micro:bit.

Challenge

Try to add the following functionality:

  • Display text following a temperature reading to make it easier for the user
  • Display a temperature logo
  • Change the temperature for Celsius and Fahrenheit following a button press or a different gesture.

Learn More

See the 'Die Roll' activity, which uses the gesture command again but adds control of the LED screen and introduces IF statements.

Selecting this opens external content from our support system, which adheres to their privacy policy.