# Lesson video

In progress...

Hello and welcome to lesson six in our selection and physical computing unit.

I'm Andy and in this lesson, you're going to be writing an algorithm and then turning that algorithm into code, to programme the Crumble to control the model that you've made.

For this lesson, you will need the Crumble starter kit and three AA batteries.

You'll also need your model that you made in the previous lesson, and you will need the Crumble software installed on your computer.

You should get yourself a pen and paper as well, because you'll need to jot some things down.

Other than that, please clear away any distractions, and then we can start.

In this lesson, you're going to create a controllable system that uses selection.

You're going to write an algorithm to control the lights and motor.

You're going to use selection to achieve the outcome that you want, and you're going to test and debug the code that you write.

Firstly, we're going to have a look at some code.

So this is a bit of code and there's a number of statements made about it, and you need to decide if those statements are true or false.

So, in your worksheet there is the code and some statements.

So please pause the video, have a look at those statements and decide if they're true or false.

Pause the video now.

Okay, so let's have a look at each of those statements then.

So the first one, only two output devices are used.

We've got motor one, we have sparkle zero and sparkle one.

So, that's three so that's not true.

There's three devices, not to two, that's false.

Both sparkles change colour at the same time.

So, the sparkle has changed colour here next to each other at the same time, and again here at the same time.

So, that's true.

All output devices are turned off.

So, the motor is on here.

The sparkle is switched on here and here and they remain on here.

The sparkles go off, but the motor's not turned off, so that's false.

Selection is used.

So, we've got an if then statement.

So, that is selection, so that's true.

And then lastly, we've got the condition will be checked repeatedly.

So, here's our condition.

If the button is pressed and it's checked once.

There's no repeatedly around that, so that's false.

So, this is what we've done so far.

You're going to be controlling your model using selection and at least two output devices.

And so far you've drawn your model, you've create the wiring diagram.

You've made the structure and you connected the Crumble parts to it.

So, that's how far we've got.

The next thing you're going to do now is write an algorithm for how your model is going to work.

So, you need to use selection so you need to have an if then statement in your algorithm and you need to check if that condition has repeatedly been met and you need to check that the devices are turned off at the end.

So, there's a few things to check and then your algorithm, you need to outline what does happen if the condition is met and then how the whole thing kind of ends.

You need to write an algorithm for controlling your model.

There's further guidance on the worksheet.

Okay, so here's my algorithm and I've got the selection here.

If the button is pressed is my selection and I've got this forever statement to mean continually check if the button is pressed.

Button was down here, remember? So, if the button is pressed, flush the LED green three times.

So, here's my sparkle the LED, then put the motor on forwards first 75% and wait five seconds.

That means the motor will spin forwards for five seconds and then make the motor go backwards and then do that for five seconds, and after that time is up, stop the motor.

So, the motor goes forward, backwards and then when it's stopped, flash the LED red three times.

So, I'm using the LED to kind of show the start and the end of the ride.

If you need to make any tweaks to your algorithm, now would be a good time to do that.

So, now you've got your algorithm and you're happy with it, it's time to write the code.

So, use your algorithm as a guide and get coding.

So, pause the video now.

So, here's my algorithm and the code beside it.

So, I've got this forever check if the button is pressed and here it is, do forever.

If the button is pressed, that's my selection, flash the LED green three times.

So, I've gone for repeat three.

So, three times light the LED, wait a second, turn it off, wait a second.

So, that's the three flashes of the LED.

Turn the motor forward at 75%, that's it there.

Wait for five seconds and then make it go in reverse, wait for five seconds and stop.

So, that all matches the algorithm.

And then lastly make the LED flash red for three times.

So there it is, do three times red off, red off, and that's it.

So, every time the button is pressed, it should go through this sequence.

Let's have a look at it running.

Okay, so here's my model and here's my programme, and you can see the code there.

If I click on the green triangle, that will send the code to the Crumble and it says it's ready to go.

And I've got my forever loop checking if the button is pressed, so let's press the button and see what happens.

There we go.

Green light flashes one, two, three.

Motor forwards at 75%.

Motor reverses, motor stops and then the sparkle flashes red three times.

Because it's continually checking, if I press the button again, it should run again.

Green light flashing one, two, three.

Motor on.

Reverse, we have a bump there.

Motor stop, and the LED flashes three times.

So there we go, that's the algorithm implemented as code.

We've got selection in our programme.

It's checking if the button is pressed and then the chairoplane runs.

I'm sure there's one or two little tweaks, I could make to this.

I might have a pause before it turns backwards.

Maybe I should stop the motor, rather than just throwing it into reverse.

I might have different colours flashing as the chairoplane goes round.

So, there's all sorts of things I could do to develop it further, but I've met the objective that we had, which was to include selection.

So, please take some time now.

So, have a look at your project to check your coding at your algorithm, to do any testing that you need to test to iron out any bugs, fix any issues, also any changes you want to make.

So, spend a bit of time looking through what you've done and get it just as you want to, okay? Pause the video now.

So, in this lesson, you wrote an algorithm to meet the requirements of the task that you had from the previous lesson.

You turned your algorithm into code, you implemented it as code, and then you tested and debugged and you evaluated your algorithm to check it did everything you wanted and everything the task needed it to.

Okay, so that's it for this lesson and for this unit.