2. Radio examples

2.1. Send on button pressing

Set up the group in pairs with a value 0-255 by modifying the group in: radio.config(group=8).
Turn on the radio using: radio.on()
Use button_a.was_pressed() to send a message, “A”.
Scroll any received messages.
if incoming_message is not None: relies on radio.receive() returning None when there is no message received.
from microbit import *
import radio

# Choose own group in pairs 0-255
radio.config(group=8)
# Turn on the radio
radio.on()

while True:
    # send
    if button_a.was_pressed():
        radio.send("A")
    # receive
    incoming_message = radio.receive()
    if incoming_message is not None:
        display.scroll(incoming_message)

Tasks

  1. Modify the code to send “B” if the B-button was pressed.

  2. Modify the code to send back a “B” if “A” is received and “A” if “B” is received, but not until having first displayed the received message.

  3. Modify the code to send “T” or “F” from button pressing so it can be used to answer True and False questions.

  4. Modify the code to send “T” or “F” from button pressing and then show the Image.YES if “T” is received, and the Image.NO if “F” is received.

Q2

Modify the code to send back a “B” if “A” is received and “A” if “B” is received, but not until having first displayed the received message.

from microbit import *
import radio

# Turn on the radio
radio.on()
# Choose own group in pairs 0-255
radio.config(group=8)

while True:
    if button_a.was_pressed():
        radio.send("A")
    elif button_b.was_pressed():
        radio.send("B")
    incoming_message = radio.receive()
    if incoming_message is not None:
        display.scroll(incoming_message)
        if incoming_message == "A":
            radio.send("B")
        elif incoming_message == "B":
            radio.send("A")

Modify the code to send “T” or “F” from button pressing so it can be used to answer True and False questions.

from microbit import *
import radio

# Choose own group in pairs 0-255
radio.config(group=8)
# Turn on the radio
radio.on()

while True:
    # send
    if button_a.was_pressed():
        radio.send("T")
    elif button_b.was_pressed():
        radio.send("F")
    # receive
    incoming_message = radio.receive()
    if incoming_message is not None:
        display.scroll(incoming_message)

Modify the code to send “T” or “F” from button pressing so it can be used to answer True and False questions.

from microbit import *
import radio

# Turn on the radio
radio.on()
# Choose own group in pairs 0-255
radio.config(group=8)

while True:
    if button_a.was_pressed():
        radio.send("T")
    elif button_b.was_pressed():
        radio.send("F")
    incoming_message = radio.receive()
    if incoming_message is not None:
        if incoming_message == "T":
            display.show(Image.YES)
        elif incoming_message == "F":
            display.show(Image.NO)

2.2. Send random number on button pressing

Set up the group such as in: radio.config(group=23, power=7).
Turn on the radio using: radio.on()
Use button_a.was_pressed() to send a message containing a random integer from 1 to 9.
Scroll any received messages.
if message: relies on radio.receive() returning a message, otherwise it will return None, which in turn fives a False result for the conditional.
from microbit import *
import radio
import random

radio.config(group=23, power=7)
radio.on()

display.scroll('Hi')
display.show(Image.HEART)

while True:
    if button_a.was_pressed():
        randomnum = str(random.randint(1,9))
        radio.send(randomnum)
        display.scroll(randomnum)
    message = radio.receive()
    if message:
        display.scroll(message)

Exercise

  1. Modify the power level to values from 0 to 7 and experiment with a partner to get an estimate of the transmission distance at each power level.


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

  1. Modify the code to allow 9 different messages to be sent based on tilting.