2018年1月31日星期三

Micro:bit Game: Flappy Bird

Take flight and achieve your pipe dreams with your own version of the notoriously challenging Flappy Bird game, using nothing but a micro:bit (no extras needed) and some Python code.
Made by Cheryl from Raffles Institution. Warning: heavy dosage of bird puns included.

Goals

We’re going to create a full-fledged interactive game on your 5x5 LED screen, playable for ages 9 days to 90 years old. In the process, you’ll learn how to: First step is to import the micro:bit library into Python. Then, let a ‘READY’ message scroll across the screen and initiate countdown that shows when the game starts. Line 1: This imports the micro:bit program. Line 4: This initiates the ‘READY’ message that scrolls across the screen. Double quotation marks indicate a string (in this case ‘READY’). Lines 5-10: This flashes each number on the screen for 1 second (or 1000 milliseconds, the measurement involved) by using the sleep() function. Line 11: clears the screen for us to draw the bird and walls later on. Note: It’s always good to add comments to explain your own code for others or yourself to understand when coming back to it. You add a comment with ‘#’. Also space out your code when necessary to indicate different segments that do different things.

Materials

1 x BBC micro:bit 1 x Micro USB Cable (Seriously, that’s all you need.)

Why Python?

Reads like English – Python is one of the easiest languages to read, which makes it such a fantastic beginner’s language.
Versatile – Python is industry standard for good reason. It can be used to do so much. This is why Google and YouTube utilise the language for part of its back-end software.
Active community – Python is one of the most popular languages for beginners. There are tons of resources and many more than willing to help look over your code, which will prove invaluable to helping you get over stumbling blocks in your coding journey.
Actual coding looks cooler than block-based drag-drop coding. I know it’s intimidating, but look at these colours! (Demo of Flappy Bird on Sublime Text) How Do I Start Coding in Python?
If you’re a fledgling to programming, you probably don’t have Python lying around. Don’t worry! Just go to the official micro:bit Python editor or download the offline Python editor mu to write code and send it to your micro:bit. You can also your own text editor (three cheers to Sublime 3 and Atom) but you have to flash it to the micro:bit. This might turn out to be quite troublesome. Alternatively, you can use a micro:bit simulator, which is really useful to test code out without downloading the .hex file each time, and makes it easier to fix errors.
Once set up, connect your micro:bit to your computer using the micro-USB cable. It should connect to the port at the top of the backside of the micro:bit. Once ready to be flashed, the micro:bit should light up bright yellow. Ignore this step if you’re on the simulator. Otherwise, stop reading and set it up if you haven’t already. Don’t worry, I’ll wait.
Welcome back. Without feather ado, let’s get started! A Bird’s Eye View of What We’re Doing.
The key to tackling every programming problem is to break it into bite-sized achievable bits. Let’s look at what we’ll need. Refer to the video to see a demo of the game. As we go through the process, let’s ask what elements are within the game. A ‘READY’ message and countdown shows when the screen starts.
Create a coordinate to indicate the bird. Move the bird around by pressing button A. Keep track of the number of pipes the bird passes. Create walls for the bird to fly past When the bird collides with a wall, the game is over. You might already know how to do some of these. Try covering these steps on your own first. If necessary, break the steps down further into smaller steps. There are also game checks which should be the progress you’ve made by that step. Use these to make sure you’re on track.

