# Lesson video

In progress...

Hello, and welcome to lesson two of our Selection in Physical Computing unit.

I'm Andy, and in this lesson we're going to be combining output devices.

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

Ideally, it would also be great if you had a motor.

You'll also need to have the Crumble software installed on your computer and having something to to write on and something to write with would be a really good idea.

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

In this lesson, you're going to write a programme that includes count-controlled loops.

You're going to connect more than one output device to the microcontroller.

You're going to design sequences for the things that you've connected, and you're going to decide which output devices to control with count-controlled loops.

So think back to the last lesson and what we did there, we connected some things up just keep your moments is here if you can remember what all those things were called.

Okay so in the middle we had the Crumble, the microcontroller itself.

We had power from three double A batteries, connected with two wires, a positive and a negative connection.

And over here we have the sparkle, which was connected with three wires, that was a positive and negative for the power, and this yellow lead here was for the signal to let our programme control the LED.

We also had this lead, which connected it to a computer, allowing us to write a programme and send it to the controller.

So, if you could look at this animation for a moment- it's going to countdown 3, 2, 1.

And watch, it flashes once, twice, three times.

Okay, so a little animation playing on repeat, it's not the programme.

Which of these two sequences would make that happen- would make the LED flash three times? I'll give you a moment to look at them and see if you can work out which one it is.

Look through both bits of code.

Okay so this piece of code it lights Sparkle once, turns it off, lights it twice, turns it off, lights it a third time, and turns it off.

This one actually sets the sparkle on, waits a second, turns it off, and it's in a forever loop so it keeps going on and off and on and off forever.

So it's this programme which is making this sequence happen.

So what you're going to do in a moment is you're going to connect another, an additional output device.

So last lesson, we connected the Crumble to one sparkle, and if we wanted to, we can actually daisy chain.

We can connect another sparkle so we can actually have- this would be sparkle zero and this would be sparkle one.

So we can have two sparkles connected.

We can also connect a motor.

And so motors connect just with two wires, and they connect on these two sides down here.

In this example it's connected to one side.

So, that's the wiring diagrams to show you how they're connected.

These two are the same two that go to the battery pack, they're not shown on here but if you recall last lesson we connected the battery pack and these show you how to connect the additional things.

If you've got all three things, if you've got two sparkles and a motor, you can do that.

There's a worksheet with a diagram on showing you how it connects.

So, if you'd like to pause the video now, you can connect those things up.

Okay, so, this is the same setup that we had in the last lesson.

I've already got the battery pack connected to the Crumble and the Crumble connected to the sparkle.

And I've got the wires matching, positive to positive, negative to negative, positive to positive, negative to negative, and then the D, the signal cable, is in there and also the lead connected to the computer.

So first I'm going to add another sparkle, so I've got another three leads and I've got another sparkle.

So I need to make sure the sparkle is the right way up, and then I can actually match the colouring.

So I'll start with the red wire, and I connect to the existing sparkle, and I connect onto the new one, and it needs to be the same way up.

So I need to look at it carefully, and get that connected the same way up.

So that's one connection.

Turn that over.

I need to connect the negative here.

Make sure that's on straight.

And then I adjust the right around, the next connection.

Okay.

And then I've got the last connector to put in.

The signal connector.

And then, again, I connect that- I need to make sure I've got it connected to the right one.

And, hopefully, if I lay that down, you can then see, I've got my connectors.

First sets here, and then there's another set connecting onto the next sparkle.

And I've matched the same D to D, plus to plus, minus to minus.

Okay so that's the additional sparkle connected.

And then we've got the motor.

So the motor comes with two leads on it already, and it's the same idea really.

So if we go look at the Crumble, we have.

We can connect to either side, I'm going with side one, motor one.

There's a positive and a negative, and we've been using red for positive, so I'm going to connect this red to the positive.

And then it means the other one goes to the negative.

Okay.

So we've got a lot of bits connected there.

We have the batteries, we have the motor, and then we have one sparkle, two sparkles, and I mentioned before, this is sparkle zero when we programme it, and this is sparkle one.

Okay, so hope that's all connected.

If you need to go back and check any of your connections you can do that.

Okay, so we've got everything connected up.

There's sparkle zero, sparkle one, and I've put a wheel onto the motor to make it easier to see.

So, you need to double check that you have the lead connecting it to the computer and it's plugged into the computer and double check that your batteries are on, otherwise it won't have any power and it won't work.

So, if we now add some code, we'll just check it all works.

So I'll use a programme start, and we'll just start with set sparkle zero to red.

Press run, there we go, that's one sparkle lit up.

So I'm going to add another one now which says set sparkle, and we can click in on the number and put sparkle one, that's the other sparkle.

And we'll leave it as red and we'll just do stop, and play, you've now got one, two sparkles lit up.

And then lastly, we're going to add the motor.

So if I click motor, and it's got motor one, and just remember that I connected it to side one, so that's motor one, side one.

Go forwards at 75% of its speed.

So if we now run that, I had both LEDs lit and I had the motor going.

So just so you can see the other things that are available, we'll just put some gaps in.

So I'm going to set sparkle zero first, and then I'm going to set sparkle one, so sparkle zero, wait a second, sparkle one, wait a second.

Then I'm going to set- I've got a few choices here.

I'm going to turn sparkle zero off and I'm also going to turn sparkle one off.

So I'll just click in there and type in a one.

Here we go, so they come on a second apart and go off after one second together.

The motor, let's have another wait.

Put that in there.

