2018年8月28日星期二

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 carrying a small silver box keeps press it while passing by. She is murmuring:1,2,3,4,5,6...... You must guessed it--she is counting the total number of people sitting on the airplane. And the small silver box on her hand is a mechanical counter. Today, we are going to make an electronic one with BBC micro:bit.
First, we have to know what function we want to realize with this electronic counter. I have a simple conclusion for it.

Basic Requirement

1.Press button “A” , counter number minus 1;
2.Press button “B” , counter number plus 1 ;
3.Press button “A”、“B” together, counter number become 0.

Material

1 x micro:bit
1 x USB
You might wondering how can we make an electronic counter with a micro:bit board and a USB cable only. Don’t worry! It is very easy and simple. You will learn it soon.
After we gathered our materials, we have to program for it. Connect micro:bit board with your computer. Then click this link: https://makecode.microbit.org/# to open programming interface. We are going to use Block method to do programming. You can read the following steps to learn how to program.

Step 1

To start, we build a new variable named “counter” and set “0” as the initial value .

Step 2

Write code for press button “A”、”B” and “A+B” separately.
1.Press button “A”:
The function of button “A” is deduct count number. We all know that no matter what we count, the count number would not be under 0. If negative number appears, then there must be something wrong. In order to avoid this mistake, we have to set counter judgment “≥ 1” in our program. If “counter≥1”, then press button “A”, it will deduct 1 automatically.
2.Press button “B”:
Every press increases 1 counter number.
3.Press button “A+B”:
Press button “A” and “B” together, the counter number become 0. Then you can start a new count.

Step 3

After we finished button code writing, we have to use 5*5LED screen to show the counter number.
We can directly drag block “show number” under button code. Then the count number on screen will change according to the variable counter number.
You can see the whole program code in the following picture:
You can rewrite code by yourself to enjoy programming by dragging different blocks within block editor. It is very simple and easy just like playing bricks.Or you can download code directly into your micro:bit through the link below:
Now, let’s download the whole program code into micro:bit and see what will happen.
With this counter, we can know how many books we have placed on our book shelf, how many plates in the kitchen, how many eggs left in our fridge. Even, we can use it to count a basketball game. This tiny electronic is really powerful.

Question

How to revise our program if we want to limit the maximum value of counter? Your further discussion is welcomed!

Rock Paper Scissors

Rock Paper Scissors, you might know it well for you had played it in your childhood again and again. No matter how fairness it seems, this game has drawback, which comes from your hand motion speed and tricks,and you must have observed if you paid attention to it. We all know that BBC micro:bit has Bluetooth function and different micro:bit boards can transmit data with it. Today we are going to use this Bluetooth function to play Rock Paper Scissors. Bluetooth signal is unseen and untouchable so that it will completely eradicate tricks during the game.

Our Goal

Through studying the case i am going to show you, you can gradually get to know the usage of micro:bit Bluetooth function such as Group ID setting, data transmission and data receive.

Our Requirement

1.Press button “A” to choose Rock, Scissors or Paper.
2.Press button “B” to confirm our choice and transmit it with Bluetooth.
3.If we have finished choose and received data transmitted from our partners, then micro:bit will begin to judge who wins and display it on the panel.

Materials Needed

2 x micro:bit
2 x USB Cable

Programming

Step 1

Set different Bluetooth group ID. Here we set group ID to be 13. Only if two micro:bit board have same group ID, then Bluetooth communication can be done.
We use button “B” to confirm. Set button to “0”, means we do not press button while set to “1” means button pressed down.
We set variable “ received” to show it has received Bluetooth signal. “0” for not-received and “1” for received.
We use variable “item” to preserve our hand signs. “0” for rock, “1” for scissors, and “2” for paper.

Step 2

Set the function of button “A”. Every press changes pattern order.
We use pattern  to show rock, pattern  to show scissors, and pattern  for paper.

Step3

Set the function of button “B”. With Bluetooth signal, we can send out our hand signs(i.e. variable “item”) At the same time, set button to “1”.

Step 4

Set Bluetooth receiving function. Store the received data into variable “receiveNumber”, and set”received” to “1”.

Step 5

Judge in “while” circulation. Once we showed our choice, then it will start to judge who wins. Usually we use  to show victory,  for lose, and  for tie.
Then, let’s download the code into micro:bit and see what will happen.
You can download code here directly:
https://makecode.microbit.org/_e7q0bcMVXAuX

Question

If we want to show rock, scissors, or paper randomly, then how shall we change our program?