Step 1 – Hello, World!

 
First step is to import the micro:bit library into Python. Then, let a ‘READY’ message scroll across the screen and initiate countdown that shows when the game starts. Line 1: This imports the micro:bit program Line 4: This initiates the ‘READY’ message that scrolls across the screen. Double quotation marks indicate a string (in this case ‘READY’) Lines 5-10: This flashes each number on the screen for 1 second (or 1000 milliseconds, the measurement involved) by using the sleep() function. Line 11: clears the screen for us to draw the bird and walls later on. Note: It’s always good to add comments to explain your own code for others or yourself to understand when coming back to it. You add a comment with ‘#’. Also space out your code when necessary to indicate different segments that do different things. What you’re doing is applying functions to the object display such that the LCD screen lights up. In Python, you also have the flexibility of slowing down the scrolling rate of text in line 4. display.scroll(“READY”, delay = 200) scrolls the text twice as fast and display.scroll(“READY”, delay = 800) scrolls the text at half the speed. The standard delay setting is 400. Increasing the value decreases scroll speed and decreasing the value increases scroll speed. Congratulations! You finished the pre-game message! Next, we have to actually set up the game for the user to play.

Step 2 – Fly, Birdie!

Next, we have to create the image of the bird. For those who never analysed the game, Flappy Bird only allows the bird to move upwards and downwards, and pushes it at a constant speed towards the walls. Of course, our screen only has 5 rows of LED so it’s quite limited. To make the bird-flapping more realistic, we’ll be splitting these 5 segments into 100 different positions. This gives us more flexibility when adding speed of descent later on. In this case, the top of the screen is position y=0 and the button is position y=99 so there are 100 positions. The start position is y=50. Line 13: This sets the start position of the bird right in the middle, as y=0 is the top and y=99 is at the bottom. Line 17: This determines the actual position of the bird on screen, since there are 100 positions and 5 LED rows. Hence, you divide the value stored in variable y by 20 so you scale the bird down onto the screen Line 18: This displays the bird on the screen using the display.set_pixel function, which has 3 parameters: x, y and brightness. The x-coordinate is 1 so it will appear in the second column. The y-coordinate is presently 2 because we divided 50 by 20 and rounded it down. That’s the third row. (Note: Indexes begin at 0 usually for computer programming, so you have rows 0-4 from above to below and columns 0-4 for left to right.) Brightness can be any integer from 0 to 9, with 9 being the brightest. In this case, 7 will suffice to avoid eye-strain. We add a while loop to tell the micro:bit to keep repeating the block of code that is indented. (Python uses indentations to separate code.) The sleep code tells the micro:bit to run this loop every 20ms so it makes your game far more manageable and makes sure your CPU doesn’t work too hard and crash the browser, which would otherwise happen. Game check: At this point, a welcome message should appear, then disappear for a bird to appear.

Step 3 – Leaving The Nest

The previous step only created the bird image, but it still can’t move! This is what we will do in the next step, by simulating realistic gravity. Firstly, let’s add a new variable ’speed’ right below the y-coordinate. Shift the display.clear() into the while loop such that it no longer just clears the welcome message, but also clears the old position of the bird, as it runs before the new position is set each time Lines 25-29: This sets a new y-coordinate of the bird within the borders (max y=99, min y=0), based on the ‘gravity’ acting at that point. Why place it all in the while loop? Well, you want this block to continually update the position of the bird every few milliseconds (20 to be exact) so this block will keep repeating itself Terminal velocity: to make the motion of the bird more realistic, speed reaches a constant rate of 2, but only after two iterations of the code whereby speed = 0 becomes speed =2. The if function ensures that speed does not increase beyond 2. You can play around with this to vary the speed of bird descent.

Step 4 – Defying Gravity

Now, we have to get the bird to hop by pressing button A. In this step, we also include a new ‘score’ variable to track the number of walls that the bird flies past. This can be accessed at any point using button B. To react to key-pressing of A, run ‘buttona.waspressed()’ under an if-loop like in line 21. If, during that iteration, the A button was pressed at any time, we bring the bird up, reset the falling rate, then let it accelerate back down to the ground, giving the falling and flapping motion. Change the value of speed on flapping, which is currently -8, to see the visual changes to rate of bird’s descent. Add variable ‘score = 0’ to set new variable score to 0, underneath the speed and y variables. As a coding habit, try to set all your variables in one place, above the code that uses it so it’s easier to follow, and actually can be inputted for use. Show score when button B is pressed by creating an if loop similar to button A. display.show(score) shows the score at any point in time. We’ll learn to vary and count the score after each wall-passing later. Game check: Welcome message appears, disappears, then bird appears that falls down. Press A for it to flap upwards and B to check the score, which should remain at 0 right now.

