1 of 120

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.

Check you can access girlsprogramming.network/workshop python.microbit.org

2 of 120

Welcome to the Labs

Welcome to the labs!

Tamagotchi! - Micro:bits

3 of 120

Thank you to our Sponsors!

Platinum Sponsor:

Gold Sponsor:

4 of 120

Who are the tutors?

5 of 120

Who are you?

6 of 120

Things in Common

  1. Get in a group of 3-5 people

  • Try to find things you all have in common! For example…
    • Does everyone have a brother?
    • Does everyone have a pet cat?
    • Does everyone have the same favourite colour?

  • Write what you have in common on the board.

  • Switch groups, play again!�

7 of 120

Log on

Log on and jump on the GPN website

girlsprogramming.network/workshop

Click on your location

8 of 120

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!

9 of 120

Log on

Click on your Room

You’ll see:

  • A link to the Workbook
  • These Slides (to take a look back on or go on ahead)
  • Other helpful bits like a Cheatsheet to help you code

10 of 120

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

  1. Start by doing this part
  2. Then you can do this part

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’)

11 of 120

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.

12 of 120

Today’s project!

Tamagotchi- Micro:Bit

13 of 120

Tamagotchi

  • You’re going to make your own Tamagotchi electronic pet using a micro:bit

  • Tamagotchi pets were a worldwide fad created in Japan in 1996

  • Give your pet a name and write some code to feed it, play with it and let it sleep

  • Don’t let it get hungry, bored or sleepy!

  • Keep it alive, watch it grow and change

14 of 120

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:

https://microbit.org/

15 of 120

Intro to Micro:Bit

16 of 120

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

17 of 120

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

18 of 120

Using python.microbit.org

Today we will be using python.microbit.org to program our Micro:Bits.

You should see this page pop up!

19 of 120

python.microbit.org

This is where we code

This is the simulator where we test our code

20 of 120

python.microbit.org

This is the reference. Click on the reference button to help you find the syntax for different instructions.

Click here

21 of 120

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 *

22 of 120

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

23 of 120

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!

24 of 120

Using the Simulator

  • Click the arrow on the Simulator to run the code
  • A heart is displayed for 1 second and then ‘Hello’

Restart

Stop

We can run our code on the Simulator or the real micro:bit!

Stop, Restart, Simulator settings are underneath

25 of 120

Connect the Micro:Bit

  • Tutors will hand out the micro:bits & cables

  • Connect the small end of the cable to the top of micro:bit

  • Connect the other end to computer USB port

  • New micro:bits will play a “Meet the Microbit” program for you to follow:
    • Push the buttons
    • Shake
    • Tilt to catch flashing LED
    • Clap a few times
  • The tutors will help you

26 of 120

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

  1. Make sure your Micro:Bit is plugged into your computer
  2. Click bottom left
  3. Follow the prompts
  4. Choose your micro:bit and click CONNECT
  5. Wait for the red light on the back of your micro:bit to stop flashing
  6. Your code should be running on the micro:bit!

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

27 of 120

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

  1. Make sure your Micro:Bit is plugged into your computer
  2. Click bottom left
  3. Click Close when you get a popup
  4. Name your project and click Confirm and Save
  5. Follow the instructions on the popup (drag the file from your downloads folder to the MICROBIT device)
  6. Wait for the red light on the back of your micro:bit to stop flashing
  7. Your code should be running on the micro:bit!

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

28 of 120

Comments

  • We use comments to write things in our code for humans!
  • The computer ignores comments
  • Comments start with a #

  • Programmers use comments to explain what their code does
  • You can ‘comment out’ code to stop it from running

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

29 of 120

Mistakes are Great! Errors on the Micro:bit!

  • Programmers make A LOT of errors!
  • Error messages give us hints on how to fix the problem
  • Mistakes don’t break computers!

  • Lots of unexpected words on the micro:bit is an error message
  • Run on the simulator to see it better

I ❤️ errors!

30 of 120

We can learn from our mistakes!

  • In your code - red dot at the start of the line
  • Put the cursor over than line of code to get a hint

2. What went wrong

1. Where the error is

31 of 120

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 …

  1. Go to girlsprogramming.network/workshop
  2. Click on
  3. Click on your room
  4. Click on

32 of 120

Intro to Programming

33 of 120

What is programming?

It’s giving computers a set of instructions!

Programming is not a bunch of crazy numbers!

34 of 120

A Special Language

A language to talk to dogs!

Programming is a language to talk to computers

