Lesson video

In progress...


Hello, and welcome to lesson 4 in our selection in Physical Computing unit.

I'm Andy and in this lesson, we're going to be starting with selection.

For this lesson, you'll need a crumble starter kit and three AAA batteries.

You'll also need the crumble software installed on your computer.

It would be great if you'd have a pen and paper as well, because you'll be needing to write some things down.

Other than that, please clear away any distractions.

And then we can start.

In this lesson, you'll be able to explain that a loop can be used to repeatedly check if a condition has been met.

You'll also be able to explain that a condition being met can start an action.

You'll identify a condition and an action in a project.

And you'll also use selection within a programme to direct the flow of a programme.

So we're going to start by looking at this.

We have an algorithm and we have a programme below it.

And the first thing I'd like you to do is look at them and see if you can identify the condition in both.

I'll give you a moment to look.

So you're looking for the condition in each.

So in the algorithm we have, "Do until I hear a clap." So that's the condition.

Until I hear a clap.

And in the programme we've got do until, and this A is hi is when the button is pressed.

So the button pressed it makes a circuit in A as hi.

So this one is do until the button is pressed.

Could you look at it again now, please? And I'd like you to think about what happens while the condition is false in each and while the condition is true.

So have a look through them again and we'll look at what happens when conditions are true or false.

Okay, so when the condition is false, so while I don't hear a clap, I tap my nose with my thumb.

And when I do hear a clap, I stop.

With the programme down here, we've got do until the button is pressed.

So while the button isn't pressed, the sparkle will like different colours.

And as soon as a button is pressed, the loop stops and it moves on to the next thing.

So the next thing you're going to do is, I'd like to use the structure below to construct an algorithm that shows the condition needing to be met and the action that needs to be carried out repeatedly.

Okay, so here's an example.

We want to light the sparkle blue, and we want it to stop when the button is pressed.

So here the example is filled in.

We have, do until.

So until the button is pressed, light the LED blue.

Okay, so this is the example of what we want to happen and here's the algorithm for it.

So your first task is to write some conditions and actions, for examples given.

So on the worksheet, there are some things you need to fill in.

So have look at those.

Pause the video now.

Okay, so let's just work through those.

So the first one was to flush the sparkle red and blue and stop when the button is pressed.

So the condition is the button being pressed.

So we have, is the button pressed? So do until button is pressed.

And while it's not pressed, light the LED red, wait, light it blue, wait and so on.

When the button is pressed, stop.

You might have chosen different lights but it didn't ask for a specific light, but we're alternating red and blue until the button is pressed.

The next one was to switch the sparkle green and then off every second, so it has a timer in it.

Stop when the button is pressed.

So again, that's do until the button is pressed and we've got, light the led green, wait a second, switch it off, wait a second, and so on.

And it will carry on doing that until the button is pressed.

So that's our algorithm there.

And then here slightly unusual one, it's wait and stop waiting when the button is pressed.

So our condition is the button being pressed and our repeat is wait, and I've put a 10th of a second.

So it's repeatedly checking is the buttons pressed.

And if it's not, it carries on waiting.

So we look at selection now.

Sometimes you want one set of actions to be carried out if the condition is met and another set of actions to be carried out, if the condition is not met.

This is called selection.

So the structure of selection is if and then, and it's used when we give these commands.

So here's an example.

If it is after seven o'clock, then get out of bed and get ready for school.

The condition is the, if part, if it is after seven o'clock and the action to be carried out is, if it is, then get out of bed and get ready for school.

So here's an example.

What condition needs to be met for Nia to put her on umbrella up? And how would you represent that using selection? So I'll give you a moment to think about that.

So if what then what? Okay, so if it's raining, then put the umbrella up.

Look at another example.

What condition might need to be met for someone to have a glass of water and how can we show that with selection? So again, if what then what? I'll give you a moment to look.

Okay, if I'm thirsty, then drink a glass of water.

So there is our selection.

If I am thirsty, then drink a glass of water.

So what other decisions could you make using if-then, and how could they be represented using selection? How could we describe them using if and then? So your task please is to write some if-then statements and there's some ideas there on the worksheet.

So pause the video now and write some if-then statements.

Okay, hope you've got that.

So here's my example.

Here's my algorithm.

So I've got, if the button is pressed, turn sparkle zero to red, turn sparkle one to blue, pause for one second, turn sparkle zero to blue, turn sparkle one to read and pause for one second.

So we are waiting and if the button is pressed, then do this.


So let's all look at this.

So when we're doing selection in a programme, what's the command block? Can you see the command block there? Okay It's the if-then command block.

What's the condition.

So in our if-then, we have a condition and here's the condition, if A is hi.

So if the button is pressed then? And that's it highlighted, and then we've got what actions need to be carried out when a condition is met.

So when the button is pressed, do the following things.

Okay, so let's have a look at a programme using selection.

So before we have a look at that, I'll just show you down here.

Here's my wiring.

So I've changed the colour of the wires a bit 'cause that's the wires I had.

But I have my two sparkles wired up as we've done it before.

And I've got my button connected.

I've just used a green wire here instead of the red one I was using before.

Okay, so that's my setup and here's my programme.

So the programme is, if the button is pressed, then, and I've just gone for one sparkle and the next sparkle zero.

Make it green, wait a second, turn it off, make it red, wait a second, turn it off.

So let's run that programme.

And if I press the button, ah, nothing happens.

Okay, so what's actually happening is the programme starts, it checks to see if the button is pressed.

If it was being pressed, it would do these things.

If it wasn't, then it gets to here and that's it.

It's not checking again.

Okay, so what we need to do is change the programme so it continually checks if the button is pressed.

And for that, we use a do-forever loop.

So I'm going to snap in this do-forever loop and drop my code back in there.

And if I then reprogram the crumble, I see it's successful.

Now we can try it.

So if I press the button, here we are.

Green, red, off.

Press the button again, green, red off.

One last time.

Okay so we've had to use a do-forever loop to continually check if the button is pressed.

Okay, so you're going to implement your algorithm as code.

So that's the algorithm you wrote earlier.

You decided what was going to happen and then the light pattern using the two sparkles.

And you're going to create that code.

You need to remember this do-forever loop.

You want to keep checking whether the button has been pressed or not.

So that's your task.

Write a programme to implement your algorithm.

That's it, pause the video now.

Okay, so here's my algorithm from earlier.

So if the button was pressed, make sparkle zero red and sparkle one blue, wait a second and make sparkle zero blue and then one red.

So they're switching colours and then wait a second.

So this is it as a programme.

So forever check is the button pressed.

If it is, then sparkle zero red, sparkle one blue, wait a second, then switch the colours.

So if we press that.

Let's reprogram the crumble and we can press the button and try it.

There we go.

So blue, red, red, blue, and then it stops.

And that's right, that's what my programme does.

So the button is pressed, set the colours, change the colours.

So it's not going to carry on.

But if I hold the button, we can see what happens then.

Okay, so while I'm holding the button, the condition is true and the lights are alternating.

And if I let go, it should stop.

And it will stop with these colours sparkles zero on blue, sparkle one on red.

Okay, so there we go.

That's it for this lesson.

And what you've done this time is you identified conditions and selection.

You wrote an algorithm that used selection, and then you wrote a programme which used selection and you used a repeat to continually check if the button had been pressed.

So if you'd like to share your work with Oak National, you can do that.

Please ask your parents or carers to do so.

They can share it on Instagram, Facebook, or Twitter, and they need to tag it with @OakNational and #learnwithOak.

That's it for this time, see you next time.