And then we have the motor forwards, we're going to wait a second, we'll put the motor one- stays as one, and we can click and it goes to reverse, we'll wait another second, and we'll add the motor again, and if I keep clicking it goes to stop.

One click is forwards, two is reverse, three is stop.

So, let's try that then.

So, first sparkle in the chain, sparkle zero, then sparkle one, second one in the chain, turn them both off, wait a second, make the motor go forward, wait a second, make it go backwards, wait a second, stop the motor.

Let's try all that then.

Go, spark one, two, they're off.

Motor goes briefly a second, backwards and forwards.

Okay, so that's it.

That's everything we've got connected programmable and working.

So now you've seen the different ways we can programme the Crumble to control the additional things we've connected to it.

If you want to go and try some of those things out for yourself you can do that.

You might want to pause the video temporarily to have a go.

Your next task is going to be to try out the four programmes shown below.

They each do different things and they control the things that we've connected.

I'd like you to look through the programmes, try and predict what they will do, have a think, what do you think they'll do, and then actually try them out and see what actually happens and note that down.

So, you're going to have a look at the worksheet, look through the programmes, try and predict what they will do, and then try them out and note down what happens, so pause the video now.

So let's try those programmes out and check what happens.

So this is programme one, and it was to set sparkle zero to red, wait a second, to amber, wait a second, green, wait a second, off.

So this is sparkle zero, the first one in the chain.

And we'll run it and see.

So it should go red, amber, green, off.

Perfect.

There we go, so that was the first programme.

The second programme is this one, and it was to set sparkle one this time.

So sparkle one is the next one down the chain.

And it was to go green for two seconds, red for two seconds, green for two seconds, red for two seconds, and off.

So, green one two, red one two, green one two, red one two, and off.

That's it.

And this is controlling the motor, we can see the wheel here attached to the motor.

And it should go forwards for a second and then forwards at slightly faster speed, and then go backwards, and then stop with a second gap between each, let's try that one.

There we go, so you could hear the speed change, and then it went backwards.

So that was the motor going forwards and backwards with one second gaps.

And then lastly, this one should make everything do something.

So sparkle zero, this one, should go red.

Sparkle one should go blue, the motor should go forwards for five seconds, at 75% of its speed, and then it turns the sparkle zero off, sparkle one off, then stop.

Let's try that one.

There we go.

So that was all the programmes that you were given to try out.

Hope you got them all working and they did what you've just seen.

You need to use at least two different output devices, so it could be two sparkles or a sparkle and a motor, to create a little effect for a disco.

So you can choose between the flashing lights or you can use a motor.

You need to choose the commands for each device and you need to put those commands into a sequence.

So, there's your task, so write a programme that controls two devices that would make a light effect from a disco.

So pause the video now.

Okay, I hoped you enjoyed doing that, here's my programme.

I've got the um, sparkles only, that's all I went for, and I've just gone for different colours for half a second, so we'll just run that and you can see it running.

Run it a couple of times.

So the next thing I'd like you to do is to have a look at this, so over here we have some code, we have a programme.

And we have an algorithm here, or some statements from an algorithm.

Your task is to look at these and work out, of this code, which ones have these been used and in which order were they used.

So that's your task, have a look at the code and work out which parts of the algorithm were used, and in which order.

So pause the video now.

Okay, I hope you followed that through, so we'll just look through it together.

So, the first instruction is motor one reverse at 75%.

So if we look through these, there's one here, "change motor one to clockwise, set speed to 75%" and this one doesn't have any number so I think it must be that one.

So that's the first instruction.

Set sparkle zero to green, we've got a change sparkle to green here.

We've got a wait, so wait a second.

That's that one.

Turn sparkle zero off, there we are that was that one, turn it off.

And then we've got another pause.

So there we go.

So that was our five algorithmic statements matched to our programme.

So in a moment you're going to modify your programmes to make the sparkles flash more than once.

So this programme here used a form of repetition that we looked at in the last lesson.

I'll give you a moment to see if you can remember what it was called.

So this was a do forever loop, it's an infinite loop cause it will carry on forever.

We're now going to have a look at a different sort of repetition.

These are count-controlled loops and we use them when we know how often we want something to happen rather than carry on forever.

We may want it to happen so many times.

So this example is do ten times what's in the loop, and then it will carry on with the rest of the programme.

So here is my programme from before, I'm going to modify it using a count-controlled loop.

So I'm going to simplify it as well, I don't need all these instructions cause I'm going to use this repeat.

So I'll get rid of those, I might choose to take that bit off, I might choose to use some of these bits as well.

So I'll take those and I'll get rid of the rest.

I now need to add a do, and I need a different sort of loop for this one, so I need to go into control, and it's do so many times.

So I'm going to do this one, do ten times, and if I put that on there, and I can snap it in, and I can change this to be five if I want, so I'll put five in there, I'm then going to add another one, so I can put that in there, and I can do this five times as well.

I'm going to put those in.

And if I wanted to I could turn them off at the end.

So let's now try that out.

So it should flash reds and blues and then greens and purples and then stop.

We'll just run it one more time, reds and blues, moves on to greens and purples, and then it stops.

There we go.

I'd like you to now pause the video and go back to your programme that you've got and create a count-controlled loops to control some parts of your programme.

So pause the video now.

Okay I hope you had fun with that, using count-controlled loops to make different things happen a different number of times.

In this lesson you've connected an additional sparkle and a motor to a Crumble controller.

You designed a sequence that used more than one Crumble component.

And you used count-controlled loops as part of your programme.

If you'd like to share your work with Oak National, that can be done you can share screenshots or little videos of your things moving and lighting up.