Lesson video

In progress...

Loading...

Hello, my name's Mrs. Jones and I'm pleased you decided to join this lesson Today.

In this lesson, we will look at using the Sense HAT add-on board for the Raspberry Pi and using the emulator to learn to programme, even if you don't have the hardware.

In this lesson, we'll look at using randomly-generated colours for pixels and creating your own fortune-telling ball.

So let's get started.

Welcome to today's lesson.

Today's lesson is called Programming Project, Sense HAT 2, from the unit Programming Strings and Lists.

And by the end of this lesson, you'll be able to randomly-generate pixel colours and use this to create a fortune-telling ball.

There are two keywords to today's lesson.

Append.

Append means add to an existing data structure.

Pixel.

Pixel is a single element of an image on a computer screen.

There are two sections to today's lesson.

The first is randomly generate a pixel colour grid and the second, create a fortune-telling ball.

So let's start with randomly generate a pixel colour grid.

What is a Sense HAT? The Sense HAT is an add-on board for the Raspberry Pi computer that was designed for the Astro Pi space mission.

There are currently two Raspberry Pis with Sense HATs orbiting the Earth on the International Space Station.

You can see the Sense HAT on the top of this Raspberry Pi computer in this photograph.

Let's see some features of the Sense HAT.

You have the eight-by-eight RGB LED matrix display, gyroscope, accelerometer, and magnetometer, temperature and humidity sensor, barometric pressure sensor, and a mini joystick.

In computer science, you often need to manage large amounts of data.

You need to know how this data is stored and the types of operations that you can perform on it.

Data structures are used to store data in an organised and accessible way.

Data structures can be static or dynamic.

Static data structures reserve memory locations for a set amount of data.

Their size cannot change.

Dynamic data structures are more flexible.

The memory capacity is not fixed.

Characteristics of arrays.

An array holds multiple items under one name.

You can see an example here on the right.

The top_ten_pizza_toppings is the name of this array.

An array is a static data structure.

An array is fixed in size and can only contain data of the same type.

You can see in this example, they're all strings or text.

The contents can be changed, but items cannot be added or deleted.

And the size of an array must remain fixed.

Characteristics of lists.

A list holds multiple items under one name and the example on the right here is shopping_list.

A list is a dynamic data structure.

The size of a list can change, which means that you can add and delete items. A list can also contain data of different types.

Let's do a quick check.

Match the keyword to the description.

You have the keywords on the left, append, array, list, pixel, and LED matrix, and you need to match it to the correct description on the right.

Pause the video to consider your answer and then we'll check them.

Let's check your answer.

Append is add to an existing data structure.

Array, a fixed static data structure that holds items of the same data type under one name.

List, a dynamic data structure that holds items under one name.

The items can be of varying data types.

Pixel, a single element of an image on a computer screen.

And LED matrix, a group of LEDs placed in grid structure.

Well done if you've got those correct.

This code creates a list to add colour to individual pixels in the LED matrix.

Line 1 and 2 are the setting up, importing the library and getting things set up.

You have line 4 and 5, setting up the variables r and b to store the colours red and blue.

You have line 7 starting the list, setting up smile as a list, and inside the square brackets, those variables with commas in between, which represents each of the pixels on the matrix display on the right, and on line 18, they have sense.

set_pixels.

And then inside the brackets, the name of the list.

Sofia says, "I don't want to specify the LEDs and the colours that they are set to.

I want this happen randomly, but can I do that?" The random module can be used to randomly assign colours to pixels or to randomly select pixels to assign a colour to.

The choice open and close brackets function can be used to randomly choose a colour from a list.

This code sets all the pixels to either red, yellow, or blue, randomly each time the programme is run.

So you can see at the top on line 2, from random import choice, which is important there to import the library, so we can use choice.

We have red, yellow, and blue variables set up on line 6, 7, and 8.

A list, colours, set up to store those variables red, yellow, and blue.

And then we're using line 12, colour = choice.

And then inside the brackets, the name of the list.

Sense.

clear and then colour inside the brackets sets the entire LED matrix to a single colour.

Let's have a quick check.

Sofia says, "this randomly changes all of the pixels, but I want the individual pixels to be different colours.

What should I do?" Pause the video to consider your answer and then we'll check it.

Let's check your answer.

Sofia could start with an empty list for the LED matrix, then use a for loop to randomly select a colour for each pixel and then add this colour to the list.

Well done if got that correct.

For x in range 64, you can see the code there on line 13, this is a for loop and executes 64 times to randomly assign a colour to each pixel.

So you can see there, line 13.

