-2

I have some really simple code, which I want to build up to detect button presses and then send out the button status using the radio function. But I cannot get the code to work, I thought setting the string with a fix text and then broadcast using the radio function would just work, but seem to be getting an error. I don't want to do via if states, as I will add more into the string with different sensor values and button presses etc.

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)
nekomatic
  • 5,988
  • 1
  • 20
  • 27
Brendon
  • 123
  • 9

2 Answers2

3

You don't say how you are detecting the transmission. There is a working example of sending and receiving simple messages using button presses at this question:

BBC Bit Micro - Radio string transfer random carriage returns

The example uses if statements which you say you want to avoid. These are used to detect the button press.

Oppy
  • 2,662
  • 16
  • 22
1

Your code as written will work, transmitting that fixed text message every second, on group 0, with no error. Make sure that you write a receiving program that is also on radio group 0. Here's an example of a program that receives and shows messages:

from microbit import *
import radio

radio.config(group=0)
radio.on()

while True:

    incoming = radio.receive()
    if incoming:
        display.show(incoming, delay=200)
neillb
  • 4,893
  • 1
  • 22
  • 18