35 of 120

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!

36 of 120

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!��

37 of 120

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!

38 of 120

Everyone & everything has strengths!

How is the human brain different from a computer’s brain?

39 of 120

Everyone & everything has strengths!

  • Only does exactly what humans tell it
  • Does it the same way every time
  • Will work endlessly
  • Really good at being repetitive
  • REALLY fast
  • Get smarter when humans tell it how
  • Understand instructions very well despite spelling mistakes or typos
  • Solve hard problems
  • Invent computers and tell them what to do!
  • Get smarter by learning

40 of 120

Variables

41 of 120

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

42 of 120

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

43 of 120

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

44 of 120

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

45 of 120

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

46 of 120

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

47 of 120

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

48 of 120

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

49 of 120

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?

50 of 120

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?

51 of 120

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?

52 of 120

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?

53 of 120

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

54 of 120

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

55 of 120

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

56 of 120

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

57 of 120

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

58 of 120

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

59 of 120

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

  • we can turn a string into an integer using int()

  • we can turn an integer into a string using str()

You’ll be doing this in your code!

Don’t worry if you forget. Tutors

will help!

60 of 120

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”)

61 of 120

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!

62 of 120

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!

63 of 120

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!

64 of 120

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)

65 of 120

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

66 of 120

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.

67 of 120

Project Time!

Let’s use our MicroBit!

Try Part 2 of your Workbook!�� The tutors will be around to help!

68 of 120

While Loops

69 of 120

Loops

Humans know how to do things on repeat!

Sometimes we want to do some code on repeat!

70 of 120

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

71 of 120

If Statements

72 of 120

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

73 of 120

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

74 of 120

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

75 of 120

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

76 of 120

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

77 of 120

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

78 of 120

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

79 of 120

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

80 of 120

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")

81 of 120

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!

82 of 120

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?

  • Well, fave_num is 5
  • And it’s True that 5 is less than 10
  • So it is True!

83 of 120

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?

  • Well, fave_num is 5
  • And it’s True that 5 is less than 10
  • So it is True!

84 of 120

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

85 of 120

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

86 of 120

If statements

fave_num = 5

if fave_num < 10:

display.scroll("that’s a small number")

This line …

… controls this line

87 of 120

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 …..

88 of 120

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?

89 of 120

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

90 of 120

If statements

word = "GPN"

if word == "GPN":

display.scroll("GPN is awesome!")

What happens?

91 of 120

If statements

word = "GPN"

if word == "GPN":

display.scroll("GPN is awesome!")

What happens?

GPN is awesome!

scrolls across the micro:bit

92 of 120

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"

93 of 120

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

94 of 120

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

95 of 120

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

96 of 120

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

97 of 120

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

98 of 120

Micro:Bit Inputs

99 of 120

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():

100 of 120

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)

101 of 120

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

102 of 120

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

103 of 120

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

104 of 120

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'):

105 of 120

Accelerometer!

What do you think this code does?

while True:

if accelerometer.was_gesture('shake'):

display.scroll('I’m getting dizzy')

106 of 120

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')

107 of 120

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?

>>>

108 of 120

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!

109 of 120

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’

110 of 120

But how do we indent?

There a couple ways to indent a line of code:

  • Press the TAB button on your keyboard before a line of code.

  • Select all the lines you want to indent and press the TAB button to indent them all at once.

  • Select all the lines you want to indent and press the CTRL and the ] button at the same time.

Remember you need to indent for your code to work right!

111 of 120

Conditions on the micro:bit

You will be using if statements to test if certain things happen and then do something

  • If micro:bit is shaken → scroll info about your pet 🔈

  • If button A pressed → feed pet so it doesn’t get hungry 🍲

  • If button B pressed → play with pet so it doesn’t get bored 🧶

  • If micro:bit turned face down → let pet sleep so it doesn’t get cranky 💤

112 of 120

Project Time!

while we’re here:

See if you can do Part 3 & 4

� The tutors will be around to help!

113 of 120

Time

114 of 120

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()

115 of 120

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()

116 of 120

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()

117 of 120

Running Time

  • We’re going to keep track of the time in our game

  • As time passes, your pet will get more hungry, bored and sleepy

  • You have to push buttons to feed and play with it and turn it upside down to give it some sleep!

  • If you don’t, it will get too hungry, sleepy or bored and it will die and turn into a ghost!

118 of 120

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()

119 of 120

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!

120 of 120

Tell us what you think!

Click on the

End of Day Form

and fill it in now!