Lesson video

In progress...

Loading...

Hello everyone, my name is Mrs. Jenkins.

I can't wait to learn with you today.

Welcome to today's lesson which is called Sensing Inputs.

It is taken from the unit: Sensing Movement with Physical Computing.

Today's lesson, you will be able to update a variable based on the state of an input.

During today's lesson, we have got three key words.

Those key words will appear throughout the lesson, so it's really important we know what they mean before we get started.

Those three words are input, sensor, and variable.

An input is data that is sent to a programme to be processed.

A sensor is an input device that allows a computer to collect data from the physical environment.

And variable is a named piece of data stored in a computer's memory, which can be accessed and changed by a computer programme.

Keep an eye out for those words as we move through the lesson.

Remember they are input, sensor, and variable.

In today's lesson, Sensing Inputs, we have two learning cycles.

We have Explore how sensors detect physical input and Use inputs to change a variable's value.

We're going to start with Explore how sensors detect physical input.

Jun asks us, "What is an input?" Inputs come from sensors like buttons or a light sensor that detects changes.

Programmes can access and process the information from these inputs.

Have a look at these two pictures of the micro:bit.

We have the front and the back.

How many different inputs can you see on the micro:bit? Have a go.

How did you get on? So I asked you how many different inputs could you see on the micro:bit? And here, there are probably more than you realise.

We have on the front first, which is the front, we have button A, a touch logo, button B, a microphone, and a light sensor.

On the back, we have got the reset button, a temperature sensor, an accelerometer, a compass.

And we have across the front and back, the GPIO pins.

Your programme can receive inputs from the buttons and use these to be the trigger to change or update a variable.

So we'll see later on, where we'll use a code block called when button A or button B is pressed.

Okay, let's have a little check in here.

Which of these is an input on the micro:bit? A, an LED display; B, button A; or C, a speaker.

What do you think? Have a go.

How did you get on? I asked you which of these is an input on the micro:bit.

A, an LED display; B, button A; or C, a speaker.

The answer is button A.

Button A is an input on the micro:bit.

Great job, everyone.

A variable is a value that can be set and changed throughout the running of a programme.

So here, the variable is the red block and it says on start, set count to 0.

Izzy is asking us, "What is the first thing you need to do when working with a variable?" And Jun has replied, "A variable should be set or initialised at the beginning of a programme." In MakeCode, you can use the on start block to set your variable.

This means it will be reset every time you start the programme.

So that's really important.

So on this block here, it says: on start, set count to 0.

So even if previously you have counted up to a really high number, when you press start again, the count will go back to 0.

Okay, let's have another check in here.

True or false? A variable should be set to a starting value at the beginning of a programme.

What do you think? Have a go.

How did you get on? I asked you, true or false? A variable should be set to a starting value at the beginning of a programme.

The answer is true.

A variable should be set to a starting value at the beginning of a programme.

That could be if you were having lives, you might set them to 3 or 5.

If you were setting a score, you want it to be back at 0.

This is important because the programme needs a value to begin with.

Great job, everyone.

We're going to start on task A now.

I would like you to follow this flow chart and create the programme in MakeCode, which is the oak.

link/makecode.

Remember, you will need a forever loop.

So the code goes set count to 0.

Is button A pressed? If true, increase count by 1.

Is button B pressed? If true, decrease count by 1.

Here, we have it slightly bigger for you to follow.

Have a go.

How did you get on? I asked you to follow the programme flow and create the code in MakeCode.

So our programme flow went set count to 0.

Is button A pressed? If true, increase count by 1.

Is button B pressed? If true, decrease count by 1.

Here is our code that we would need.

Remember we did discuss we needed a forever loop around the whole code and this means it works all the time.

So we have forever.

If button A is pressed, then change count by 1.

Show the number count.

So that will show us what number we have counted to.

If button B is pressed, then change count by -1 and show number count.

So again, it will show us the number it has counted each time because we have put that show number count.

Great job, everyone.

Let's move on to learning cycle two: Use inputs to change a variable's value.