micro:bit Breakout Adaptor

micro:bit Breakout Adaptor

Why we need adaptor for micro:bit? In the following article we are going to show you the answer for this question.
We can take out a micro:bit board and watch it closely. We all know the side pin on BBC Micro:bit board can lead to the input signal or generate control signal with special interface. As for pins of 0/1/2/3V/GND, we can use alligator clip to lead signal. But other pins are too close to lead, which is obviously presented on micro:bit board. Usually the voltage of micro:bit is 3.3V while other modules (like LED digital tube module, IIC LCD module, etc.) connected to it require 5V power supply and 5V TTL voltage to work properly. To solve the above problems, we have invented Micro:bit Adaptor.
Elecfreaks Adaptor can completely match with BBC Micro:bit pins. Plug our Adaptor into BBC Micro:bit, you will find it is much more convenient to lead.

Features of Elecfreaks Adaptor

1.Extended PGIO port from P0 TO P16;
2.There are pins for VCC and GND beneath each I/O port with different colors for your recognize. You can connect breakout modules conveniently. The arrangement of footers are fully compatible with OCTOPUS series products.
3.Booster module added enable you to select VCC signal of P19 to P16 between 3.3V and 5V at will.
4.Lead UART port, I2C port and SPI port. Among these ports, I2C port can connect 3 road I2C devices while SPI can connect 2 road SPI devices.
5.Two adaptors can connect each other directly to do serial port communication.
The main feature of this adaptor lies in the function of switching power supply. You can remain the original 3.3V voltage while setting the voltage of partial I/O serial ports to be 5V. With this function, we can extend the usage of 5V electronic modules like 4 bits digital tube module. On the other hand, by slide a switch only, we can make it compatible with 5V serial port communication devices. You don’t have to add a voltage switching circuit or a resistance. This adaptor greatly minimized the difficulty of operation so that you can pay more time and energy to programming and creation.
In the following passages, we are going to show you the voltage change of Micro:bit with an oscilloscope.
First of all, we have to do a small program with purpose to make P16 lead out a square wave on the oscilloscope. Then download the code into Micro:bit.
Slide VCC voltage selection switch to 3.3V.
Look at the oscilloscope for the voltage of P16.
Then slide switch to 5V.
Here’s the result of testing P16 with 2 different voltage channels:
We can see from the oscilloscope that before switching, I/O port voltage is 3.3V; after switching, the voltage becomes 5V. Exchange between 2 different voltage channels are so easy that you only have to slide a switch. Isn’t it very cool?
Adaptor is fully compatible with OCTOPUS series products.You can connect Octopus series products to micro:bit easily and fast just the same with Freaduino. With the guidance of color, you do not have to worry about connect wrong. Look, the picture below, isn’t it looks like a big octopus?
As for students in the doorstep of learning program, complicated circuit design will be difficult for them and greatly minimize the interest of learning. What students need most at this time? It is encourage and proper guidance. We invented this Adaptor aiming to help students use micro:bit as easier as possible and create their own programming artworks. And this is the same with the design purpose of micro:bit--make kids enjoy the happiness of programming and creation.

micro:bit Fundamental Course--Button & Display

In our last blog “ Start Your micro:bit Programming Trip ”, we have talked about the basic operation method of Microsoft Makecode. In the following articles, we will teach you some fundamental functions of blocks one by one.

Our Purpose

To know basic usage of Makecode through studying buttons and blocks.

Materials

micro:bit × 1
USB Cable × 1
On micro:bit, there are 5*5 LED metric dot display and 2 buttons. And today we are going to learn how to use button and LED display together.

Our requirement

Press down a button to display its relative direction. Press 2 buttons together for clearing the display.

Procedure

Step 1

Drag out block “on button pressed” from Input column and set the button to be “A”.
Under block “on button A pressed”, we insert block “show arrow”, which can make micro:bit to display directions with arrow. And here we set direction to be “west”.

Step 2

Choose block “on button pressed”, cope and paste it. Then set button to be “B” and direction to be “East”.

Step 3

Drag out block “on button pressed” from Input column again and set the button to be “A+B”.
Insert “clear screen” into this block.

Step 4

In the left analog window, we can check analog operation. Click Virtual button on Micro:bot to check our program.

Step 5

After confirmation, download program into micro:bit.
Link of the whole program: https://makecode.microbit.org/_UWfD1g4pud5L

Question

If we want to use character string to display “west” and “east”, then how to edit our program ?

micro:bit Raining Alarm Module

