MissingwingsJake VaughanRegan
Created November 16, 2021

Red Light Green Light

Red-Light-Green-Light game using the Bluefruit Circuitboard and Bluefruit Connect app with audio and visual cues.

11
Red Light Green Light

Things used in this project

Hardware components

Circuit Playground Bluefruit
Adafruit Circuit Playground Bluefruit
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
×1
Adafruit Circuit board container
×1

Software apps and online services

Mu

Hand tools and fabrication machines

Cloth Strips
Sewing Kit

Story

Read more

Custom parts and enclosures

resized_20211115_193708_rvl9ZrrLYx.jpeg

resized_20211115_195010_bxS6GhPhkh.jpeg

resized_20211115_195734_u5aavcC07C.jpeg

resized_20211115_210711_5dVdqxcVnH.jpeg

resized_20211115_194449_K0Fz7Cfvjo.jpeg

resized_20211115_213416_VmPMRNfvrT.jpeg

resized_20211115_213412_ZUwodoGjfP.jpeg

vid_20211116_150839974_Eq7OdWRQbg.mp4

Code

Red Light Green Light

Python
The object is a wristband that detects movement based on the state changed in the app. In Green Light mode movement is freely allowed and is signaled with two bells, while in Red Light mode movement is detected and lights turn red, and the board plays a sound and changes multiple colors to signal a loss. While disconnected, the lights turn blue.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# Print out the color data from ColorPackets.
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
# Connect, and then select colors on the Controller->Color Picker screen.
import time

from adafruit_circuitplayground import cp
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_bluefruit_connect.packet import Packet

# Only the packet classes that are imported will be known to Packet.
from adafruit_bluefruit_connect.button_packet import ButtonPacket


ble = BLERadio()
ble = BLERadio()
uart_server = UARTService()
advertisement = ProvideServicesAdvertisement(uart_server)

cp.pixels.brightness = .3

greenLight = False

gameOn = False

print('start-up')

while True:
    # Advertise when not connected.
    ble.start_advertising(advertisement)
    print('advertise')
    while not ble.connected:
        # Show blue if device is not connected
        for x in range(10):
            cp.pixels[x] = (0, 0, 255)
        # Turn device to red light mode and game to off if device is disconnected.
        greenLight = False
        gameOn = False
        pass

    while ble.connected:
        # Set lights to red when game starts
        if gameOn is False:
            for x in range(10):
                cp.pixels[x] = (255, 0, 0)
            gameOn = True

        # Debug tool- print game state
        print(greenLight)
        # Check for button input from phone
        packet = Packet.from_stream(uart_server)
        # If the board is receiving input, read in input from phone
        if isinstance(packet, ButtonPacket) and packet.pressed:
            # Set green light with 1.
            if packet.button == ButtonPacket.BUTTON_1:
                for x in range(10):
                    cp.pixels[x] = (0, 255, 0)
                greenLight = True
                cp.play_file("DoubleBell.wav")
            # Set red light with others
            else:
                for x in range(10):
                    cp.pixels[x] = (255, 0, 0)
                greenLight = False
                cp.play_file("AirHorn.wav")
                # Give the player one second to stop moving
                time.sleep(1.0)
        # if red light, player loses if moving
        if greenLight is False:
            if cp.shake(shake_threshold=10):
                print("You lose")
                cp.play_file("laugh.wav")
                for y in range(5):
                    for x in range(10):
                        cp.pixels[x] = (10*x, 0, 100)
                    time.sleep(.25)
                    for x in range(10):
                        cp.pixels[x] = (100, 10*x, 0)
                    for x in range(10):
                        cp.pixels[x] = (0, 100, 10*x)
                    time.sleep(.25)
                    for x in range(10):
                        cp.pixels[x] = (5+x*10, 50+x*5, 100+x*3)
                    time.sleep(.25)

Credits

Missingwings
4 projects • 0 followers
Contact
Jake Vaughan
4 projects • 0 followers
Contact
Regan
4 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.