2018年5月24日星期四

Computational Thinking for Educators


I was asked for our school’s latest INSET day to devise a half-hour session which I would repeat during the day for all teaching staff on the fairly loose topic of sharing good practice / sharing our classroom activities.
Now I was fairly sure that 30 minutes was not going to be long enough for me to introduce much interesting in the way of Computer Science or code for a group of teachers who had never experienced it before, so I began to think about what I could deliver.
My thoughts turned to some of the Computational Thinking lessons we do with our students as they are accessible and don’t require the use of a computer. The fact that I was going to deliver a session on sharing good practice from my teaching and that it would not involve any tech other than a projector and presentation, would no doubt come as a shock to some of my colleagues!
So I began to look at the different resources we use with our KS3 students and also had a look through the Google Computational Thinking for Educators course (I might even complete it some time soon!). Google have a fantastic set of resources available for all educators; some of which we use with our students already.
You can find the slides from presentation below (hint open up the speaker notes too!). If you want to make a copy of them, feel free but please credit back here and leave the other credits in the notes too. You can copy them by opening the link under the slide and selecting “File > Make a copy…” to add them to your own Google Drive.
This article comes from Jon Witts.

2018年5月23日星期三

Snake on the BBC micro:bit


The BBC, in their first digital literacy project since the 1980's, recently gave Year 7 students (aged 11-12) in the UK a tiny single-board computer called the micro:bit. It has a 5x5 grid of LEDs, two buttons, plenty of sensors, and not much memory (16K) or processing power (16 MHz). Thankfully, that's still plenty enough computer to have some fun with.
Playing snake on the micro:bit
To me, the 5x5 LEDs looked like they might display a small game of Snake. All I had to do was figure out the input. I originally used the accelerometer to control the snake by tilting the device, but it lacked the speed and precision of a good Snake game. What I really wanted was a way to move the snake in four directions using the two A/B buttons.
If you know binary, you'll know that two on/off values gives us four possible combinations. Of course, one of the combinations is off/off meaning no buttons are pressed. For this case, I decided it was most natural to have the snake constantly move 'down' until a button is pressed: A goes left, B right, and A+B up.

Controls

Video

Despite a slightly odd control layout and tiny display, I found Snake on the micro:bit to be surprisingly playable. As proof, here's a video of me playing a full game. It's converted from a GIF so the timing looks a bit odd. In reality the movement is nice and smooth.

Source code

The game clocks in at around 120 lines of Python. To read, I suggest you start with the 'main game loop' at the end of the file.
from microbit import *
from random import randrange


class Snake():

from microbit import *
from random import randrange


class Snake():
def __init__(self):
self.length = 2
self.direction = "down"
self.head = (2, 2)
self.tail = []

def move(self):
# extend tail
self.tail.append(self.head)

# check snake size
if len(self.tail) > self.length - 1:
self.tail = self.tail[-(self.length - 1):]

if self.direction == "left":
self.head = ((self.head[0] - 1) % 5, self.head[1])
elif self.direction == "right":
self.head = ((self.head[0] + 1) % 5, self.head[1])
elif self.direction == "up":
self.head = (self.head[0], (self.head[1] - 1) % 5)
elif self.direction == "down":
self.head = (self.head[0], (self.head[1] + 1) % 5)

def grow(self):
self.length += 1

def collides_with(self, position):
return position == self.head or position in self.tail

def draw(self):
# draw head
display.set_pixel(self.head[0], self.head[1], 9)

# draw tail
brightness = 8
for dot in reversed(self.tail):
display.set_pixel(dot[0], dot[1], brightness)
brightness = max(brightness - 1, 5)


class Fruit():
def __init__(self):
# place in a random position on the screen
self.position = (randrange(0, 5), randrange(0, 5))

def draw(self):
display.set_pixel(self.position[0], self.position[1], 9)


class Game():
def __init__(self):
self.player = Snake()
self.place_fruit()

def place_fruit(self):
while True:
self.fruit = Fruit()
# check it's in a free space on the screen
if not self.player.collides_with(self.fruit.position):
break

def handle_input(self):
# change direction? (no reversing)
if button_a.is_pressed() and button_b.is_pressed():
if self.player.direction != "down":
self.player.direction = "up"
elif button_a.is_pressed():
if self.player.direction != "right":
self.player.direction = "left"
elif button_b.is_pressed():
if self.player.direction != "left":
self.player.direction = "right"
else:
if self.player.direction != "up":
self.player.direction = "down"