Step 5 – Pipe Blaster

We’re going to create our first pipe using a makepipe function! Then we’ll assign it to variable i, and show pipe within the while loop. I know it’s complicated, but it’ll also be the start of owl/our game finally looking complete! Functions are blocks of code that are run conveniently under the function name. By calling a function, we can run the entire block of code within it. This makes it easier to understand what we’re doing at each step. In this case, we’ll name our function makepipe() which runs code to make a new pipe each time. Let’s break down what each step of the makepipe() function does At line 19, we define the function using def makepipe(): – the indented blocks beneath make up the function At line 20, a custom image is drawn, with the ‘0’ indicating 0 brightness for each coordinate, starting from row 1, column 1 then row 1, column 2 and so on. This basically lights up the LED of the entire last column with the brightness of 4. (You can tweak this as you like. I personally like for the bird to be clearly brighter than the wall so you can identify its position.) At line 21, we use the random library to call a random number between and inclusive of 0 and 3. This means 0, 1, 2 and 3. We don’t use 4 because we blast two holes, one which is gap+1. If 4 was selected, we would blast a hole in column 4, row 5. But there’s no row 5 so an error is returned. We have to return this image so that it can be called as the value of i later on. The hole is blasted by setting the LED brightness for the gap position and the LED above it to be zero. Pretty cool, eh? That’s your first function. Good job! Note: always define the functions above the actual code, beneath the variables. This is just a convention, but it makes your program readable! Let’s assign variable i to the function, as per line 27. Now, in the while loop, if we add a display.show(i), the display now shows the pipe (and hole) i. Persevere! We’re nearly there. Now, we just have to get the wall moving, count scores and react to bird-wall collisions. Game check: Same as step 4, and now there’s an unmoving wall with holes! Check earlier steps if something has gone afowl.

Step 6 – Frame Rate

This step is where we set up the game constants. Here, the frame variable starts at 0, then increases by 1 every 20ms so it takes 400ms or 0.4s for the frames variable to increase by 20. Remember this, it’ll be easier for the incoming math. These constants aren’t used until Step 7, but let’s set them up first. Line 15 just indicates the time taken (in ms) for frame to increase by 1, which is added as part of the while loop in line 37 (frame += 1). You can change the sleep(20) at the bottom of the code to sleep(DELAY) so it corresponds. Line 16 sets the time taken for the wall to shift by 1 column. This is currently 0.4s or 20 frames. Line 17 sets the time between the occurrence of another wall. This is currently 2.0s or 100 frames. Line 18 sets the time between the score increasing. This should always be equivalent to the FRAMESPERNEW_WALL value so that each wall you pass is equivalent to one additional score. To make the game harder, you would adjust these game constants, perhaps reducing the distance between each new wall for more walls (but change FRAMESPERSCORE to correspond to it). The game is currently set for one wall on the screen at any time, but you can definitely make it more chaotic by playing around with the values. Note: The game constants are in uppercase, differentiating them from the other variables used. These are just standard rules for Python programming. It’ll still work without following it, but your code should follow conventions to be readable.

Step 7 – Pipe Dreams

