Coding
Setting up the Code Area
-
Select and drag the
on start
block to the left of the screen and drop it on the bin. -
Select and drag the
forever
block to the left of the screen and drop it on the bin. -
From the Input menu, select and drag the
on shake
block to the code area and drop it. -
From the Input menu, select and drag a
on button A pressed
block to the code area and drop it. -
Right-click on the
on button A pressed
block and select duplicate.On the duplicated block select the little arrow next to A and choose B.
Your code area will now look like this:
Temperature Sensing
-
From the Basic menu, select and drag a
show number
block to the code area and attach it within theon shake
block. -
From the Input menu, select and drag a
temperature
block to the code area and attach it within the 0 of theshow number
block.
We can now sense the temperature around us.
Light Sensing
-
From the Led menu, select and drag a
plot bar graph of
block to the code area and attach it within theon button A pressed
block. -
From the Input menu, select and drag a
light level
block to the code area and attach it within the 0 of theplot bar graph of
block. -
Type 255 within the 0 of the
up to
line.
This will plot a graph on the LED matrix of the amount of light in your environment when the A button is pressed.
Compass Sensing
-
From the Basic menu, select and drag a
show number
block to the code area and attach it within theon button B
pressed. -
From the Input menu, select and drag the
compass heading
block to the code area and attach it within the 0 ofshow number
.
Completed Code
Here is what our completed code looks like:
We are now ready to download the code to our micro:bit so we can go out and sense our environment.