KRITSADA icon

String and radio example

KRITSADA | PRO | 10/28/19 09:31:45 AM UTC | 0 ⭐ | 633 👁️ | Never ⏰ | []
Python |

960 B

|

None

|

0 👍

/

0 👎

# Share and enjoy!
 
from microbit import *
import radio
 
# Channel for transmission
ch = 2
# Just something to send
dummyPacket = "A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5"
 
# Had to resort to 10ms for fidelity
txTimeMS = {"0": 1, "1": 10}
 
radio.on()
radio.config(channel=ch, data_rate=radio.RATE_250KBIT)
 
while True:
 
    x = accelerometer.get_x()
    if x < -1000:
        x = -1000
    elif x > 1000:
        x = 1000
    # rescale -1000->1000 to 0->255
    x = round(x / 7.84313255 + 127.5)
 
    # Convert to binary, making sure the string has 8 bits
    binStr = "{0:08b}".format(int(x))
    # Add start/stop bits ("2")
    fStr = "".join(["2" + c for c in binStr]) + "2"
 
    # A little life sign
    display.set_pixel(0, 0, 9)
 
    for c in fStr:
 
        if (c == "2"):
            # Make some noise
            radio.send(dummyPacket)
        else:
            sleep(txTimeMS[c])
 
    display.set_pixel(0, 0, 0)
    sleep(100)

Comments