And then on line 14 and 15 indented.

We have the random colour.

And then on line 15, the append to the, so the new list, the empty list grid, which has been created on line 11.

Let's do the activity.

This code will create a list that contains the colours to draw an alien on the LED matrix.

So you can see the code there that displays that alien on the right, created using the list alien on line 4.

Continue down to line 13.

For this activity, you'll create a programme that displays an alien with randomly changing pixels.

Note, in the Raspberry Pi Sense HAT emulator, the alien will be displayed on its side.

And these are the steps.

One, create variables for each colour that you wish to use.

Two, create a list of the variables for each colour.

Three, create the initial alien.

Four, display the initial single-color alien for two seconds.

Five, create a new list based on the original alien.

Six, if the item in the list is a C, then it should generate a random colour item.

Seven, if the item in the initial list is a B, then it should use the original item.

Eight, light the LED matrix using the newly-generated grid.

And nine, continuously show the new list and the original list for two seconds each.

Pause the video to have a go at that activity.

Go back through the slides if you need to, and then we'll go through the solution.

And let's have a look at the solution.

So you can see the code here on lines 1 to 20 was the initial setup of the alien, but also you'll notice on line 2 and 3 that we needed to import from random import choice and from time import sleep.

On lines 21, 22 and 23, we have the colours green, yellow, and red.

25, we have a colours list for those new variables.

And we also have an empty list on line 26.

On line 28, we have the for pixel in alien.

And inside that for loop, we've got if and else to look at whether it was a B to randomly generate a colour and add it to the list or not.

On line 36, we have while true, so that we've got that repeating for the two seconds interval.

Well done if you've got that correct.

Let's move on to the next part of this lesson.

Create a fortune-telling ball.

A fortune-telling ball is a toy that you ask a closed question to and it'll give you a prediction.

Your fortune-telling ball will have eight different responses to a question.

And these are, signs point to yes.

Without a doubt.

You may rely on it.

Do not count on it.

Looking good.

Cannot predict now.

It is decidedly so.

And outlook not so good.

This code displays a message that scrolls across the screen.

So on line 4 there, you can see sense.

show_message.

And inside the brackets, inside the quote marks, the message, Here is a message.

Let's have a quick check.

How could you store the eight different responses for the programme? Pause the video to consider your answer and then we'll check it.

Let's check your answer.

Sofia says, "I could use a list to store each of the eight different responses.

I could then use the random function to randomly select one of the responses from the list." Well done if you got that correct.

In Python, the syntax for a list is a common-separated list of values, items, in square brackets.

You can see here an example of a list planets, with each of the planet's names inside quote marks, separated by a comma inside the square brackets.

Planets is the identifier.

And you have the list items. In this example, the list items are string literals, pieces of text.

So they need to be in quotation marks.

Let's have a quick check.

In Python, what character do you use to separate each list item? Is it A, a comma, B, full stop, or C, plus sign? Pause the video to consider your answer and then we'll check it.

Let's check your answer.

The answer was A, a comma.

Well done if you got that correct.

Let's do the activity.

Develop your fortune-telling ball programme following these requirements.

A, the user should be able to ask a question.

B, the programme should wait three seconds.

And C, the programme should then reveal one of the eight responses.

And those eight responses are signs point to yes, without a doubt, you may rely on it, do not count on it, looking good, cannot predict now, it is decidedly so, and outlook not so good.

Pause the video to have a go at that activity and then we'll go through the solution.

And let's have a look at the solution.

Here you can see the code here.

You have lines 1, 2, and 3, importing those libraries.

Line 6, we have that initial message, sense.

show_message.

Inside the quote marks, ask a question, scroll_speed equals 0.

06.

And then on line 7, we have sleep.

And inside the brackets, 3.

On line 9, we set up the list that is storing each of those eight possible outcomes, all inside quote marks separated by a comma.

And then on line 19, we have sense.

show_message.

And then inside the brackets, choice, and inside the brackets, replies.

Well done if you got that correct.

For the second part of the activity, it's asking you to extend the fortune-telling ball programme to show an icon on the LED matrix after the message is displayed.

Pause the video to have a go at the extension before we carry on.

Well done for completing the activities.

And I hope you've enjoyed creating your fortune-telling ball.

In summary, static data structures reserve memory locations for a set amount of data.

Their size cannot change.

Dynamic data structures are more reliable.

The memory capacity is not fixed.

The choice open closed brackets function can be used to randomly choose an item from a list.

For example, it could randomly pick a pixel from a list, storing all pixels in an LED matrix.

Well done for completing this activity, Programming Project, Sense HAT 2.