Loading...
Hello, my name is Ben and welcome to lesson three of this programming unit, which is all about sensing.
Today, we'll be using the micro:bit to look at different ways in which it can sense inputs.
We'll use the buttons initially to sense some input and then we'll move on to using the accelerometer to sense movement.
You'll need somewhere quiet, free of distractions to complete this lesson and a pen and a piece of paper would also be really useful.
When you're ready, let's begin.
Let's start by looking at the objectives for this lesson.
So the overall objective is that you will update a variable with a user input.
In doing that, you will use a conditional statement to change a variable, experiment with different physical inputs, and explain that if you read a variable, the value remains.
Let's look at the inputs on a micro:bit.
So either using the handout, which has the front and rear images of the micro:bit, which you can see on the screen here, or if you have a micro:bit itself, you can look at the device itself.
What inputs do you think there are on the micro:bit? Pause the video, have a look at it, and record your ideas on the handout.
There are more inputs than you think.
There's a couple that you'll be familiar with from the previous lesson.
So button A and button B can both be used as inputs, and you can also combine button A and B together to make a third input.
There's also the accelerometer, which we looked at in the previous lesson, and we'll be using today and the compass, which we mentioned too.
They're combined in this tiny little chip on the rear of the micro:bit.
There is a light sensor which actually sits within the led lights on the front of the micro:bit.
And there is a temperature sensor, which is within the processor on the rear of the micro:bit.
And finally, we have the GPIO pins, and that stands for general-purpose input/output pins.
We'll not be using those in this unit, but they enable you to connect peripheral devices.
So outside devices to the micro:bit using crocodile clips along these tiny little connections here along the bottom, so that's the GPIO pins.
And finally, we have a reset button on the rear of the micro:bit near the top.
So there are lots and lots of inputs we can use on a micro:bit.
For this task, you're going to be changing a variable using the buttons on the micro:bit.
So we'll be using buttons A and B.
We're going to use selection to make the micro:bit into a counter.
Because we're using selection, we're going to use an if then block.
That means we can't use the on button A input block.
So that's why you see it there with a big cross on the screen.
We're not going to be using that block.
The counter is going to count up and down, depending on which button has been pressed.
And it's going to display the value once the button's been pressed.
And just to remind you, the event should be triggered by selection, so you're using an if then block.
This is what your final project should look like on the micro:bit.
So if I click button A, the counter increases by one each time I press button A and displays the value on the screen.
If I click button B, the counter decreases and also shows the value as it decreases.
On the handout, you've been provided with the design for this project.
The design is broken down into several sections.
You have on the top-left the task section.
This describes what the project should do.
You then have the variable, explains which variable you need in the project, and what that variable should achieve.
And on the right hand side, we've got what will be displayed.
So in this case, on the led lights on the front of the micro:bit together with a description.
On the following page, you have the algorithm for the project and the programme flow represented in the diagram.
Now, go to makecode.
microbit.
org, create a new project and use the design to create your code.
Resume the video once you've finished.
Good luck.
For a simple micro:bit based counter project, this is what your code could look like.
I've used one forever block and then placed two if then blocks inside it.
On button A being pressed, the count is going to change by one.
And the number is going to be displayed by displaying the variable count.
And when button B is pressed, the count will change by minus one.
And again, the number will be shown by displaying the variable count.
You may have presented this in two forever blocks.
That's fine, that would also work.
Did you remember to use an on start block to set your count to zero? You need to make sure that's in your project so that every time you start the counter, it will start from zero.
If you need to, go back to your project and make any changes.
For the next activity, to make the code a little easier to read.
We're going to split it into two forever blocks.
You may have already done this in the first activity.
So if you have, you need to skip this step.
If you haven't, pause the video and split out your code into two forever loops, like you can see on the screen here.
To develop your project, we could add a second condition.
We could use this second condition to make the counter more meaningful.
For example, we could turn it into a counter which could tell you if a classroom is full or empty.
If there are more than 30 people in the classroom, we could say the classroom is full.
If there's nobody in the classroom, the classroom is empty.
Can you add a second condition inside your if then statement to show whether the classroom is full or empty.
As before, you have a design sheet to help you.
So use that to help you create your code.
This is how you could include a second condition in your code.
So as before, I've got two forever blocks, each with an if then statement for button A or button B being pressed.
I've now added a second if then statement within the first one.
So for button B, if the count is less than one, I'm going to show the string empty.
So anything less than one would be zero or a negative number.
Or for the button A block, so if the count is greater than 29, so 30 or more, we're going to show a string which says full.
Have a look at the code you've created, and if you need to, use some ideas from this code to make some changes to yours.
Pause the video while you do this.
Up to now, you've used the buttons to change the value of count up or down.
We could use the accelerometer instead and use movement to change the value.
Using the is gesture block from the input set of blocks, adapt your code so that a movement will either trigger an event to change the count by one or minus one.
I'm now going to show you how you can combine your two blocks of code back into one.
To do that, we're going to need an if then, else if block.
So from the logic blocks, I'm going to choose an if then else block.
We need to add in another else, which we're now going to remove, which will give us the else if block.
So now I have an if then, else if block.
Now what I'm going to do is take the condition from the first set of code.
Just try that again, there we go.
And drop that in there.
And the code from inside the if then block.
And drop that in there.
And I'm going to do the same on the other side.
So there's the condition going in, and I'll just move that across so we can see it all and the code going in.
Okay, so move that up.
And what we need finally is one of the forever loops to go back around the code.
There we go.
Okay, there we go.
We can look at that on the emulator.
So A is increasing the count, B is decreasing the count, and when I get to zero, I'll get the message empty.
Pause the video now, and have a go at combining your code into one if then, else if statement.
Resume the video once you are done.
The programme you have just made reads a variable and then displays a message or no message, depending on the value of that variable.
When you read the value of a variable, the value remains.
To change the value of the variable, there must be an input like a button press or a shake.
Thank you for taking part in this lesson with Oak National.
We'd love to see some of the work you've produced in this lesson.
If you'd like to, ask your parent or carer to share your work using Instagram, Facebook, or Twitter, tagging @OakNational and #LearnwithOak.