Connect to UWA WiFI
1. Select UnifiGuest wireless network
2. Enter the username and password
Username: perthgpn
Password: 8Sq jRd8?
�3. Accept the Terms of Use.�4. Click the "Log in" button.
Welcome to the Labs
Welcome to the labs!
Tamagotchi! - Micro:bits
Thank you to our Sponsors!
Platinum Sponsor:
Gold Sponsor:
Who are the tutors?
Who are you?
Things in Common
Log on
Tell us you’re here!
Click on the
Start of Day Survey
and fill it in now!
Come to the front to get your ticket for the free prize draw when you’ve finished!
Log on
Click on your Room
You’ll see:
Using the workbook!
The workbooks will help you put your project together!
Each Part of the workbook is made of tasks!
Task 6.1: Make the thing do blah! |
Make your project do blah …. |
Hint |
A clue, an example or some extra information to help you figure out the answer. |
Task 6.2: Add a blah to your code! |
This has instructions on how to do a part of the project
|
Tasks - The parts of your project
Follow the tasks in order to make the project!
Hints - Helpers for your tasks!
Stuck on a task, we might have given you a hint to help you figure it out!��The hints have unrelated examples, or tips. Don’t copy and paste in the code, you’ll end up with something CRAZY!
print('This example is not part of the project’) |
Using the workbook!
The workbooks will help you put your project together!
Check off before you move on from a Part! Do some bonuses while you wait!
★ BONUS 4.3: Do something extra! |
Something to try if you have spare time before the next lecture! |
Checklist - Am I done yet?
Make sure you can tick off every box in this section before you go to the next Part.
Lecture Markers
This tells you you’ll find out how to do things for this section during the names lecture.
CHECKPOINT |
If you can tick all of these off you’re ready to move the next part! ☐ Your program does blah ☐ Your program does blob |
Bonus Activities
Stuck waiting at a lecture marker?
Try a purple bonus. They add extra functionality to your project along the way.
Today’s project!
Tamagotchi- Micro:Bit
Tamagotchi
Tamagotchi
Sadly you can’t keep them at the end of the day. 😥
If you want one for home (maybe for christmas or your birthday!) they’re about $25 .
Find out where to buy them here:
Intro to Micro:Bit
What is a Micro:Bit?
Buttons: We can press these and tell the Micro:Bit to do different things
Lights: We can turn each light on or off to make different images
Pins: These let us connect the Micro:Bit to other devices using wires
Button A
Button B
Lights!
Pins
Front
What is a Micro:Bit?
Reset button
Input
Input: Where we connect the cable from the computer to transfer our code/power to our Micro:Bit
Reset button: Let’s you stop your code and starts it again
Battery connection: You can use your micro:bit even when it is not plugged into your computer! Ask you tutor for a battery pack if you need one.
Accelerometer: The Micro:bit can tell us when it is accelerated - so it knows when we shake it!
Back
Battery connection
Accelerometer
Using python.microbit.org
Go to python.microbit.org
Today we will be using python.microbit.org to program our Micro:Bits.
You should see this page pop up!
python.microbit.org
This is where we code
This is the simulator where we test our code
python.microbit.org
This is the reference. Click on the reference button to help you find the syntax for different instructions.
Click here
How do we write code for it?
Micro:Bits use Python, which is the programming language that we usually teach here at GPN!
Always make sure this line is at the top of your code!
This lets us use lights, sounds, buttons and lots of other cool in our Python code for the Micro:Bit
from microbit import *
The Display
Your Micro:Bit has a 5 x 5 display grid of little red LEDs on the front!
You can do some cool stuff with the display like:
Show an image, like a heart!
Scroll a word across the display, like ‘Hello’
This code is in your python.microbit.org coding space - have a look
It’s indented in a while loop - so it will repeat forever
To show or to scroll….
Remember our microbit has a 5x5 grid of lights.
display.scroll('Hi!')
display.show(Image.SAD)
So if you want to display long words or messages, use scroll. If you want to display something that fits in a 5x5 grid, use show!
Using the Simulator
Restart
Stop
We can run our code on the Simulator or the real micro:bit!
Stop, Restart, Simulator settings are underneath
Connect the Micro:Bit
Run the code on the Micro:Bit (Chrome/Edge)
It’s fun to mess around with the Micro:Bit on the simulator.
Now let’s see your code on a Micro:Bit in real life!
Run your code on your Micro:Bit like this
You should see a HEART displayed for 1 second and then HELLO (repeating)
Want your code to start again? Press black “reset” button on the back
Chrome or Edge
Run the code on the Micro:Bit (other browser)
This is for if you don’t have the Chrome or Edge browser (eg Safari)
Run your code on your Micro:Bit like this
You should see a HEART displayed for 1 second and then HELLO (repeating)
Want your code to start again? Press black “reset” button on the back
Comments
Have a look at the code in the coding space - can you see the purple comments lines starting with the #
# This code was written by Alex
Mistakes are Great! Errors on the Micro:bit!
I ❤️ errors!
We can learn from our mistakes!
2. What went wrong
1. Where the error is
Project Time!
Let’s use our MicroBit!
Try Parts 0 & 1 of your Workbook!� The tutors will be around to help!
You’ve already done the first task!
Remember … to get to your workbook …
Intro to Programming
What is programming?
It’s giving computers a set of instructions!
Programming is not a bunch of crazy numbers!
A Special Language
A language to talk to dogs!
Programming is a language to talk to computers
People are smart! Computers are dumb!
Programming is creating a set of instructions, like a recipe.
Computers do EXACTLY what you say, every time.�
Which is great if you give them a good recipe!
People are smart! Computers are dumb!
But if your recipe is wrong e.g. get it out of order….��A computer wouldn’t know this recipe was wrong.
It would still try to make it anyway!��
People are smart! Computers are dumb!
Computers are bad at filling in the gaps!��A computer wouldn’t know something was missing, it would just freak out!
Everyone & everything has strengths!
How is the human brain different from a computer’s brain?
Everyone & everything has strengths!
Variables
No Storing is Boring!
It’s useful to be able to remember things for later!
Computers remember things in "variables"
Variables are like putting things into a labelled cardboard box.
Let’s make our favourite number 8!
In our code we make a variable and set it to a value like this:
fav_num = 8
fav_num
8
Variables
Instead of writing the number 8, we can write fav_num. The computer will substitute the fav_num’s current value.
1. fav_num - 6
2. fav_num * 2
3. fav_num + 21
4. fav_num / 2
fav_num
Variables
Instead of writing the number 8, we can write fav_num.
fav_num
1. fav_num - 6
2. fav_num * 2
3. fav_num + 21
4. fav_num / 2
2
Variables
Instead of writing the number 8, we can write fav_num.
fav_num
1. fav_num - 6
2. fav_num * 2
3. fav_num + 21
4. fav_num / 2
16
2
Variables
Instead of writing the number 8, we can write fav_num.
fav_num
1. fav_num - 6
2. fav_num * 2
3. fav_num + 21
4. fav_num / 2
16
2
29
Variables
Instead of writing the number 8, we can write fav_num.
fav_num
1. fav_num - 6
2. fav_num * 2
3. fav_num + 21
4. fav_num / 2
16
2
29
4
Variables
Variables are useful for storing things that change
Variables contain data that "vary" - hence the word "variable".
Let’s change fav_num to 102.
fav_num = 102
fav_num
102
Variables
We're able to use our code for a new purpose, without rewriting everything:
fav_num
1. fav_num - 6
2. fav_num * 2
204
96
3. fav_num + 21
4. fav_num / 2
123
51
Reusing variables
We can replace values in variables and show it:
animal = "dog"
display.scroll(animal)
animal = "cat"
display.scroll(animal)
animal = animal + "dog"
display.scroll(animal)
What will this scroll?
Reusing variables
We can replace values in variables and show it:
animal = "dog"
display.scroll(animal)
animal = "cat"
display.scroll(animal)
animal = animal + "dog"
display.scroll(animal)
What will this scroll?
Reusing variables
We can replace values in variables and show it:
animal = "dog"
display.scroll(animal)
animal = "cat"
display.scroll(animal)
animal = animal + "dog"
display.scroll(animal)
What will this scroll?
Reusing variables
We can replace values in variables and show it:
animal = "dog"
display.scroll(animal)
animal = "cat"
display.scroll(animal)
animal = animal + "dog"
display.scroll(animal)
What will this scroll?
Variables
Your turn!
Can you guess what each display.scroll will do?
x = 3
display.scroll(x)
3
display.scroll(x + x)
6
y = x
display.scroll(y)
3
y = y + 1
display.scroll(y)
4
Variables
Your turn!
Can you guess what each display.scroll will do?
x = 3
display.scroll(x)
3
display.scroll(x + x)
6
y = x
display.scroll(y)
3
y = y + 1
display.scroll(y)
4
Variables
Your turn!
Can you guess what each display.scroll will do?
x = 3
display.scroll(x)
3
display.scroll(x + x)
6
y = x
display.scroll(y)
3
y = y + 1
display.scroll(y)
4
Variables
Your turn!
Can you guess what each display.scroll will do?
x = 3
display.scroll(x)
3
display.scroll(x + x)
6
y = x
display.scroll(y)
3
y = y + 1
display.scroll(y)
4
Variables
Your turn!
Can you guess what each display.scroll will do?
x = 3
display.scroll(x)
3
display.scroll(x + x)
6
y = x
display.scroll(y)
3
y = y + 1
display.scroll(y)
4
Strings and Ints - Data Types!
Strings can have any characters in them, even just spaces!
Strings are surrounded by quotes (“ or ‘)�
"Hello, world!" "bla bla bla" “abcd1234”
":)" " "'I can use single quotes too!'
Integers are whole numbers in python - no quotes (“)
We can do maths with integers but not strings.
1 0 22 954865746
Strings and Ints!
Sometimes we need to turn a string into an integer and vice versa so we can use them as we want to in the code
You’ll be doing this in your code!
Don’t worry if you forget. Tutors
will help!
Strings and Variable names
The difference between strings and variable names can be confusing! Let’s take a look at an example.
What do you think is shown?
animal = “dog”
display.scroll(animal)
display.scroll(“animal”)
Strings and Variable names
The difference between strings and variable names can be confusing! Let’s take a look at an example.
What do you think is shown?
animal = “dog”
display.scroll(animal)
display.scroll(“animal”)
No quotes = uses the value of the variable!
Quotes = uses the word in the quotes!
Strings and Variable names
The difference between strings and variable names can be confusing! Let’s take a look at an example.
What do you think is shown?
animal = “dog”
display.scroll(animal)
display.scroll(“animal”)
No quotes = uses the value of the variable!
Quotes = uses the word in the quotes!
Strings and Variable names
The difference between strings and variable names can be confusing! Let’s take a look at an example.
What do you think is shown?
animal = “dog”
display.scroll(animal)
display.scroll(“animal”)
No quotes = uses the value of the variable!
Quotes = uses the word in the quotes!
Scroll… Scroll… Scroll… on the micro:bit
Words are too big to display within a 5x5 grid of lights.
Remember we can display words with display.scroll().
Sometimes the text scrolls across too slowly - you can speed it up with delay.
A smaller delay (eg 100 results in faster scrolling).
The default speed is 150!
display.scroll('Hello World')
display.scroll('Hello World', delay=100)
Multiple Instructions
What happens if we want to change the speed AND join variables with strings?��This is how you would do it! :)
Without a sleep, the computer will run through the code so quickly, and we will only see a CONFUSED face.
See that we need to use str( ) to convert the number win_count to a string before we can join it (+) with the the other string!
win_count = 3
display.scroll('Wins: '+ str(win_count), delay=75)
Message
delay
Sleep… zzz! … on the micro:bit
Computers are really fast, sometimes our program moves too quickly to enjoy it!
For example:
We can slow it down by using sleep()
Sleep is done in milliseconds (so the number of seconds x 1000)
display.show(Image.HAPPY)
sleep(1000)
display.show(Image.SAD)
sleep(1000)
display.show(Image.CONFUSED)
sleep(1000)
Without a sleep, the computer will run through the code so quickly, and we will only see a CONFUSED face.
Project Time!
Let’s use our MicroBit!
Try Part 2 of your Workbook!�� The tutors will be around to help!
While Loops
Loops
Humans know how to do things on repeat!
Sometimes we want to do some code on repeat!
While True: Loop
Micro:bit programs use a while True: loop to run code again and again forever
All code indented under while True: runs again and again
The only code we want to have before the while loop are things we only want to happen once - like setting variables or saying Welcome
This code is indented
If Statements
Conditions!
Conditions let us make a decision.
First we test if the condition is met!
If it is, then we do something
If it’s raining take an umbrella
Yep it’s raining
…... so take an umbrella
Booleans (True and False)
Computers store whether a condition is met in the form of �True and False
To figure out if something is True or False we do a comparison
"Dog" == "dog"
"D" in "Dog"
"Q" not in "Cat"
5 < 10
3 + 2 == 5
5 != 5
Booleans (True and False)
Computers store whether a condition is met in the form of �True and False
To figure out if something is True or False we do a comparison
"Dog" == "dog"
"D" in "Dog"
"Q" not in "Cat"
5 < 10
3 + 2 == 5
5 != 5
True
Booleans (True and False)
Computers store whether a condition is met in the form of �True and False
To figure out if something is True or False we do a comparison
"Dog" == "dog"
"D" in "Dog"
"Q" not in "Cat"
5 < 10
3 + 2 == 5
5 != 5
True
True
Booleans (True and False)
Computers store whether a condition is met in the form of �True and False
To figure out if something is True or False we do a comparison
"Dog" == "dog"
"D" in "Dog"
"Q" not in "Cat"
5 < 10
3 + 2 == 5
5 != 5
True
True
False
Booleans (True and False)
Computers store whether a condition is met in the form of �True and False
To figure out if something is True or False we do a comparison
"Dog" == "dog"
"D" in "Dog"
"Q" not in "Cat"
5 < 10
3 + 2 == 5
5 != 5
True
True
False
False
Booleans (True and False)
Computers store whether a condition is met in the form of �True and False
To figure out if something is True or False we do a comparison
"Dog" == "dog"
"D" in "Dog"
"Q" not in "Cat"
5 < 10
3 + 2 == 5
5 != 5
True
True
False
False
True
Booleans (True and False)
Computers store whether a condition is met in the form of �True and False
To figure out if something is True or False we do a comparison
"Dog" == "dog"
"D" in "Dog"
"Q" not in "Cat"
5 < 10
3 + 2 == 5
5 != 5
True
True
False
False
True
True
Conditions
So to know whether to do something, find out if it’s True!
fave_num = 5
if fave_num < 10:
display.scroll("that’s a small number")
Conditions
So to know whether to do something, they find out if it’s True!
fave_num = 5
if fave_num < 10:
display.scroll("that’s a small number")
That’s the condition!
Conditions
So to know whether to do something, they find out if it’s True!
fave_num = 5
if fave_num < 10:
display.scroll("that’s a small number")
That’s the condition!
Is it True that fave_num is less than 10?
Conditions
So to know whether to do something, they find out if it’s True!
fave_num = 5
if fave_num < 10:
display.scroll("that’s a small number")
True
Put in the answer to the question
Is it True that fave_num is less than 10?
Conditions
So to know whether to do something, they find out if it’s True!
What do you think happens?�>>>
fave_num = 5
if fave_num < 10:
display.scroll("that’s a small number")
True
Conditions
So to know whether to do something, they find out if it’s True!
What do you think happens?�that’s a small number
scrolls on the micro:bit
fave_num = 5
if fave_num < 10:
display.scroll("that’s a small number")
True
If statements
fave_num = 5
if fave_num < 10:
display.scroll("that’s a small number")
This line …
… controls this line
If statements
fave_num = 5
if fave_num < 10:
display.scroll("that’s a small number")
display.scroll("and I like that")
display.scroll("A LOT!!")
This line …
… controls anything indented below it!
Actually …..
If statements
fave_num = 5
if fave_num < 10:
display.scroll("that’s a small number")
display.scroll("and I like that")
display.scroll("A LOT!!")
What do you think happens?�
If statements
fave_num = 5
if fave_num < 10:
display.scroll("that’s a small number")
display.scroll("and I like that")
display.scroll("A LOT!!")
that’s a small number and I like that A LOT!!
scrolls across the micro:bit
If statements
word = "GPN"
if word == "GPN":
display.scroll("GPN is awesome!")
What happens?
If statements
word = "GPN"
if word == "GPN":
display.scroll("GPN is awesome!")
What happens?
GPN is awesome!
scrolls across the micro:bit
If statements
word = "GPN"
if word == "GPN":
display.scroll("GPN is awesome!")
What happens?
GPN is awesome!
scrolls across the micro:bit
But what if we want something different to happen if the word isn’t "GPN"
Else statements
word = "Chocolate"
if word == "GPN":
display.scroll("GPN is awesome!")
else:
display.scroll("The word isn’t GPN :(")
What happens?
else
statements means something still happens if the if statement was False
Else statements
word = "Chocolate"
if word == "GPN":
display.scroll("GPN is awesome!")
else:
display.scroll("The word isn’t GPN :(")
What happens?
The word isn’t GPN :(
scrolls across the micro:bit
else
statements means something still happens if the if statement was False
Elif statements
word = "Chocolate"
if word == "GPN":
display.scroll("GPN is awesome!")
elif word == "Chocolate":
display.scroll("YUMMM Chocolate!")
else:
display.scroll("The word isn’t GPN :(")
What happens?
elif
Means we can give specific instructions for other words
Elif statements
word = "Chocolate"
if word == "GPN":
print("GPN is awesome!")
elif word == "Chocolate":
print("YUMMM Chocolate!")
else:
print("The word isn’t GPN :(")
What happens?
YUMMM Chocolate!
Scrolls across the microbit
word = "Chocolate"
if word == "GPN":
display.scroll("GPN is awesome!")
elif word == "Chocolate":
display.scroll("YUMMM Chocolate!")
else:
display.scroll("The word isn’t GPN :(")
elif
Means we can give specific instructions for other words
Remember ….
word = "Chocolate"
if word == "GPN":
display.scroll("GPN is awesome!")
elif word == "Chocolate":
display.scroll("YUMMM Chocolate!")
else:
display.scroll("The word isn’t GPN :(")
:
At end of each
if,elif,else line to
say you have finished writing your condition
==
When testing for equals in your condition
Micro:Bit Inputs
Buttons!
Your Micro:Bit has 2 buttons: Button A and Button B
You can use this code to check if a button is pressed:
The statement will be TRUE if the button is being pressed at that time and it will be FALSE if it is not being pressed
if button_a.was_pressed():
if button_b.was_pressed():
Buttons!
What do you think this code does?
If button a is pressed when the Micro:Bit gets to this line of code then what happens?
If button b is pressed when the Micro:Bit gets to this line of code then what happens
What do you think happens if both button a AND button b are being pressed?
The Micro:Bit shows a Sad face
if button_a.was_pressed():
display.show(Image.HAPPY)
if button_b.was_pressed():
display.show(Image.SAD)
Buttons!
What do you think this code does?
If button a is pressed when the Micro:Bit gets to this line of code then what happens?
If button b is pressed when the Micro:Bit gets to this line of code then what happens
What do you think happens if both button a AND button b are being pressed?
The Micro:Bit shows a Sad face
if button_a.was_pressed():
display.show(Image.HAPPY)
if button_b.was_pressed():
display.show(Image.SAD)
The Micro:Bit shows a Happy face
Buttons!
What do you think this code does?
If button a is pressed when the Micro:Bit gets to this line of code then what happens?
If button b is pressed when the Micro:Bit gets to this line of code then what happens
What do you think happens if both button a AND button b are being pressed?
The Micro:Bit shows a Sad face
if button_a.was_pressed():
display.show(Image.HAPPY)
if button_b.was_pressed():
display.show(Image.SAD)
The Micro:Bit shows a Happy face
Buttons!
What do you think this code does?
If button a is pressed when the Micro:Bit gets to this line of code then what happens?
If button b is pressed when the Micro:Bit gets to this line of code then what happens
What do you think happens if both button a AND button b are being pressed?
The Micro:Bit shows a Sad face
It will show one face and then the other immediately
if button_a.was_pressed():
display.show(Image.HAPPY)
if button_b.was_pressed():
display.show(Image.SAD)
The Micro:Bit shows a Happy face
Accelerometer!
Your micro:bit has a motion sensor (accelerometer).
This sensor has the ability to detect when you move it.
We can check if the micro:bit has been shaken like this:
We can check if the micro:bit has been turned face down like this:
You’ll be doing this in your code!
�
while True:
if accelerometer.was_gesture('shake'):
Information from the sensor
while True:
if accelerometer.was_gesture('face down'):
Accelerometer!
What do you think this code does?
while True:
if accelerometer.was_gesture('shake'):
display.scroll('I’m getting dizzy')
Accelerometer!
What do you think this code does?
It will display I’m getting dizzy every time the micro:bit is shaken
while True:
if accelerometer.was_gesture('shake'):
display.scroll('I’m getting dizzy')
OR
What if you wanted to check if the weather was raining or snowing, and take an umbrella for either possibility.
weather = "snowing"
if weather == "raining" or weather == "snowing":
display.scroll("Take an umbrella!")
What happens?
>>>
OR
What if you wanted to check if the weather was raining or snowing, and take an umbrella for either possibility.
weather = "snowing"
if weather == "raining" or weather == "snowing":
display.scroll("Take an umbrella!")
What happens?
>>>
What happens?
Take an umbrella!
Indentation
Remember …
When we have an if statement or while loop, we indent the code that should run if the condition is true.
Indenting means we push the code that has to run to the right so it sits ‘under’ the while or if line.
Indentation tells the code that a piece of code is "inside" another
while True:
if num>10:
display.scroll('a big number')
These gaps are indentation!
We say ‘the if statement is indented under the while’
‘ the display.scroll is indented under the if’
But how do we indent?
There a couple ways to indent a line of code:
Remember you need to indent for your code to work right!
Conditions on the micro:bit
You will be using if statements to test if certain things happen and then do something
Project Time!
while we’re here:
�See if you can do Part 3 & 4
� The tutors will be around to help!
Time
Running Time
Sometimes you want to time things. Like, for example, if you wanted to put a time limit on a game and see how many points you can get in 30 seconds!
To figure out how long the Micro:Bit program has been running (in milliseconds) you can use this command:
What would running_time() be after 4 seconds?
What about after 10 and a half seconds?
4000
10,500
time = running_time()
Running Time
Sometimes you want to time things. Like, for example, if you wanted to put a time limit on a game and see how many points you can get in 30 seconds!
To figure out how long the Micro:Bit program has been running (in milliseconds) you can use this command:
What would running_time() be after 4 seconds?
What about after 10 and a half seconds?
4000
10,500
time = running_time()
Running Time
Sometimes you want to time things. Like, for example, if you wanted to put a time limit on a game and see how many points you can get in 30 seconds!
To figure out how long the Micro:Bit program has been running (in milliseconds) you can use this command:
What would running_time() be after 4 seconds?
What about after 10 and a half seconds?
4000
10,500
time = running_time()
Running Time
Give me a break!
Sometimes we might want to stop looping through our
while True: loop
That’s when we use the break keyword!
from microbit import *
while True:
display.show(Image.HAPPY)
if button_a.is_pressed():
break
display.clear()
Project Time!
Now you know how to pass the time …
Try to do Parts 5 - 7!
And Extensions 8 & 9 if you want!
� The tutors will be around to help!
Tell us what you think!
Click on the
End of Day Form
and fill it in now!