HomeTurtlegraficsGPanelRobotics WebTigerPython |
Python - Online |
Deutsch English |
YOU LEARN HERE... |
how two computers exchange information with each other via Bluetooth. |
WIRELESS COMMUNICATION |
In communication between two micro:bits (also called nodes), each node is both a transmitter and a receiver. First, both nodes switch on the sender/receiver device with radio.on(). This establishes a wireless Bluetooth connection. Subsequently, each of the nodes can send a message (as a string) to the other node with radio.send(msg), which is first stored there in a receive buffer like in a queue. With radio.receive() you fetch the “oldest” message, which is then deleted from the buffer.
|
EXAMPLES |
You create a wireless communication with another person (with a second micro:bit) to exchange information with dots and dashes (like in Morse code). If you click button A on one micro:bit, the middle LED of the display on the other micro:bit lights up for ½ second (like a dot), if you click button B, three LEDs light up (like a dash).
The central part of the program consists of an infinite loop in which first sending and then receiving is controlled. When A is clicked, you transmit the message “p” (short for “point”), when B is clicked, you transmit “d” (short for “dash”). Then use receive() to get the oldest entry in the queue. (If this is empty, you get the special value None.) Depending on whether you have received “p” or “d”, you execute the corresponding action. The following program must run on both micro:bits.
Real Morse code stations almost always work with short and long tones. To do this, you must connect headphones (or an active loudspeaker as shown in the Sound chapter). Your program should now switch on the sound at the receiver as long as button A is pressed. As soon as the button is pressed, you send the message “d” (for “down”) to the receiver; when you release the button, you send “u” (for “up”). To do this, you must introduce a Boolean variable down, where you remember whether the button is pressed or released. Use the Morse code to orient yourself and try to exchange a few short messages (e.g. “SOS”). |
REMEMBER YOU... |
that the sender sends a message with send(msg), which is then stored in the receive buffer in sequence until the receiver picks up the “oldest” message in the buffer with msg = receive(). |
TO SOLVE BY YOURSELF |
|