def update(self):
# move snake
self.player.move()

# game over?
if self.player.head in self.player.tail:
self.game_over()

# nom nom nom
elif self.player.head == self.fruit.position:
self.player.grow()

# space for more fruit?
if self.player.length < 5 * 5:
self.place_fruit()
else:
self.game_over()

def score(self):
return self.player.length - 2

def game_over(self):
display.scroll("Score: %s" % self.score())
reset()

def draw(self):
display.clear()
self.player.draw()
self.fruit.draw()


game = Game()

# main game loop
while True:
game.handle_input()
game.update()
game.draw()
sleep(500)

Installation

You can use the Mu editor to flash the above code to your micro:bit.
This article is written by Mr. Caolan McMahon.

Our facebook: https://www.facebook.com/ElecFreaksTech/?ref=bookmarks                Twitter:https://twitter.com/elecfreaks

2018年5月22日星期二

How to Use micro:bits for Teaching and Learning


In 2016 – as part of the BBC’s Make it Digital initiative – the micro:bit was given free to millions of pupils across the UK in a bid to address the growing skills shortage in our technology sector. It was hoped, that this ingenious little device would inspire digital creativity in a new generation of tech pioneers.
But just what is it?
Put simply, the BBC micro:bit is a pocket-sized, codeable computer packed with features. It boasts an ARM-built processor, a motion sensor, built-in compass, USB port, 25 LED lights, buttons, and Bluetooth technology. So, the micro:bit can be used to create all sorts of things.

 

The benefits of using micro:bits in the classroom

Technologies are just another tool in the teachers box, but one that can be used in very innovative ways to inspire and educate children.

 

Boost engagement

With endless possibilities when it comes to what it can be used for, the whole purpose of the micro:bit is to inspire higher levels of engagement and creativity in a new generation of young people.

 

Prepare pupils for employment

Making learning with technology fun, the easy to use micro:bit encourages students to engage with STEM subjects, boosting their future employability.

 

Applicable across education levels

The simplicity and versatility of the micro:bit makes it an easy, fun starting point for early years digital engagement. But the more you hack, the more you can do. So it’s also a powerful tool for more advanced coders, designers, artists, scientists, and engineers.

 

Boost computational thinking across the curriculum

When it comes to the educational benefits of the micro:bit, its not always about programming. The device can also be used to instil computational thinking, enhanced learning, and problem-solving across disciplines. These skills are vital when it comes to giving students the tools they need to thrive in our emerging and complex economy,

 

Boost learning and retention

Students learn best when they are immersed in learning, and this is achieved through interaction and application. With the micro:bit, pupils can get hands on and switch from being consumers of digital information to designers and creators. Such active learning also ensures that they retain information with greater ease.

 

Encourage collaborative learning

A hands-on tool, the micro:bit is a fantastic way to use technology to create more multi-sensory classrooms based on communication, innovation, and collaboration skills.

 

No need for costly investment

With a lack of funds cited as one of the main reasons for insufficient technology in schools, a class set of micro:bits can be purchased for about the same price as a laptop. The micro:bit can also be used in conjunction with your existing kit, and to access free learning materials and programming environments on any internet-connected device.

 

How to use micro:bits in the classroom

Here are just some examples of how you can use the BBC Micro:bit in your lessons:
  • Create a micro:bit 8 ball. Create a magic 8-ball game which generates random predictions when the micro:bit is shaken or a button pressed
  • Create a sorting hat. Children love Harry Potter, so bring some magic into your classroom by creating your very own sorting hat (you can also do this with a Raspberry Pi)
  • Become composers. Pupils can use the micro:bit to create their very own music
  • Build a robot. Its easy to make your own mini robot (micro:bot). You can even get micro:bot packs to help you get started.
  • Control the TARDIS. On 28th March 2017, The micro:bit will be put to the test at the controls of the TARDIS in a special Live Lesson in collaboration with the team behind Doctor Who. Find out more.
  • Solve real-world problems. Present your class with a real engineering problem and encourage pupils to work collaboratively to come up with a solution
How are you using micro:bits in your classroom? Tell us on Twitter!