micro:bit is an educational product put forward by British Broadcasting Corporation (BBC), which aims to help young people to learn basic programming knowledge.
BBC micro:bit has rich board carrier resources such as button, 5*5 LED dot matrix, gyro, Bluetooth module, etc.. It is portable with only half size of a credit card. Maybe it is for keeping board size small, all IO ports on it do not lead to pins like Arduino . Today we are going to show you a small case to connect all pins on micro:bits with a breakout board.
In the following case, we will use micro:bit( breakout board connected) to produce a raining alarm module.

Requirement

When micro:bit senses raining,it will alarm with light and sound.

Materials

1 x micro:bit 
1 x Breakout Board
1 x Rain Sensor
1 x Passive Buzzer
1 x LED Module
3 x Dupont Cable

IO Connection

P0 connect to passive buzzer module;
P1 connect to raining sensor module;
P2 connect to LED module.
Note:Here, “P” stands for Pin.
You can refer to the following picture for IO connection:
Note: The bottom base of pins on micro:bit breakout board has 3 different colors. Black stand for GND, red for VCC, yellow for signal. These colors are completely relative to the color of dupont cable. You can connect cables according to the relative color.

Programming

After we completed cable connection, click this link: https://makecode.microbit.org/# to open programming interface. We are going to do programming with Block method.
Here we have to note that the output of raining sensor module is analog signal and its correspond value read out by micro:bit is 0-1023.(In this case, 0 for no rain, 1023 for pour rain. Bigger value stands for heavier rain) Now we set the threshold value as 200. If the value on P1 port tested beyond 200, then it is raining and we can see a sign of umbrella on LED screen of micro:bit. At the same time, buzzer will alarm with music. You can refer to the following picture for how to set threshold value.
As for the whole program, you can refer to the following picture:
After we download the code to micro:bits, it will start to run the program automatically. Now pour some water on the raining sensor module to analogue raining effect. Let’s do it and take a look!
Link of the whole program: https://makecode.microbit.org/_Y8i6iRifMUu7

Question

If we want to divide raining to several levels like small rain, middle rain and pour rain and show it with different light and sound, then how to program? Your further discussion is welcomed!

2018年8月27日星期一

micro:bit Fundamental Course--Music

BBC micro:bit has very powerful music functions. Today we are going to use micro:bit to make a music broadcaster.
A piece of music usually consists of tones and rhythms. Different tones correspond to different frequencies. And rhythm controls the holding time of a tone. If we change rhythm, the tone will change with it. If we combine tones and rhythms according to a certain rule, then we can create a harmonious melody. If we relate the combination of tones and rhythms to the wavy frequency output by micro:bit analog footers and its lasting time, and input these signals into a loudspeaker, then it can broadcast the melody. With this conception, we can use micro:bit to design our own music broadcaster.

Our Goals

To learn music functions of micro:bit and create a piece of music with micro:bit.

Materials

2 x Crocodile Clip
1 x micro:bit
1 x USB Cable
1 x LED Music Frequency Spectrum Display Soldering Kit

Requirement

Use micro:bit to compose a piece of music and output it with LED Music Frequency Spectrum Display Soldering Kit.

Procedure

Step 1

Find a music score. Here we take the song “Are You Sleeping”, which you might have known it well and sung it for a lot of times in your childhood, as an example. You can look at the music score below.

Step 2

After we have found our music, the next step is to open Makecode online editor
(https://makecode.microbit.org/) and start our program.
Usually, the note “duo” in score stands for “1” and its relative music scale is “Middle C”. In stave, an independent solid “tadpole” represents a beat. (If you would like to know more music knowledge, you can ask your music teacher for help.) According to this principle, we can translate the first period of music into the following program.
And then we do it the same for the rest music periods to make sure the whole music conversed into code.

Step 3

Plug input ports as the picture showed below with 2 crocodile clips onto LED Music Frequency Spectrum Display Kit. (https://www.elecfreaks.com/9401.html)If you don’t have LED Music Frequency Spectrum Display Kit, you can replace it with a music box or earphone. And the cable connection is the same with the picture showed.

Step 4

Connect micro:bit to your computer with a USB cable and download the program into micro:bit. Then let’s take a look of the effect!

Question

Louise, one of my colleagues, is a music lover. When she knew we were creating a DIY music case, she made a song “ We Wish You Merry Christmas” with micro:bit for us. You can see her code in the below.
If we want to use Touch Button to shift the above 2 songs freely, then how shall we program? I am looking forward to your comments, feedback, or further discussions with us.

精选博文

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