Using the Micro:bit radio¶
Broadcast radio¶
Sending accelerometer data:
from microbit import *
import radio
radio.on()
radio.config(power=7,queue=10)
while True:
x,y,z = accelerometer.get_values()
radio.send("A,%d,%d,%d,%d,%d,%d" % (x,y,z,pin0.is_touched(),pin1.is_touched(),pin2.is_touched()))
sleep(100)
Receiving accelerometer data:
from microbit import *
import radio
radio.on()
radio.config(queue=20)
while True:
data = radio.receive()
if data:
print("%s" % data, end="\n")
sleep(150)
Two way radio¶
from microbit import *
# must include these two lines to use the radio
import radio
radio.on()
# any channel from 0 to 100 can be used for privacy.
radio.config(channel=99)
while True:
if button_a.was_pressed():
# send this message over the radio. Up to 32 bytes OK.
radio.send('HAPPY')
sleep(200)
if button_b.was_pressed():
radio.send('SAD')
sleep(200)
# if there's a message in the queue, retrieve it. Up to 3 messages
# can be in the queue at once, and if it's full, messages are dropped.
msg = radio.receive()
# ALWAYS CHECK for None..
if msg != None:
# as long as there is a message, display something
if msg == 'HAPPY':
display.show(Image.HAPPY)
sleep(200)
display.clear()
elif msg == 'SAD':
display.show(Image.SAD)
sleep(200)
display.clear()