While the micro:bit is designed for educational use, to capitalise on its potential, many educators will need guidance on how to use it. In response, the Micro:bit Foundation offers a range of free cross-curricular teaching resources and support; including curriculum-linked lesson activities and step-by-step tutorials. Teacher training is also available.

This article comes from Promethean.

Desk Light Ornament and Door Light Sign


This tutorial will show you how to program and build a desk ornament that lights up. These lights change colors over the course of an hour. You will also learn how to program and build an accompanying door sign that lights up. You can use the door sign as decoration or as a way to let others know you are on the phone or busy :)

Step 1: Gather Supplies

You will need:
TECH
AESTHETIC (you can use different materials for a different looking desk ornament or door sign)

Step 2: Programming the Desk Ornament Light

Desk ornament code - created using Makecode.org
I programmed the Micro:bit for the desk ornament using Makecode.org's block-based programming platform. To use makecode.org, you can click on the link to the desk ornament code above and then select edit. This will take you into makecode.org's editor where you can begin using this code.
The video above walks through what each part of the code does, so you can easily customize it for your project.
Some ideas for customizing your code are changing the time intervals or changing the color of the lights. Time intervals are currently at every ten minutes the light color will change. You can change the light colors to any RGB color.
Once the code is how you want it, download the code and upload it to one of the Micro:bits. For help, please see Makecode.org's documentation on uploading code to the Micro:bit.
Tips
You can use smaller scale numbers to test your code in the Makecode.org simulator, which also simulates the neopixel light strip.
If changing the time intervals, use an online milisecond to minute converter to help figure out your time in miliseconds.

Step 3: Programming the Door Sign Light

Door sign code - created using Makecode.org
I programmed the Micro:bit for the door sign using Makecode.org's block-based programming platform. To use makecode.org, you can click on the link to the door sign code above and then select edit. This will take you into makecode.org's editor where you can begin using this code.
The video above walks through what each part of the code does, so you can easily customize it for your project.
Some ideas for customizing your code are changing the color of the lights in your door sign.
Once the code is how you want it, download the code and upload it to one of the Micro:bits. For help, please see Makecode.org's documentation on uploading code to the Micro:bit.
Tips
You will need to have the door sign code uploaded onto the Micro:bit used for the door sign and the desk ornament code uploaded onto the Micro:bit used for the desk ornament in order to test this code.
If your code is not working, make sure you've set both Micro:bits to "radio set group 1." This is like setting the radio frequency and will make sure your Micro:bits can communicate to each other.

Step 4: Testing the Tech

Now that your code is completed, you can test it with your Micro:bits and LED strips. Before doing this, you will need to prepare your LED strips and connect the Micro:bit with the LED strip.
PREPARING YOUR LED STRIPS
I found that it is suggested that the Micro:bit only powers 8 Neopixel lights at a time. You'll need to cut your strip, so there are only 8 lights on it. There is a scissor icon above each section of copper signifying where you can cut your LED strip.
Once you have two LED strips of 8 lights (one for your desk ornament and one for your door sign), you're ready to connect the Micro:bit to the LED strip for testing.
CONNECTING THE MICRO:BIT WITH THE LED STRIP
To test your Micro:bit code with your LED strip, you can use alligator clips to connect the two components. You'll need to do this for each Micro:bit and LED strip.
Connect one side of your first alligator clip on the pin number you assigned your LED strip to (in my code this was pin 0). This sends your code to the LED strip.
Connect the other side of your first alligator clip to to Din copper piece on the LED strip. Make sure it is NOT Dout. The arrows on the LED strip should be going to the right.
Connect one side of your second alligator clip to the 3V pin on your Micro:bit. This pin powers the LED strip。
Connect the other end of your second alligator clip to the 5V copper piece。
Connect one side of your third alligator clip to GND on your Micro:bit. This is your ground.
Connect the other side of your third alligator clip to GND on the LED strip.
TEST YOUR CODE
Now that your technology is connected, you can press the buttons on your desk ornament Micro:bit to see if your LED strips are lighting up.
Tips
Reasons your LED strip might not be working:
If you are using more than 8 LED lights to one Micro:bit, you might not have enough power.
Double check that your alligator clips are connected to the correct sources.
Make sure your alligator heads on the LED strip are not touching.