Here, we will compare the frame value with game constants to move the wall left, create a new wall and increase the score. This is all within the while loop so it’s checked every 20ms. Ready? Let’s go. At this step, we’ll use the modulo sign (%). This provides the remainder when a number is divided by another number. So 4 % 2 returns 0 but 4 % 3 returns 3. Here, we’ll use it to check that the frame variable is equal to any of the game constants. Moving wall left: Look at lines 65-67. This means the wall shifts when the frame is equal to 20, 40, 60… since they’re divisible by FRAMESPERWALLSHIFT value of 20. You can vary this to make the walls move faster and increase the difficulty. Currently, the walls move every 0.4s. Creating new wall: Look at lines 69-71. Every 100 frames, or 2 seconds, a new pipe is made by calling the makepipe() function for i. This is the constant used to create and move the wall. Increasing the score: look at lines 73-75. This means that a point is added when the bird travels for 2 seconds, or 1 wall. This value corresponds with the distance between walls so each wall passed is one point. Game check: The game should be almost fully playable, with the welcome message, then the bird moving by pressing button A. You can see score with button B. There’s gravity acting on the bird so it falls down over time. Then the walls created randomly move right past it. Wow, you’re nearly done! Now, we just have to react to pipe collisions, ending the game and revealing the score when the bird collides with any pipe.

Step 8 – Collision Course

Phew, you made it to the last step! Ready to wing it? Now, we just need to add a collision reaction. This uses a getpixel function that returns the LED brightness value at that position. ‘!=‘, the NOT function is also used. Let’s explain how it’s used below. Add this collision checking code to the while loop, between the bird-drawing and wall-shifting. This means it checks for collision before new walls are created so there’s no extra scores by error. As shown in line 66, we use an if loop. ‘i.getpixel(1, ledy) != 0 checks if there is a pipe in the position of column 1 (where the bird is), specifically at ledy, the displayed position of the bird. If there is a pipe pixel in the same position as the bird’s coordinates, the i.getpixel(1, ledy) returns 4, the brightness of the wall. This is NOT 0 so the function beneath, the collision checker, runs Line 67-68 display the in-built sad face image for 0.5s. You can change how long this lingers, and to whatever other image you like. Python has a lot of images you can input. You can find the entire list here. Line 69 displays the score as a string, behind “Score: “ Line 70 ends the while loop so the game ends. This means that it’s ‘game over’.

Start Game!

And… that’s it! You’re done. Your game should be able to run and end, revealing the score at the end. It’s now a full-fledged frustratingly simple yet challenging game. Pat yourself on the back! That was a lot of hefty coding and new concepts. Look through your code, and try and figure out what each line. Add comments to explain it to yourself if necessary. This is a good practice for you to easily read your own code when coming back to it months later.
Good job! Have fun frustrating your friends with this novel interface for the annoying game. Now, you’re free as a bird to look for other projects, with a better understanding of the Python code. Extension: Add a game loop, such that you can play again without resetting the device. I suggest changing the while loop’s requirements from True to a certain variable, a play_again function which can be changed with the press of a button. Look at other Python game loops for inspiration, like a scissors, paper, stone game.

2018年1月30日星期二

Finger Dexterity Micro:bit Game

Are your psychomotor skills as bad as mine?

Goals

We are going to create a game where the player must click on a key (on the ADKeypad) that corresponds to the column on which a random LED lights up (A for the first column and E for the last). The pace at which the LED lights up gets quicker and quicker as the game goes on. You’ll learn how to:
  • use an ADKeypad with the micro:bit.
  • use functions recursively.
  • use while loops.
  • improve your finger dexterity!

Materials and Pre-requisites

1 x BBC micro:bit
1 x Micro USB cable
1 x F-F Jumper Wires
1 x ADKeypad
Or
ElecFreaks Micro:bit Tinker Kit (contains all components in the above.)
You also need some experience about if-else statements, variables etc.

Procedure

Step 1

Plug in your ADkeypad to Pin0, making sure the positive lead is connected to the yellow signal pin and the negative lead is connected to the black ground pin on the breakout board.

Step 2

