bbc微比特电台串播
bbc micro bit radio string broadcast
我有一些非常简单的代码,我想构建这些代码来检测按钮按下,然后使用无线电功能发送按钮状态。但是我无法让代码正常工作,我认为用固定文本设置字符串然后使用无线电功能进行广播就可以了,但似乎出现了错误。我不想通过 if 状态来做,因为我将使用不同的传感器值和按钮按下等将更多内容添加到字符串中。
from microbit import *
import radio
radio.config(group=0)
radio.on()
while True:
button_status_a = "Button A pressed"
radio.send(button_status_a)
sleep(1000)
你没有说你是如何检测传输的。在这个问题上有一个使用按钮发送和接收简单消息的工作示例:
BBC Bit Micro - Radio string transfer random carriage returns
该示例使用了您说要避免的 if 语句。这些用于检测按钮按下。
您编写的代码可以正常工作,每秒在组 0 上传输固定文本消息,没有错误。确保您编写的接收程序也在无线电组 0 上。这是一个接收和显示消息的程序示例:
from microbit import *
import radio
radio.config(group=0)
radio.on()
while True:
incoming = radio.receive()
if incoming:
display.show(incoming, delay=200)
我有一些非常简单的代码,我想构建这些代码来检测按钮按下,然后使用无线电功能发送按钮状态。但是我无法让代码正常工作,我认为用固定文本设置字符串然后使用无线电功能进行广播就可以了,但似乎出现了错误。我不想通过 if 状态来做,因为我将使用不同的传感器值和按钮按下等将更多内容添加到字符串中。
from microbit import *
import radio
radio.config(group=0)
radio.on()
while True:
button_status_a = "Button A pressed"
radio.send(button_status_a)
sleep(1000)
你没有说你是如何检测传输的。在这个问题上有一个使用按钮发送和接收简单消息的工作示例:
BBC Bit Micro - Radio string transfer random carriage returns
该示例使用了您说要避免的 if 语句。这些用于检测按钮按下。
您编写的代码可以正常工作,每秒在组 0 上传输固定文本消息,没有错误。确保您编写的接收程序也在无线电组 0 上。这是一个接收和显示消息的程序示例:
from microbit import *
import radio
radio.config(group=0)
radio.on()
while True:
incoming = radio.receive()
if incoming:
display.show(incoming, delay=200)