Step 5: Assembling the Tech

Now that you know your tech is working, you can solder jumper wires to the LED strip. Soldering jumper wires will make for a more reliable connection between the Micro:bit and LED strip as well as make sure the copper on the LED strip doesn't get damaged from the alligator clips.
Once the jumper wires are soldered to the LED strip, you can connect the alligator clips to the end of the jumper wires instead of directly to the LED strip.
Tip
Some Neopixel LED strips will come with an end like the one in the video for this step. If yours has this end, you can either cut it off or choose not to solder. If you choose not to solder, you will need to insert a jumper wire into the hole that is above the white (or Din) wire. You can then connect an alligator clip to this jumper wire and connect your other alligator clips to the pre-connected wires. The video above shows what inserting a jumper cable into this hole looks like.

Step 6: Assembling the Desk Ornament

I've outlined the steps for how I made my desk ornament. This part of the project could be 100% customized though to make a different looking desk ornament.
1.Use the frosted spray on the glass light bulb, so the light bulb is not see through. You might need to do a couple coats.
2.Cut a hole in the cardboard box. This hole is where you can place your lightbulb and is also where the wires will travel through. Make sure to measure the bottom of your light bulb so it is a snug fit.
3.Cut a small hole in the back of the box for your alligator clips to come out of. In this project, the Micro:bit acts as a controller, so you need to have easy access to it, which is why mine is on the outside of the box.
4.Laser cut the top wood piece. You will need to measure the circle of your light bulb to make sure the hole you laser cut is big enough.
5.Place the light bulb cap in the hole of the cardboard box. It should fit snuggly.
6.Put your wires through the hole in the light bulb cap, which is in the hole you cut. Your LED strip should be poking out the top with your wires in the bottom of your box.
7.Put the alligator clips through the hole in the back of the box.
8.Connect the alligator clips to the Micro:bit.
9.Place the top wood piece over the light bulb cap and make sure it looks how you want it too. If it does, you can glue the piece down.
10.Insert the LED strip into the light bulb and screw the light bulb into place.
11.Laser cut any additional decorative pieces for the desk ornament and glue them on.
You can now test your desk ornament code with the desk ornament assembled.
Tips
Use duck tape to organize your wires inside your box.
Use duck tape to mark your wires as GND, Din, and 3V.
The Micro:bit drains battery power even when nothing is on, so you will want to be able to easily access the on/off switch on your battery pack.

Step 7: Assembling the Door Sign

I've outlined the steps for how I made my door sign. This part of the project could be 100% customized though to make a different looking door sign.
Laser cut what will be the front of your door sign. Make sure that light can come through your design
Assemble your technology inside the shadow box and behind the piece that was laser cut, so you can't see it from the front. I used duck tape to organize my wires and keep everything in place
Use a piece of styrofoam to press the laser cut piece to the front of the shadow box. This makes it so you do not have to glue this piece to the glass, which could ruin the aesthetic
Put the back of the shadow box on. This will press on the styrofoam keeping the laser cut piece in place. Keep the battery pack outside of the shadow box. The wires will run out from the side of the back
Attach the Micro:bit's battery pack to the back of the shadow box. The Micro:bit drains the batteries even when not being used, so this will make it easier to access the battery pack's on/off switch
You can now test your door sign code with your desk ornament's Micro:bit controller.

Step 8: Have Fun Using!

You can now use your desk ornament as a fun way to see how time is progressing and your door sign either as decoration or to let others know that you're on the phone or busy :).
This article comes from Instructable.

2018年5月19日星期六

Micro:bit Basics for Teachers Part 3: MicroPython

Teachers: learn the basics for teaching your students to code with the micro:bit using MicroPython.

Hardware components

Story

Introduction

In part one of our micro:bit series, we learned about the hardware, and in part two, we learned how to code using Javascript blocks with the micro:bit.
Today, I will be covering the basics of coding with MicroPython. I will focus mainly on using the micro:bit’s LED display. I will start by walking through a simple code and how it works.
I’ll show you where to find common MicroPython commands used in coding and then we will walk step by step through writing the code to measure temperature with your micro:bit. There is so much more that you can do with the micro:bit than what we will cover in this video. At the end of this lesson I will include a list of resources including the reference guide to MicroPython, lesson plans, and written code for you to use in your classroom.