In order for the ease of randomisation of the LED that lights up, we will use a function recursively. A function used recursively will call itself (!) so as to acheive the end goal. I created the function plotLight for this reason. If you have not covered functions, go here.
Then i set two variables randomLightXIndex and randomLightYIndex to integers between 0 and 4. This will correspond to the specific LED that lights up. Doing this will ensure randomness (let us not get into the discussion as to where true randomness can really be generated) of the LED that lights up so the game will be different and unpredictable every time.
I also set the variable bool to true. While this may not be obvious now, it will come in handy later (in reality this was a later addition that I decided to add after the rest of the function was fleshed out. The reason for this will come to light later). This is a common technique in coding (especially with while loops).
In order to increase the difficulty, it was my judgement that a time variable could be useful. We use this later to decrease the pause time betwen one LED lighting up and the next. We have set a lower limit for the pause time at half a second so as to not make the game impossible. When we call the function recursively, the if-statement modifying the pause time is what will decrease the pause time everytime the function is called.
I have created a bunch of if-else statements inside a loop. These statements periodically check if a button on the keyboard was pressed and if the button corresponds to the x-coordinate of the LED that lights up. We have to do this because the pressing of the keypad does not emit an event that our event listeners in micro:bit’s core modules can respond to (like how it does for shaking or button presses). Thus, we had to create our own event listener. This event listener only runs as long as bool (which we created earlier) is true.

Step 3

Inside the if-else statement, we check to see which key was pressed and if it corresponds to the column of the LED (x-coordinate). If it was, we CALL THE FUNCTION AGAIN. This is how recursive programming works. By calling the function again, we basically start over with a new LED. Note that when we call the function again we decrease the value of the time variable and thus the pause duration will be shorter.
Note that I unplotted the point first LED. This is to ensure that we don’t have more than one LED in each round so as to not confuse the player. If you wanted to make the game more difficult, you could show multiple LEDs and play for only the most recent LED that lights up. Treat that as an extension!
Interestingly, I have set bool to false. Why?

Step 4

The bool is set to false so as to terminate the above while loop. This is not strictly necessary and I initially disregarded this. However, it is important to note that terminating the while loop greatly improves the efficiency of your program and efficiency of our programs is something generally worth considering.
I have also created and called a function to handle the case where the player types the wrong key. This will be covered later.

Step 5

That was quite a lot for one function! It can be quite a bit for a newbie at programming so let me go through that one more time.
We use random integers between 0 and 4 for the determination of the LED that lights up. (Note that we use an index that starts with 0 – this means that the top left corner is (0,0))
In anticipation of the function being called in some point in the future, we decrease the pause time so that when that happens the game is more difficult.
We run our own homemade event listener (the name betrays its function – it simply waits for an event to happen and acts with our preset code when it does). We use a while loop to listen for an event. If it does not find an event in one loop the if-else statements inside will not be activated and thus, it will go on to the next iteration. When the event does happen (in this case the pressing of the key), the if-else statement is activated from its slumber and thus, in this rather ingenious way, we have created an event listener. (Extension: Browsers listen for events like clicks or keypad presses in the same way).

Step 6

Inside each if-else statement, we have decided to end the game if the wrong keypad was pressed and tell the player what we think of him/her.
If the right key was pressed, we immediately go on to the next LED light whilst ending the previous while loop or effeciency purposes (just to be clear, your code will still work but it’s best not to foster such bad habits).
Whilst going on to the next LED light, we make use of a concept called recursion. To fully understand the inner workings of recursions we must be familiar with concepts like execution contexts, which is beyond the scope of this tutorial.

Step 7

That was a lot of work!!
But in the end, we have created a wonderful function that can be called recursively. It is remarkable that such a game can be simplified so much so that its crux is in one block of code!

Step 8

Now we just want to tie up some loose strings.
The lose function is one that we will call when the player presses the wrong key. It is mostly self-explanatory and if you could get past the previous parts, it should be obvious what the code does.

Step 9

Now to start the first LED.
We call the function when the file loads. Due to the recursiveness of the function, the game will take care of itself thereafter with minimal effort from us. How is that for effeciency!

Wonderful!

