# Lesson video

In progress...

Loading...

Hello, and welcome to lesson three of our selection in physical computing unit.

I'm Andy and in this lesson, we'll be controlling things with conditions.

For this lesson, you'll need a Crumble starter kit and three AA batteries.

You'll need the Crumble software installed on your computer.

Having a pen and paper to hand would be really useful as well.

So if we can clear away any distractions, then we can start.

In this lesson, you'll be able to explain that a loop can stop when a condition is met.

Next, you'll be able to explain that a condition is something that can be either true or false.

You'll experiment with a to-do until loop.

And you'll programme a microcontroller to respond to an input.

So the first thing we've got is responding to actions.

So while I show the revealed action, you need to do it.

I'll be watching carefully to make sure you do.

Okay, so the first one.

Do two nods.

Do four nose touches.

Do three claps.

Okay, so what was different about the three commands you were given.

How were they different from each other? What information did you need to carry out those commands? How did you use this information? I'll give you a moment to think.

Okay, so the three different commands you were given, you had to do something a different number of times and you had a different action to do.

What information did you need? Well, you needed to know a number and you needed to know an action.

And how did you use it? Well, you followed those instructions and you did what the instructions said.

So if we look at this now, we've got do four nose touches in the box and we've got some code underneath it.

I'll just give you a moment to look at it.

The code says do four times, set sparkle zero.

Wait a second.

Turn the sparkle off.

Wait a second.

And that's in a loop and above it is do four nose touches.

So are there any similarities between the command you were just given and the count-controlled loop, which we can see underneath? I'll just give you a moment to think about that.

Okay, well, they were both loops, weren't they? You had to do something repeatedly.

So you had to touch your nose four times and the count-controlled loops was to do the instructions in that loops four times.

So both of them wanted something to be repeated four times.

So we're going to look at using conditions and what a condition is.

A condition is a statement that needs to be met to allow something else to happen.

If a condition is met, it's true.

If the condition is not met, it's false.

So what conditions did you use to carry out those actions? Just give you a moment to think.

Okay, so the condition was two.

You were doing two nods, okay? After you'd done two, you stopped.

So we'll look at using conditions.

So think about these phrases and if they are true, you would do them.

So if you could read the first phrase, I have an older sibling.

If that was true, stand up, tap your head three times and then sit down.

Here's another condition.

So this condition is I walked down some stairs this morning.

If you did, if that's true, stand up, spin round twice, then sit down.

Here's the last one.

Okay, so if this condition is true, we'll read it out.

I would like extra homework this week.

If that's true, clap your hands five times.

Okay, so we've got some different conditions and we have some actions.

So when the condition is true, when it's met, we do the action.

So here's some examples.

We have questions like do you have a pet? Do you like strawberries? Have you ridden a bike this week? Would you like to finish school at 2 p.

m.

on Fridays? And for each of those conditions, we could have an action if it was true.

So let's hit the first one again.

Do you have a pet? And you could decide that if the person has a pet, they should nod, okay? So the condition being true would mean the person nods.

So your first task is to write out a condition and an action to carry out.

So pause the video now.

Okay, so let's look through that together.

So the example was Samir was trying to write a condition and an action to carry out if the condition was true.

And he wrote what's your favourite flavour of ice cream? And the response was to do a nod.

Well, you can't actually get somebody's favourite flavour by them nodding.

So we needed to ask a different question.

Samir's question couldn't be used for a condition.

So a better condition might have been if you like ice cream, do a nod.

Or if your favourite flavour of ice cream is chocolate, do a nod, okay? So we need to use conditions, which are true or false or can only be answered yes or no.

So let's just look at this.

We're looking at conditions in do until loops.

So we'll look at using conditions in the loop.

So here's the statement.

I can act out heads, shoulders, knees and toes until my partner taps their nose.

So what condition is used? I'll give you a moment to think.

What's the condition? What needs to be true? Okay, so the condition is my partner tapping their nose.

So what do they do when the condition is false? So while my partner's not tapping their nose, what should I do? Okay.

So while they're not tapping their nose, they should be acting out heads, shoulders, knees and toes.

And the last question is what do you do when the condition is true? So you're acting out heads, shoulders, knees and toes, when their partner taps their nose, it's then true, what do you do? You stop, you stop the actions.

So when the condition is true, you stop.

So we're going to look at do until written as code.

So with the example we were just looking at, we had act out heads, shoulders, knees and toes until your partner taps their nose.

So we had the condition and the action.

So what is the condition? The condition is partner taps their nose.

The action to be repeated while the condition is false.

