步驟1:製作
它是什麼?
Use the micro:bit’s radio feature to scare your friends at Halloween.
You will need two micro:bits to make this project. Press buttons on one micro:bit to cause creepy icons to appear and scary sounds to play on the second micro:bit.
What you will learn
By making this project you will learn about networks, how information is sent between electronic devices by radio, and about using selection in a computer program.
運作方式
- There is one program, which is added to both micro:bits.
- The program sets the radio group to 31. Groups are like channels, so any micro:bit using the same group will get the message. 您可以在 0-255 之間選擇任何您喜歡的群組號碼。
- You can send one of three scary messages. Press button A to send the message ‘angry’, press button B to send the message ‘skull’ and press the touch logo to send the message ‘ghost’.
- The micro:bit can send messages as numbers or text. You're using text messages in this project, so make sure you use radio blocks that refer to strings rather than numbers or values.
- In computing, a string is a sequence of characters that can contain letters, numbers, symbols, and spaces.
- When the other micro:bit receives the radio message, it shows either an angry face, a skull or ghost on its LED display, depending on the message received, and different scary sounds play.
- The relevant icon is also shown on the display of your micro:bit so that you can see the message has been sent.
你需要的東西
- 2 片micro:bit
- MakeCode編輯器
- battery packs (recommended)
步驟2:編碼
1from microbit import *
2import radio
3radio.config(group=31)
4radio.on()
5
6while True:
7 message = radio.receive()
8 if message:
9 if message == 'angry':
10 display.show(Image.ANGRY)
11 audio.play(Sound.SOARING)
12 sleep(2000)
13 display.clear()
14 elif message == 'skull':
15 display.show(Image.SKULL)
16 audio.play(Sound.MYSTERIOUS)
17 sleep(2000)
18 display.clear()
19 elif message == 'ghost':
20 display.show(Image.GHOST)
21 audio.play(Sound.HELLO)
22 sleep(2000)
23 display.clear()
24
25 if button_a.was_pressed():
26 radio.send('angry')
27 display.show(Image.ANGRY)
28 sleep(2000)
29 display.clear()
30 elif button_b.was_pressed():
31 radio.send('skull')
32 display.show(Image.SKULL)
33 sleep(2000)
34 display.clear()
35 elif pin_logo.is_touched():
36 radio.send('ghost')
37 display.show(Image.GHOST)
38 sleep(2000)
39 display.clear()
This project has been designed for a micro:bit V2. To make the project work on a micro:bit V1, use buttons A + B instead of the touch logo, and use a 'play melody' block instead of the expressive sounds such as 'soaring'.
步驟3:進階
- Design your own scary icons with the 'show LEDs' block.
- Add animations instead of static icons.
- Compose some creepy music that plays when the second micro:bit receives a message.
This content is published under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licence.