You have completed this tutorial! If you wish to challenge yourself further, go ahead and add a counter that counts the number of points a player gets before he/she loses. Clue: Create a variable called counter and increment it as you see fit. Remember to display it as well!
Congratulations!
This tutorial was possibly a level higher than the rest and if you got here you are definitely rocking it. If you didn’t, take solace in the fact that it took me weeks to get my head around concepts like recursions too. Good luck!

2018年1月29日星期一

Bravo! Now We are A partners of Micro:bit Official!

Today, I am going to tell you an exciting news: BBC Micro:bit Foundation has included us as one of their partners! You can read this page http://microbit.org/resellers/ for more details.

1

Since the establishment of micro:bit, we Elecfreaks always keep a close cooperative relationship with micro:bit foundation:
First of all, we have developed more than 20 types of micro:bit related products like octopus:bit,motor:bit,ring:bit,starter kitTinker kit and so on. Except for these alreay on sale, we have about 10 new products under development. Now micro:bit series of products have become a hot product line for us.
Secondly, we are actively promoting the chinese translation work of micro:bit official website. Currently, above 90% are done by our team members. In addition to the translation of the official website, we will also do some translation on the website of MakeCode.
Thirdly, we are trying our best to help micro:bit foundation to do promotion around the world. For example, MakerFaire, BETT, etc.. Especially in BETT SHOW 2018, some of our products are presented among micro:bit official stand.

2
3

ElecFreaks has devoted to the industry of open hardware for almost 8 years already. Through the accumulation of these years, we have improved a lot in development, production, sales and marketing. Now Elecfreaks has grown into a big tree from a small sprout. Our growth can not leave without open source industry, can not leave without your long-term support. Now, it's the time for us to do some contribution. We are so glad to find that micro:bit foundation has the same sense of mission with us. Micro:bit is more than just a simple electric circuit. It is created with a final goal of motivating students to participate into the creative hardware production and software programming. In this era where STEM education is quite popular in the world, BBC Micro:bit Foundation is just like a beacon in the sea, who has promoted micro:bit to the eyesight of the global STEM educators within 2 years by using its powerful appeal. Fast speed and wide range of promotion, no one has ever done it in the open hardware field before. I believe the influence of micro:bit foundation, combining with the development ability of Elecfreaks, will have a win-win situation that lead to a new miracle.

4

Become an official parterner of BBC Micro:bit Foundation means our hardworks are admitted. At the same time, it stimulates us to continuously update our products and encourage us to improve our service and user experience.

About Us

ElecFreaks is an early start-up established in 2011 by a group of electronic enthusiasts in Shenzhen. ElecFreaks are devoted to provide superior open hardware and service to makers and educators. We focus on compatible accessories and modules development for open source platforms such as BBC micro:bit, Arduino, Raspberry Pi, and its integration modules. ElecFreaks have massive teaching blogs and video tutorials about education contents for starters to maker pros.

2018年1月26日星期五

Friday Product Post: Ring:bit Car, Acrylic Base Board and RGB Rainbow LED Bead

Hello My dear friends! How is your micro:bit study going? I guess you might have already matered the basic programming skills now and feel itch to do some projects. Well, it's Friday again. Let's find something new for our next project!

New Product 1: Ring: bit Car — Mirco: bit Educational Smart Robot Kit for Kids


Look! What a cute small elephant it is! This small car is made of micro:bit. With two octopus hunt sensors in the front, it can smell the odor of line and march on according to the direction of line. Inside the small elephant car, we have added a ring:bit board to lead out three GPIO ports, among which two GPIO are used for driving two servos and the other one is undefined. You can program for this car, or just change the shell by telling us to custom-made a shell of your favorite cartoon figures like a dear, a pig, a cat, and so on. Just move your hands to make one!

New Product 2: Acrylic Base Board with Nylon Watch Bands for Power:bit


With this case + band + Power:bit, we make your micro: bit into a wearable gadget such as Pedometer, timer or Compass.

