2. Radio examples
2.3. Send on tilting
Set up the group in pairs with a value 0-255.
Use
button_a.was_pressed()
to send a message, based on titling forward or back.Use
button_b.was_pressed()
to send a message, based on titling left or right.Scroll message on sender microbit so message being sent is obvious.
Scroll any received messages.
from microbit import *
import radio
# Choose own group in pairs 0-255
radio.config(group=8)
# Turn on the radio
radio.on()
while True:
if button_a.was_pressed():
y_reading = accelerometer.get_y()
if y_reading > 200:
display.scroll("B")
radio.send("B")
elif y_reading < -200:
display.scroll("F")
radio.send("F")
else:
display.scroll("X")
radio.send("X")
elif button_b.was_pressed():
x_reading = accelerometer.get_x()
if x_reading > 200:
display.scroll("R")
radio.send("R")
elif x_reading < -200:
display.scroll("L")
radio.send("L")
else:
display.scroll("-")
radio.send("-")
incoming_message = radio.receive()
if incoming_message is not None:
display.scroll(incoming_message)
Exercise
Modify the code to allow 9 different messages to be sent based on tilting.