2. Radio A or B
2.2. Defs for repeated code patterns
The code below uses a definition for the sender.
The pattern for A and B-button pressing is the same.
Both send text: “A” or “B”.
Both display an image: Image.YES or Image.NO.
sender(text, image)
can be used to reproduce the pattern for both button presses.from microbit import *
import radio
# Turn on the radio
radio.on()
# Choose own group in pairs 0-255
radio.config(group=8)
def sender(text, image):
radio.send(text)
display.show(image)
while True:
# sender
if button_a.was_pressed():
sender("Y", Image.YES)
elif button_b.was_pressed():
sender("N", Image.NO)
# receiver
incoming_message = radio.receive()
if incoming_message:
if incoming_message == "Y":
display.scroll("Y", delay=80)
display.show(Image.YES)
elif incoming_message == "N":
display.scroll("N", delay=80)
display.show(Image.NO)
Tasks
Modify the code to add a receiving definition.
Modify the code to add a receiving definition.
from microbit import *
import radio
# Turn on the radio
radio.on()
# Choose own group in pairs 0-255
radio.config(group=8)
def sender(text, image):
radio.send(text)
display.show(image)
def receiver(text, image):
display.scroll("Y", delay=80)
display.show(image)
while True:
# sender
if button_a.was_pressed():
sender("Y", Image.YES)
elif button_b.was_pressed():
sender("N", Image.NO)
# receiver
incoming_message = radio.receive()
if incoming_message:
if incoming_message == "Y":
receiver("Y", Image.YES)
elif incoming_message == "N":
receiver("N", Image.NO)