New Product 3: Single RGB Rainbow LED Bead 5/Pack


This Led Bead is just the 5 pieces pack of Single RGB Rainbow LED Bead, buy 5 one time, cheaper and useful.


2018年1月25日星期四

Spotlight from A Friend of Elecfreaks on 2018 BETT London

Do you know Bett?
Bett or The Bett Show (formerly known as the British Educational Training and Technology Show) is an annual trade show in the United Kingdom marketing information technology in education organised by Ascential. The first Bett show was held in London, England in 1985, and since 2013 it has been held annually in January at the ExCeL London.(From WIKIPEDIA).
This year from Jan. 24 to Jan. 27, 2018, Bett is held in London again. Waris, one of my friends and also a member from Micro:bit Foundation, has visited to this show with some of our hottest products. Here's what he send back to us:

Nice shoot! It is really attractive to see handsome faces!
Next, he showed me some products he displayed on the show.
Look! This is our IoT kit!It is beautifully placed on the stand. In the kit, there is a crystal acrylic house, a wind speed sensor, temperature and humidity sensor, ESP8266 Serial Wifi Module, PM2.5/PM10 dust sensor, Analog Photocell, Soil Moisture Sensor, Sound Sensor, etc.. This is a magic kit which enables you to detect all environment data and upload it onto IoT platform.
This is an assembled brick toy. It is made of micro:bit, octopus:bit and some bricks. You can see the perfect match of these elements. It is really a good artwork!
They are good products for this show. Because the theme of BETT 2018 is to prepare today's students for future jobs and look ahead to the future education. It can help students learn more about sensors while teaching them some knowledge about IoT(Internet of Things), or how to upload these environment data onto the web and share it with their teachers and classmates.
I am very happy to see our products showed on this important fair. Because Bett is the most influential fair in education equipment field. Most people who visit this fair are educators from junior or senior education organisations and on-the-job education organisations. Lots of people have visited BETT 2018. More and more parents, teachers, school masters care more about education than ever before. It is a trend that tells us a transform is closing in education field closer and closer, and our children will become more clever and intelligent.
In the last, I have attached two pictures sent from Waris to show you how hot The Bett Show is. You can see my photo and imagine the atomsphere on the spot. I am look forward to hear good news from BETT 2018!




https://www.elecfreaks.com/12421.html

2018年1月23日星期二

Make A Compass with Your Micro:bit

Micro:bit has integrated a compass on the board. Today, we are going to learn how to use BBC micro:bit to make a simple wearable compass device and display directions on the LED screen.

Tools&Materials:

1 x ELECFREAKS Power:bit For Micro:bit
1 x BBC Micro:bit Board for Coding & Programming
1 x Acrylic Base Board
1 x Watch Strap
7 x 3*5 Screws

How to Make

Firstly, we have to fix micro:bit onto power:bit.

Then, fix the acrylic board onto power:bit with 2 screws.

Programming

Click to open Microsoft Makecode, write your code in the editor.
Click Edit on the top right corner to edit your program, then click Download to save your code directly into your micro:bit.

Code Explain

Here we use "N" to stand for north, "S" for south, "W" for west and "E" for east.
The data “0” read by micro:bit compass represents the north, so within 45 degrees in the left or right, micro:bit will display “N”.
From 45 degree to 135 degree, micro:bit displays an E on its LED screen, while from 135 degree to 225 degree, micro:bit displays an S, and from 225 degree to 315 degree is a W.

Operation Instruction:

Once we have completed code download, we can see "DRAW A CIRCLE" displayed on micro:bit screen. Then there is a light spot on the screen center. Rotate micro:bit for a circle to calibrate your compass. After you finished your circle movement, a smily face will be shown on the screen, which indicates that you have successfully done the calibration.
Next, we can see that micro:bit is able to show the current direction.
Now just move your micro:bit and see which direction it points to!

精选博文

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