# sender.py (Wand)
from microbit import *
import radio
radio.config(power=7, channel=67)
radio.on()
game_active = False
while True:
if button_a.is_pressed() and not game_active:
radio.send("START")
display.show(Image.HAPPY)
sleep(500)
message = radio.receive()
if message == "READY":
game_active = True
display.show(Image.HEART)
if game_active:
gesture = accelerometer.current_gesture()
if gesture in ["up", "down", "left", "right"]:
radio.send(gesture)
display.show(gesture[0])
response = None
start_time = running_time()
while running_time() - start_time < 5000:
response = radio.receive()
if response:
break
if response == "NEXT":
display.show(Image.HEART)
elif response == "END":
display.show(Image.SAD)
game_active = False
sleep(2000)
display.clear()
else:
game_active = False
if not game_active:
display.show(Image.DIAMOND_SMALL)
# receiver.py (Main micro:bit)
from microbit import *
from LED_Library import *
from Halloween_Library import *
from Motor_Library import *
import radio
import random
import music
radio.config(power=7, channel=67)
radio.on()
MOTOR_NUM(pin0)
setMotor('off')
LEDS_NUM(pin1, 12)
LEDS_clear()
gesture_chars = {
"up" : Image("00000:00900:09090:09090:00000"),
"down" : Image("00000:09090:09090:00900:00000"),
"left" : Image("00000:00990:09000:00990:00000"),
"right": Image("00000:09900:00090:09900:00000")
}
gesture_nums = {
0 : "up",
1 : "down",
2 : "left",
3 : "right"
}
correct_colors = ['white', 'blue', 'purple', 'green']
pattern = []
while True:
display.show(Image.ASLEEP)
message = radio.receive()
if message == "START":
display.show(Image.TARGET)
pattern = []
for i in range(4):
current_gesture = gesture_nums[random.randint(0, 3)]
pattern.append(current_gesture)
display.show(gesture_chars[current_gesture])
sleep(1000)
display.clear()
sleep(500)
radio.send("READY")
display.show("?")
level = 0
game_won = True
while level < 4:
gesture_msg = radio.receive()
if gesture_msg:
start_time = running_time()
if gesture_msg == pattern[level] and running_time() - start_time < 5000:
display.show(Image.YES)
music.play(music.BA_DING)
color = correct_colors[level]
setColorCycle(color, 500, 100)
level += 1
radio.send("NEXT")
display.show("?")
elif gesture_msg in ["up", "down", "left", "right"]:
game_won = False
radio.send("END")
display.show(Image.NO)
music.play(music.BADDY)
setColorCycle('red', 2000, 100)
LEDS_clear()
break
if game_won:
radio.send("END")
display.show(Image.HAPPY)
setMotorPulse(1, 22)
setTune('adams')
LEDS_clear()
display.clear()
# LIGHT LIBRARY
from microbit import *
from neopixel import NeoPixel
import random
LED = None
num_leds = 0
RedHues = [
(128, 0, 0), (139, 0, 0), (165, 42, 42),
(178, 34, 34), (220, 20, 60),
(255, 0, 0)
]
def LEDS_NUM(pin_num, num):
global LED, num_leds
LED = NeoPixel(pin_num, num)
num_leds = num
def LEDS_clear():
global LED, num_leds
if LED:
for x in range(num_leds):
LED[x] = (0, 0, 0)
LED.show()
def setColorCycle(color, duration, flicker):
global LED, num_leds, RedHues, GreenHues, BlueHues, PurpleHues, WhiteHues
if LED:
start_time = running_time()
while (running_time() - start_time) < duration:
for x in range(num_leds):
if color == 'red':
base_hue = random.choice(RedHues)
elif color == 'green':
base_hue = random.choice(GreenHues)
elif color == 'blue':
base_hue = random.choice(BlueHues)
elif color == 'purple':
base_hue = random.choice(PurpleHues)
else:
base_hue = random.choice(WhiteHues)
brightness_factor = random.random()
r = int(base_hue[0] * brightness_factor)
g = int(base_hue[1] * brightness_factor)
b = int(base_hue[2] * brightness_factor)
LED[x] = (r, g, b)
LED.show()
sleep(flicker)
#Motor Library
from microbit import *
MOTOR = None
def MOTOR_NUM(pin_num):
global MOTOR
MOTOR = pin_num
def setMotor(val):
if MOTOR:
if val == 1 or val == "on":
MOTOR.write_digital(1)
elif val == 0 or val == "off":
MOTOR.write_digital(0)
def setMotorPulse(val, pulse):
if MOTOR:
if val == 1 or val == "on":
MOTOR.write_digital(1)
sleep(pulse)
MOTOR.write_digital(0)
elif val == 0 or val == "off":
MOTOR.write_digital(0)
#HALLOWEEN LIBRARY
from microbit import *
import music
adams_family = ['R:1', 'D3:1', 'E3:1', 'F#3:1',
'G3:4', 'R:9', 'E3:1', 'F#3:1', 'G#3:1',
'A3:4', 'R:9', 'E3:1', 'F#3:1', 'G#3:1',
'A3:4', 'E3:1', 'F#3:1', 'G#3:1', 'A3:4']
spooky_scary = ['G3:3', 'R:1', 'G3:4', 'F3:3', 'R:1', 'F3:4',
'C3:4']
def setTune(n):
global adams_family, spooky_scary, halloween
if n == 'adams':
music.set_tempo(bpm=120)
music.play(adams_family)
"""
elif n == 'skeletons':
music.set_tempo(bpm=150)
music.play(spooky_scary)
else:
music.set_tempo(bpm=110)
music.play(halloween)
"""