So while my partner is not tapping their nose, I should be doing heads, shoulders, knees and toes actions.

And what condition needs to be true for me to stop? The true is that my partner actually taps their nose.

Okay.

So your task now is to look at some statements and turn those into do until loops yourself.

So there's four statements and there's a do until structure that you can use on the worksheet.

So if you would like to pause the video now, and have a look at that.

Okay, so let's look at those together.

So the first one was I'm allowed to read until 9 p.

m.

So what was the condition? The condition was until 9 p.

m.

So here it is.

Do until the time is 9 p.

m.

So while it wasn't 9 p.

m, while it was false, what should we be doing? So that was to read my book.

And when the condition was true, so when the time was 9 p.

m, you had to stop reading.

Let's look at the next example.

I can play until the bell rings.

So the condition is the bell ringing.

So we have it here.

Do until the bell rings.

So while the bell isn't ringing, while it's false, we can play.

And when the bell rings, we stop.

So that loop would carry on, play, play, play until the bell rings and then stop.

Here's the next example.

We have to go home when it gets dark.

So the condition is when it gets dark.

Okay, so the action is repeating while it isn't.

So while the sky isn't dark, you can stay out and then when the sky is dark, we can't do that anymore, so we have to go in.

And here's the last example.

At pedestrian crossings, the light stays green for traffic until someone wants to cross.

So the condition is is the button pressed, okay? So while the button isn't pressed, the green light's on and the cars and the traffic can move.

When the button is pressed, the condition is true and this repeat has to stop, okay? So let's move on to looking at using do until with inputs.

We see our little code snippet here.

And it's got programme start and it says do until, and we'll explain this in a minute, A is HI.

So there's a loop there and it does something until that becomes true.

So this programme uses a push switch being pressed as a condition.

So this is how the push switch code works.

So when the switch is pressed, the condition is true.

The circuit's described as high.

When it's not pressed, the circuit condition is described as low.

So while the button isn't pressed, make the sparkle green.

And as soon as the button is pressed, button is pressed, the circuit goes HI and the loop stops.

So we can wire a push switch in with our Crumble and with the sparkle.

So here's a diagram connecting things up.

And you've done the battery pack and the sparkle before and the new bit is down here.

This is the push switch.

So the push switch is connected to the positive on the battery.

And onto connection A.

If you recall from the previous slide, it said when A is high, that's the A it was talking about, and there's our push switch.

So your task is to connect up your Crumble with a push switch and a sparkle.

Okay, so pause the video now.

Okay, I hope you've got that all wired up.

So don't forget to switch your battery back on and it should also be connected to your computer to programme it.

So what you're going to do is write a programme that uses a condition to stop a repeating light pattern.

So let's just look at it here.

We've got the programme start and we've got do until.

So this is the check as to whether the button has been pressed.

While the button isn't pressed, this is what happens.

So this is where your code goes.

So in here, you can write some code to light a sparkle different colours.

And it's a loop, so it will keep doing what's in there until the button's pressed.

So what you're going to do is use the code, it's shown on the worksheet and the bit that you change is here.

So you choose what your pattern is, you run your code and then your light pattern will keep changing until you press the button and then when you press the button, that condition is met and your pattern stops.

So you're going to write a programme using a condition and when the condition is met, the button is pressed.

It will stop a repeating light pattern.

So pause the video now.

Okay, I hope you managed that.

So here's my code.

So I've got the do until the condition A being HI.

So A on the Crumble is HI because the button's been pressed.

And here's my pattern.

It goes to different colours and it's 4.

1 of a second each time and it keeps cycling.

So if I run that now, we can see the sparkle flashing.

So we've got the sparkle flashing.

If I go and press the button, let's see if it stops.

Okay, it's stopped.

Let's try it again.

Okay, it's flashing, press the button and it didn't stop this time.

So why didn't it stop? I've pressed the button twice, it's not stopped.

If we go back and look at this, it's going through these steps, so if I press the button when it's here or here, the button press isn't registering.

Okay, the button press is only checking once every loop.

So what I might need to do is I might need to hold the button down.

And then it will stop.

Okay, so if we think about how it's been used, that will change whether the button works or appears not to work.

Okay, so there we go.

In this lesson, you identified when a condition was true or false.

You wrote statements using do until and you wrote programmes that used an input as a condition.

So as always, if you'd like to share your work with Oak National, that might be little videos of your sparkles working or screen grabs, if you'd like to share it, please ask your parent or carer to share your work on Instagram, Facebook or Twitter, tagging it with @OakNational and #LearnwithOak.

See ya next time.