| Deutsch English |
![]()
YOU LEARN HERE... |
how two micro:bits communicate with each other via Bluetooth. |
SCENARIO OF A WIRELESS COMMUNICATION |
| Two micro:bits (also called nodes) may exchange information by sending or receiving text messages simultaneously, so act as a transmitter and a receiver. Such a device is called a transceiver. First, both nodes invoke radio.on() to turn the transceiver on. This will create a wireless Bluetooth connection between the devices. Subsequently, each of the nodes can send a message (as a string) with radio.send(msg) to the partner node, where the message is stored in a receiver buffer. The receiver calls radio.receive() to get the "oldest" message, which is then remove from the buffer.
|
EXAMPLES |
|
Here you create a wireless communication link to exchange information that is coded with dots and dashes (like Morse code). If one person clicks button A on the micro:bit, the middle LED of the partner's display lights up for ½ s (like a dot), if the person clicks button B three LEDs light up (like a dash).
The main section of the program consists of an infinite loop, where first the sending and then the receiving is treated. If A is clicked, the message "p" is transmitted (short for "point"), if B is clicked, "d" is transmitted (short for "dash").
Morse stations uses dots and dashes that are perceived as short and long tones. To hear the tone signal you need to connect a headphone (or an active speaker) as shown in the chapter Sound. To exchange Morse code, your program should turn on the tone at the receiver side, as long as the button A on the transmitter side is pressed and stop the tone when the button is released. To do so, you send the message "d" (for "down") when the button is pressed and "u" (for "up") when the button is released. Use a table of the Morse code and try to exchange some short messages (for example "SOS").Program: import radio from microbit import * import music radio.on() down = False while True: if button_a.is_pressed() and not down: down = True radio.send("d") elif not button_a.is_pressed() and down: down = False radio.send("u") rec = radio.receive() if rec != None: if rec == "d": music.pitch(500, 1000000, wait = False) elif rec == "u": music.stop() sleep(10) |
MEMO |
| A micro:bit may act as sender and receiver at the same time. The sender sends a message with send(msg). The received message is stored in the receive buffer in sequence until the receiver fetch the "oldest" message in the buffer with msg = receive(). Such a buffer is also called a "queue" or first-in-first-out buffer because always the first ("oldest") entry is retrieved. |
EXERCISES |
|

Some sound frequencies:
| Tone | Frequency | Tone | Frequency |
| h' | 494 | h'' | 988 | ||
| a' | 440 | a'' | 880 | ||
| g' | 392 | g'' | 784 | ||
| f' | 349 | f'' | 698 | ||
| e' | 330 | e'' | 660 | ||
| d' | 294 | d'' | 588 | ||
| c' | 262 | c'' | 524 | c''' | 1048 |