Monday, 31 October 2016

Teleporting Ducks

The excellent Python MU editor makes it so easy to get BBC micro:bits talking to each other using simple radio data communication. No magic required, just physics.

After attending a brilliant talk / demo about the MU editor by Nicholas Tollervey, I came away feeling inspired.  I thought I would have a go myself and in no time I had 2 microbits sending data to each other wirelessly.   

I wrote some simple code based on an example by Nicholas Tollervey. First to send an image when pressing button ‘A’ between micro:bits.

  1. from microbit import display, button_a, Image
  2. import radio
  3. radio.on()
  4. while True:
  5.     message = radio.receive()
  6.     if message:
  7.         display.show(Image.DUCK)
  8.     if button_a.was_pressed():
  9.         display.clear()
  10.         radio.send("hello")




Next I thought it would be fun to add some neopixels. When button A is pressed the neopixels show red then amber on the sending microbit and green plus the image on the receiving microbit.

  1. from microbit import *
  2. import radio
  3. import neopixel
  4. np = neopixel.NeoPixel(pin0, 1)
  5. green = (0, 255, 0)
  6. red = (255, 0, 0)
  7. orange = (255, 153, 0)
  8. radio.on()
  9. while True:
  10.     # Read any incoming messages.
  11.     message = radio.receive()
  12.     if message:
  13.         np[0] = (green)
  14.         np.show()
  15.         display.show(Image.DUCK)
  16.         sleep(3000)
  17.         np.clear()
  18.     # Button A sends a "flash" message.
  19.     if button_a.was_pressed():
  20.         np [0] = (red)
  21.         np.show()
  22.         sleep(1000)
  23.         np [0] = (orange)
  24.         np.show()
  25.         sleep(1000)
  26.         np.clear()
  27.         display.clear()
  28.         radio.send("hello")




To connect the neopixels you’ll need to attach them to the micro:bit as below.






















You will need:

2 x  BBC micro:bits
2 x Neopixels ( I used Crumble Sparkles )
6 x Crocodile leads

The code is available here.