Monday, 17 April 2017

Blog 4: What can "UCreate" with a Micro:bit and Neo-pixels (Radio activated lights V1)

Last summer I created some resources to use with Micro:bit/s for CPC and over the next few months and I will try and share the best bits with the kind permission of CPC. 

This one uses Micropython and a neopixel again. The neopixel is from this kit:

The picture below shows how the Micro:bit is set up, This time it uses the radio module to light up an neo-pixel on another Micro:bit, after one of the buttons is pressed. 

Instructions
You will need to code the Micro:bit with MU either on a PC/ MAC or Linux machine. At the time of writing the BBC website will not allow the radio module to work.

This program uses the radio on the Micro:bit to send messages to trigger neo-pixels on other Micro:bits. The radio code here is taken from the demo by N Toll on the Micro:bit read the docs website. 

You will need at least two Micro:bits in order to see it working, both Micro:bits will need the same code loaded onto each one.   

Code to go on each Micro:bit:

from microbit import *
import neopixel

np = neopixel.NeoPixel(pin0,1)

import radio
import random
#import neopixel
#from microbit import display, Image, button_a, sleep



#np = neopixel.NeoPixel(pin0,1)

def Angry():
    np[0] = (255,0,0)#Red
    np.show()
    display.show(Image.ANGRY)
    sleep(3000)
    display.clear()
    np.clear()
   
def Happy():
    np[0] = (0,255,0)#Green
    np.show()
    display.show(Image.HAPPY)
    sleep(3000)
    display.clear()
    np.clear()
   
def Meh():
    np[0] = (255,69,0)#Yellow
    np.show()
    display.show(Image.MEH)
    sleep(3000)
    display.clear()
    np.clear()
radio.on()
while True:
    FeelingsList = ['Angry','Meh','Happy']
    # Button A sends a "flash" message.
    emo = random.choice(FeelingsList)
    if button_a.was_pressed():
        radio.send(emo)  # a-ha
    # Read any incoming messages.
    incoming = radio.receive()
    if incoming == 'Meh':
        Meh()
    elif incoming == 'Happy':
        Happy()
    elif incoming == 'Angry':
        Angry()

No comments:

Post a Comment