Izzy asks, "Are the buttons the only inputs for the micro:bit?" And Jun has explained, "No, there are lots of different inputs on the micro:bit." I wonder if you can remember any of those inputs.

Here are our inputs on the micro:bit.

We have button A, touch logo, button B, GPIO pins, microphone, light sensor, reset button, temperature sensor, accelerometer, and compass.

Well done if you remembered any of those.

The micro:bit contains an accelerometer.

This is a sensor that detects and measures movement.

That is.

gesture block can be used to detect movement from the accelerometer.

So we have is shake gesture.

So if the micro:bit is shaken, the accelerometer will detect movement.

This block can be found in the input menu.

How could this programme be adapted so that a shake movement will trigger an action? Have a think.

What did you think? I asked, how could this programme be adapted so that a shake movement will trigger an action? One way would be to replace the if button A is pressed block with the if.

is shake gesture block, then block.

Make sure it goes in the hexagonal space.

So it says if it is a shake gesture, then change count by 1 and show number count.

So this means when the micro:bit is shaken, the count will increase by 1 and the number will display.

The second part hasn't been changed.

So it says if button B is pressed, then change count by -1 and show number count.

So the only bit of code that is changed is the when button A is pressed has been swapped out for the is shake gesture.

I've changed the programme again here.

What changes can you see this time? Have a go.

How did you get on? I explained I had changed the programme.

What changes could you see? Now the programme won't automatically display the count variable after each button press.

So it says if button A is pressed, change count by 1, but it does not say show the number count.

And it says if button B is pressed, then change the count by -1.

But again, it does not now say show the number count.

Instead, I have added another block at the end that says if it is shake gesture, then show the number count.

So to make it count 1, we press button A.

To make it take away 1, we press button B.

If we want to see how many we have counted, we will shake the micro:bit and it will display the number counted.

Let's have a little check now.

What do you need to do to the micro:bit to make it show the count variable? Have a think.

How did you get on? I asked you, what do you need to do to the micro:bit to make it show the count variable? And you would need to shake the micro:bit or shake the emulator.

And then, it would show the number counted.

Great job, everyone.

Watch this video.

Does the count variable change when it is checked by the programme? Izzy says, "The count variable does not change when it is checked by the programme.

Shaking the micro:bit displays the count variable.

It does not change or alter the count variable." So remember, the shake gesture just shows the number.

It doesn't change it at all.

"Pressing button A or button B will change the count variable." Okay, let's have another check in here.

True or false? A variable is changed every time it is used.

Have a go.

How did you get on? I asked you, true or false? A variable is changed every time it is used.

It is false.

A variable only changes when something tells the programme to change it, like a button press.

Just using the variable doesn't change it.

Great job, everyone.

We are going to move onto task B now.

And it says different conditions can be added to our existing counter programme.

Can you add to the programme so that when both A and B are pressed, the count variable is reset to 0? And can you explain what the programme does? Have a go.

How did you get on? I explained different conditions can be added to our existing counter programme.

And I asked, can you add to the programme so that when both A and B are pressed, the count variable is reset to 0 and can you explain what the programme does? This is one of the ways you could add the new condition.

So we here forever.

If button A is pressed, then change the count by 1.

If button B is pressed, then change the count by -1.

If shake gesture, then show the number count.

If button A and B is pressed, then set count to 0.

So then we can explain what it does by explaining pressing the A button adds 1 to the count.

Pressing B takes 1 away from the count.

Shaking displays the count.

And pressing A and B sets the count at 0.

Great job, everyone.

You have worked really hard in today's lesson, well done.

Let's summarise what we have learned today.

Inputs come from sensors like buttons or a light sensor that detect changes.

Programmes can access and process the information from these inputs.

A variable is a value that can be set and changed throughout the running of a programme.

A variable's value can be changed according to a condition.

Checking the value of a variable doesn't change the variable's value.

Well done, everyone.

You have worked extremely hard in today's lesson.

I hope to learn with you again soon.