KRITSADA

String and radio example

Oct 28th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Share and enjoy!
  2.  
  3. from microbit import *
  4. import radio
  5.  
  6. # Channel for transmission
  7. ch = 2
  8. # Just something to send
  9. dummyPacket = "A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5"
  10.  
  11. # Had to resort to 10ms for fidelity
  12. txTimeMS = {"0": 1, "1": 10}
  13.  
  14. radio.on()
  15. radio.config(channel=ch, data_rate=radio.RATE_250KBIT)
  16.  
  17. while True:
  18.  
  19.     x = accelerometer.get_x()
  20.     if x < -1000:
  21.         x = -1000
  22.     elif x > 1000:
  23.         x = 1000
  24.     # rescale -1000->1000 to 0->255
  25.     x = round(x / 7.84313255 + 127.5)
  26.  
  27.     # Convert to binary, making sure the string has 8 bits
  28.     binStr = "{0:08b}".format(int(x))
  29.     # Add start/stop bits ("2")
  30.     fStr = "".join(["2" + c for c in binStr]) + "2"
  31.  
  32.     # A little life sign
  33.     display.set_pixel(0, 0, 9)
  34.  
  35.     for c in fStr:
  36.  
  37.         if (c == "2"):
  38.             # Make some noise
  39.             radio.send(dummyPacket)
  40.         else:
  41.             sleep(txTimeMS[c])
  42.  
  43.     display.set_pixel(0, 0, 0)
  44.     sleep(100)
Add Comment
Please, Sign In to add comment