video

Lesson video

In progress...

Loading...

Hello, and welcome to lesson four of this programming unit all about sensing.

This lesson is called finding your way.

And in this lesson, you will turn your microbit first into a compass and then into a navigational device.

It will be useful if you can find somewhere quiet without distractions, get yourself a pen and a piece of paper, and when you're ready, we can begin.

The main objective for this lesson is to use a conditional statement, to compare a variable, to a value.

You will also explain the importance of order and conditions and else if statements.

You'll use an operand such as greater than, less than or equal in an if then statement.

And finally, we will modify a programme to achieve a different outcome.

In this lesson, we're using the compass as a sensor.

So what do we know about compasses already? Here's some questions for you to consider.

What is a compass? Where might we use a compass? Which words are associated with them and how do they work? Pause the video for a few minutes and think about those questions.

Note down any ideas you have on a piece of paper, and when you're ready resume the video.

This is an example of a compass.

A compass uses a magnet to point towards the magnetic North pole.

If you point a compass North, with the point of pointing to North, you will know that this is the direction North and where all the other directions are.

East, South and West.

Compasses have traditionally been used by people wanting to navigate places before newer modern technology, such as satellite navigation or GPS on phones were available.

Although some of those systems do use compasses as well.

They're especially useful for people navigating the seas, where there's no landmarks or no other ways to find out which direction you're pointing in.

Let's take a couple of minutes to look at how we read a compass.

On the compass image on the right of the slide, I'm going to show three arrows.

Your task is when I show the arrows to note down which direction you think they are pointing in.

We'll then have a look at the answers before we move on to the next slide.

So here's the first arrow.

Note down on a piece of paper, which direction you think that arrow is pointing in.

And here is the second arrow.

Note down which direction you think it is pointing now.

And finally, here's the third arrow.

Which direction do you think the is pointing on the compass now? Let's have a look at the answers and hopefully you didn't find that too challenging.

So for the first arrow, it was of course pointing in the direction of North represented by N on the compass.

The second arrow was pointing in that direction of East represented by E.

The third arrow was possibly a little bit more tricky.

It's pointing somewhere between East and South, but because it's nearer to South than it is to East, we say the arrow is pointing in a Southerly direction or S for South.

For the activity that you're going to do on the microbit, we're using four points of the compass.

North, East, South, and West.

Other campuses will have other points such as Northeast, Southeast, Southwest, and Northwest, but we won't be using those today.

The project you're going to complete to make your microbit into a compass we'll read the value of compass heading to change the variable heading.

In this context, compass heading is represented in degrees.

So North is zero or 360 degrees, and then turning clockwise from North every 90 degrees, we get to the next point of the compass.

So East is at 90 degrees.

90 degrees clockwise from North.

South is 190 degrees clockwise from North.

And West is 270 degrees clockwise from North.

Using degrees of turn from North, we can create a range for each direction.

So for example, North is between 315 degrees and 360 and zero degrees and 45 degrees.

East is between 45 degrees and 135 degrees.

South is between 135 degrees and 225 degrees.

Leaving West between 225 degrees and 315 degrees.

The order of a programme is important.

To demonstrate that we're representing the programme here as code on the left hand side and through a flow diagram on the right.

So we're going to assume that the compass is heading is pointing to 40 degrees.

So let's see what that would return by following the code and following the programme flow.

So the compass heading is less than 45 degrees because 40 is less than 45.

Therefore it's going to set the heading to North and we can follow it through on the flow diagram.

So the compass heading is less than 40 degrees that's true.

So the heading will set to North.

Now, what do you think would happen if the order for North and East in the if else, if statement was reversed.

Pause the video and think about that for a couple of minutes before we return and have a look.

So if the order for North and East was reversed, what would happen is that the condition would check that the heading is less than 135 degrees, because that would be first.

It then would go no further.

It will only go down to the next part of the else, if statement, if the condition is false, but because it's true, it would stay at East.

So it would actually display 40 degrees as East, if we reversed the order of those two conditions.

So it's very important, we order our programme in the correct way.

This is the code we looked at previously.

As you can see, it is still part complete.

However, this time you have greyed out the additional blocks you will need to create a functioning compass.

Your task is to use the design, to complete the code and make a compass.

The design is included within the resources.

You will notice that the design is also part complete.

So you will need to fill in parts of the algorithm and parts of the programme flow before you can make your project.

To access this project in this state, go to ncce.

io/p6b4-1.

that's ncce.

io/p6b4-1.

Pause the video to complete your task.

You need to complete the design and the code to make a compass.

Good luck.

Before you test your code, let's check that the algorithm is accurate.

The two steps you needed to complete were steps four and five.

Step four should read, if the compass heading is less than 225 degrees set the heading to South.

And then step five should read, if the compass heading is less than 315 degrees set the heading to West.

If you need to review your algorithm, pause the video and do that now.

You may then also need to make changes to your code.

Now you can test the code on the emulator.

When you're confident it's working if you have a microbit itself, you can also flush the programme to the microbit and test it on the device itself.

Does the display change when you point in different directions? Pause the video and test your code.

So was your code accurate? This is the order the block should have been placed.

So in order, the heading should be North, East, South and West, and then the final else is North.

That's because the first if statement covers North from zero to 45 degrees, and then the final one covers 315 to 316 degrees.

Finally, you can see at the bottom, there is the show string heading block, which displays the heading on the microbit.

If you need to review your code, go back to your project now.

Now you're going to create a project which will turn the microbiome into a device which can give you directions.

For this project, you're going to be given the design, broken down into the task, the variable, what will be displayed, the algorithm and the programme flow.

So this is exactly the same design format as you used on the previous project.

This is how your navigational device should work.

So as you can see here, the microbit is currently pointing to zero degrees.

So on the display we've got North, I'm going to rotate it clockwise so that it's now pointing to 64 degrees, and when it points to 64 degrees, it's telling me that in order to go back to North, I need to go back to the left or anticlockwise.

If I go the other way so once I go beyond 315 degrees, so now it's 293 degrees.

So of less than 315 degrees, it's now telling me I need to rotate clockwise or turn right to get back into North.

And then once I'm back to North, it will display whoops too far there.

Once I get back to North, it will display North again.

Pause the video to complete the task of making a navigational device.

Use the design you've been given to create your project, test it and compare it with the example you've seen on the screen.

Good luck.

Did you manage to successfully complete your project? Here's a possible code solution.

As you can see, the code is quite similar to what we made the compass.

But the conditional statements have been adapted to turn it into a navigational device.

If you need to make changes to your project, use this code and go back to your project now.

There are many ways we could modify this project.

We could change it so that instead of directing you North, it directs you in a different direction.

For example, East or South, or as we have done in this example, we could make the device more accurate.

To do that we've narrowed the band that points to North.

Could you adapt your algorithm and then your code, to reflect these changes and make your points are more accurate.

Pause the video and see what changes you can make.

I'd like to end this lesson by thinking about what else we could programme a microbit to do.

So far in this unit, you've used the buttons as input to display images and messages.

In lesson two, we made the microbit into a fortune teller using the pick random block.

We use the accelerometer to sense movements in lesson three.

And in this lesson we used the compass to make a navigational tool.

What else do you think you could make with a microbit? Pause the video and note down any ideas you've got.

Thank you for taking part in this Oak National computing lesson.

We'd love to see some of your projects.

If you'd like to please ask your parent or carer to share work on Instagram, Facebook, or Twitter using @OakNational and #LearnwithOak.