Why Learn Python?

First of all, why learn Python?
As of 2018, Python is the fourth most popular coding language in the world. It’s commonly used by scientists, data scientists and web developers.
The code we’ll be using today is actually called MicroPython. It’s a version of Python designed specifically to run on microcontrollers. MicroPython is opensource, which means anyone can see all of the code, and anyone can edit it and submit their edit for review.

The Reference

Go to microbit.org, and click ‘let’s code.’ Scroll down to Python and open the code reference in another tab.
This reference has everything you need for coding with MicroPython. It's a good idea to keep it open when you are coding or helping your students to code in MicroPython.


The Editor

Unlike the Javascript editor, the Python editor doesn’t include blocks, although it may in the future.
The very first line of your code is “from microbit import *” The star symbol indicates the word “all”. So, by writing this command first, you are telling the MicroPython editor that you want to use (or import) all of the code needed for the micro:bit to work. The code needed to make the micro:bit work is called a library.

Libraries

What is a library? A library is a larger collection of code that can be referenced by using a short command in the editor.
Take the command - display.scroll - as an example. The command display tells the micro:bit you want something to be displayed on the LED grid and .scroll tells your micro:bit to scroll the letters within the parentheses and quotations across the micro:bit LED grid.
Since micropython is opensource you can actually view all of micropython’s libraries here: https://github.com/bbcmicrobit/micropython
If we navigate to the library for display (https://github.com/bbcmicrobit/micropython/blob/master/source/microbit/microbitdisplay.cpp) you can see that there are 579 lines of code in this library. Thankfully, they have all been defined by a simple command, so you don’t have to write all 579 lines in your code. All you have to write is .scroll.
There are many different types of libraries that you can use and import. The micro:bit library MUST be included in your code in order to use the micro:bit with MicroPython. It includes code to let you control the temperature sensor, accelerometer, LED grid, pins and everything else on your micro:bit’s hardware.
Some special code isn’t included in the basic microbit library, so you’ll have to import it separately. For instance, say you wanted to generate a random number when you shake the micro:bit. You would need to type “import random” to import the random module, since the code you need to generate a random number is not included in the basic micro:bit library.
If you want to use music, you have to type “import music.”
If you’re getting creative and you want to add some extra neopixels to your project, you’ll have to “import neopixel.”


Unfortunately, the micro:bit currently doesn’t support the onboard bluetooth radio with Python. The Python code for BLE is a library as well, and it’s simply too many lines of code to fit on the micro:bit.

While True

You’ll see "while true" a lot when you’re coding in MicroPython. “While true” basically means that while some condition is true, keep looping over some code.
It might be a bit confusing at first. What, exactly, is true? Most values in Python are true.
Since “While True” doesn’t define what condition needs to be true for it to continue looping, it will keep looping forever unless you type ‘break.’ Since it loops forever, it is used similarly to the ‘forever’ event handler in the blocks.

Hello World

Let’s walk through what the code within the While loop does. We already talked about the display.scroll command. This command will scroll whatever is written in the parentheses across the LED display.
Since the 70s, it’s been tradition for your first program to say ‘Hello world.’

Images

Our second command - display.show will show whatever is in the parentheses on the LED display. In this case it will show an image of a heart.
There are a lot of built in images that you can use with the image command. The list of images can be found in the micro:bit reference under “images”. You can also create your own images which we will talk about a bit later.


Sleep

The last part of this code is sleep(2000). Sleep is the command for “pause” or “wait”. This command tells the micro:bit to stop the display and wait the amount of time in the parentheses. When you are coding with your micro:bit the default time length is in milliseconds. So in this case, the display will stop and wait 2000 milliseconds which is the equivalent of 2 seconds, before repeating the sequence.

Download and Run your Code

Now that you know what this code does, run it!
Press download to download your code as a hex file. Make sure your micro:bit is plugged in. Drag and drop it to upload it to your micro:bit and see what it does!
The micro:bit says “Hello world”, then shows a heart. Then it waits two seconds, then it starts over again.

Building the Temperature Sensor

Go ahead and delete your code. Just like we did in the last lesson in Javascript, we’re going to build a temperature alert that alerts you if the temperature gets too hot or too cold.
Before we start writing our code we need to think about how we want our thermometer to work. We want our thermometer to show us if the temperature is too hot or too cold. There are a few ways to do this, either by playing a noise, showing a display, or both.
For this demonstration we are going to have the thermometer scroll the temperature and then show a display to indicate if the temperature is within the desired range or not. Our desired temperature is 28 degrees Celsius. If the temperature is higher than 28 degrees Celsius we will have the LED display show an angry face. Or else If it is less than 28 degrees we will have the LED display show a snowflake. If the temperature is exactly 28 degrees then that means it is in the desired range and we will have the micro:bit display a happy face.

Snippets

We’re going to make a forever loop using while true. Type ‘wh’ and hit the TAB key. Replace ‘condition’ with True.
Snippets are a cool way to avoid typing. You can access snippets from the "Snippets" button, but it's a lot quicker and easier to learn the triggers for the different fragments of code.

Get the Temperature

While our loop is running, let’s get the temperature. I’ll search the reference to see how to do that.

In the reference, I find the method microbit.temperature() I can actually just type temperature() since I’ve already imported the micro:bit library. I’ll save the temperature to a variable called “temp.”
temp = temperature() 
A variable is used to label and store information in your program. By creating a variable called “temp” we can store the temperature that is detected by the micro:bit. Then whenever you want the micro:bit to use the stored value, in this case the temperature, all you have to do is write the word temp in your code.
Now, let’s show the temperature.
Display.scroll(temp) 
Add a brief sleep, 500 milliseconds, so that our micro:bit gets a break.
sleep(500) 

Saving

Save your code. Unlike the Javascript editor, pressing save won’t save your code in the browser. Instead it will download your code as a Python file on your computer.
Download and run the temperature code that you just wrote.

Debugging

Uh oh, looks like we have an error. The error is a type error. Although this code should work (since it’s included in the reference samples) it doesn’t for some reason.
So what is a type error? Data types are a bit out of the scope of this tutorial, but I’d recommend reading more about them in the tutorial here. Because we want our micro:bit to display a number rather than letters, we’ll have to change our temperature value to be a string. To do that, we simply write “str” in front of (temp) like this:
display.scroll(str(temp))
Now when we upload the code, we can see that the code is working

Errors

MicroPython is kind of picky about how the code is written. If your micro:bit displays a NameError you may have typed something incorrectly like accidentally including a capital letter that shouldn't be there, or misspelling a word. For Python to work, it has to be typed EXACTLY right.
If your micro:bit shows a SyntaxError you may have forgotten simple punctuation that MicroPython needs to understand the code. Look over your code to be sure you haven't forgotten any special characters like quotation marks or colons.
Indentation is also extremely important in MicroPython. Indentation is how your program knows the order of operations. If you don’t indent, or indent too much, the micro:bit will complain about an indent error.

You can indent more by selecting the code you want to indent and pressing tab. Indent less by selecting it and pressing ‘shift tab.’
Sometimes your micro:bit just stops responding. Unplug the USB or battery cable and plug the cable back in again to power cycle your micro:bit. If that does work, try closing your code editing browser and opening it again. Make sure you've saved your code first!

Logic Statements

Now that we’ve written code to display the temperature, Let’s write an if statement that will alert us if the temperature is over 28 degrees.
Start by using snippets to write the framework of your statement.
  • “if” starts an if statement.
  • “ei” starts an else if statement.
  • “el” starts an else statement.
We’ll add the temperature thresholds that we want to trigger certain reactions. If temp is greater than or equal to 29, do something. Else if temp is less than 28, do something else. Else, in other words, if and only if the temperature is equal to 28, do a final thing:

Display an Image

So what is the thing we want to do? We want to display an image.
Go to the reference to figure out how to display images. Just like blocks, MicroPython comes with lots of built-in pictures to show on the display. For example, to make the device appear happy you type:
display.show(Image.HAPPY). 
We’ve added a happy face to our “else” statement when the temperature is 28 degrees. Let’s add an angry face for when the temperature equal to or above 29.

Create your Own Images

You can also create your own images.
Here is an example of the code you would write for an image. This is the happy image that is already pre-coded in the micro:bit. You can replicate it by turning specific LED’s on and off using 0 for off and 9 for the highest brightness. You can see in the code there is a 5x5 grid that matches the LED light display on the micro:bit.
happy = Image("00000:"
         "09090:"
         "00000:"
         "90009:"
         "09990")

Create a Snowflake Image

Let’s create a snowflake image to represent cold.
Each LED pixel on the physical display can be set to one of ten values. If a pixel is set to 0 (zero) then it’s off. It literally has zero brightness. However, if it is set to 9 then it is at its brightest level. The values 1 to 8 represent the brightness levels between off (0) and full on (9).
snowflake = Image("50605:"
             "05650:"
             "66666:"
             "05650:"
             "50605")
display.show(snowflake)
I’ve tested it by copying it into the default sketch and uploading it to my microbit.
Let’s be honest, that doesn’t look like a snowflake at all.

Create an Animation

Thankfully, micro:bit has an awesome feature that lets us animate images.
First, make a bunch of images that show what the snow will look like as it is falling. Name the images snowflake1, snowflake2, snowflake3, etc.
from microbit import *
snowflake1 = Image("05030:"
"90200:"
"01006:"
"30070:"
"40508")
snowflake2 = Image("30900:"
"05030:"
"90200:"
"01006:"
"30070")
snowflake3 = Image("40508:"
"30900:"
"05030:"
"90200:"
"01006:")
snowflake4 = Image("30070:"
"40508"
"01020:"
"00600:"
"05090")
snowflake5 = Image("01006:"
"30070:"
"40508"
"01020:"
"00600")
snowflake6 = Image("90200:"
"01006:"
"30070:"
"40508"
"01020")
Now we’ll add them to an array named all_snowflakes.
all_snowflakes = [snowflake1, snowflake2, snowflake3, snowflake4, snowflake5, snowflake6] 
An array is a fancy way to say “list.” This is where we can list words, numbers, or images and have them appear on the LED display in order, one after the other. We put the list into a variable called snowing. That way, on the next line we can just ask the micro:bit to display.show all_snowflakes without having to re-write all of the image code again and it will show each image in the order that we listed them in the array.
To make the snowflake animation run, type “display.show(all_snowflakes)”:
I’ll set loop to true, and clear to false, since I want my snow to fall over and over again.
Setting loop to true means it will continue to loop forever, and setting clear to false means that it will not clear the LED display before starting the loop over again.
Open another browser window and paste just the snowflake code,. Download it and upload it to your micro:bit. You should see the snow falling.
Perfect. Now, if the temperature is below 28 degrees, a winter wonderland will be displayed!

Additional References/Learning Materials

We’ve gone over the basics of the Python editor. To continue learning, I’d recommend going over the getting started micro:bit tutorials found in the MicroPython reference here. If you finish those, go up one level in the reference, and you’ll find BBC’s UCL micro:bit tutorials here. These tutorials go over a number of different topics, and they also include some cool challenges.
For inspiration, you can always go to hackser.io/microbit, or search Hackster.io for tutorials using Python on the micro:bit. Happy hacking!

Code

from microbit import *
snowflake1 = Image("05030:"
 "90200:"
 "01006:"
 "30070:"
 "40508")
snowflake2 = Image("30900:"
 "05030:"
 "90200:"
 "01006:"
 "30070")
snowflake3 = Image("40508:"
 "30900:"
 "05030:"
 "90200:"
 "01006:")
snowflake4 = Image("30070:"
 "40508"
 "01020:"
 "00600:"
 "05090")
snowflake5 = Image("01006:"
 "30070:"
 "40508"
 "01020:"
 "00600")
snowflake6 = Image("90200:"
 "01006:"
 "30070:"
 "40508"
 "01020")

all_snowflakes = [snowflake1, snowflake2, snowflake3, snowflake4, snowflake5, snowflake6] 

while True:
temp = temperature()
display.scroll(str(temp))
if temp >= 29:
display.show(Image.ANGRY)
elif temp < 28:
display.show(all_snowflakes, loop=True, clear=False)
else:
display.show(Image.HAPPY)
sleep(500)
This article written by Monica Houston comes from hackster.io.

Our facebook: https://www.facebook.com/ElecFreaksTech/?ref=bookmarks                Twitter:https://twitter.com/elecfreaks

精选博文

How to Make a Counter with microbit

How to Make a Counter with microbit When we have boarded airplane, we often encounter a situation like this: a beautiful stewardess ca...