Installing the Project Code

Download a zip of the project by clicking Download Project Bundle below.

After unzipping the file, copy code.py and jeopardy.wav to the CIRCUITPY drive which appears in your operating system File Explorer or Finder when the Circuit Playground is connected to your computer via a known good USB cable.

# SPDX-FileCopyrightText: 2021 Eva Herrada for Adafruit Industries
# SPDX-License-Identifier: MIT

# General imports
import time
import random
import board
import digitalio
import neopixel

# HID imports
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS

# BLE imports
import adafruit_ble
from adafruit_ble.advertising import Advertisement
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.standard.hid import HIDService
from adafruit_ble.services.standard.device_info import DeviceInfoService

try:
    from audiocore import WaveFile
except ImportError:
    from audioio import WaveFile

try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        pass  # not always supported by every board!

# Enable the speaker
spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
spkrenable.direction = digitalio.Direction.OUTPUT
spkrenable.value = True


# Make the input buttons
btn1 = digitalio.DigitalInOut(board.D10)  # Marked A3
btn1.direction = digitalio.Direction.INPUT
btn1.pull = digitalio.Pull.UP

btn2 = digitalio.DigitalInOut(board.D9)  # Marked A2
btn2.direction = digitalio.Direction.INPUT
btn2.pull = digitalio.Pull.UP

btn3 = digitalio.DigitalInOut(board.D3)  # Marked SCL A4
btn3.direction = digitalio.Direction.INPUT
btn3.pull = digitalio.Pull.UP

central = digitalio.DigitalInOut(board.D0)  # Marked RX A6
central.direction = digitalio.Direction.INPUT
central.pull = digitalio.Pull.UP

led = digitalio.DigitalInOut(board.D2)  # Marked SDA A5
led.switch_to_output()
led.value = False

buttons = [btn1, btn2, btn3]
upper = len(buttons) - 1

ble_enabled = digitalio.DigitalInOut(board.SLIDE_SWITCH)
ble_enabled.direction = digitalio.Direction.INPUT
ble_enabled.pull = digitalio.Pull.UP

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.5)
# Use default HID descriptor
hid = HIDService()
device_info = DeviceInfoService(
    software_revision=adafruit_ble.__version__, manufacturer="Adafruit Industries"
)
advertisement = ProvideServicesAdvertisement(hid)
advertisement.appearance = 961
scan_response = Advertisement()

ble = adafruit_ble.BLERadio()
if ble.connected:
    for c in ble.connections:
        c.disconnect()

if ble_enabled.value:
    print("advertising")
    ble.start_advertising(advertisement, scan_response)

k = Keyboard(hid.devices)
kl = KeyboardLayoutUS(k)

wave_file = open("jeopardy.wav", "rb")
wave = WaveFile(wave_file)
audio = AudioOut(board.SPEAKER)

while True:
    if ble_enabled.value:
        while not ble.connected:
            pass
        if ble.connected:
            print("Connected")
            led.value = True
            time.sleep(0.1)
            led.value = False
            time.sleep(0.1)
            led.value = True
            time.sleep(0.1)
            led.value = False

    while ble.connected or not ble_enabled.value:
        if not central.value:
            led.value = True
            print("Running")
            while True:
                i = random.randint(0, upper)
                if not buttons[i].value:
                    break

            audio.play(wave)
            if i == 0:
                print("Button 1")
                pixels.fill((0, 0, 255))
                if ble_enabled.value:
                    kl.write("Button 1")
            elif i == 1:
                print("Button 2")
                pixels.fill((0, 255, 0))
                if ble_enabled.value:
                    kl.write("Button 2")
            elif i == 2:
                print("Button 3")
                pixels.fill((255, 255, 255))
                if ble_enabled.value:
                    kl.write("Button 3")

            if not ble_enabled.value:
                print(
                    "BLE HID has been disabled, slide the slide switch to the left to re-enable"
                )

            print("Finished")
            led.value = False

            while central.value:
                pass

            print("reset")
            pixels.fill((0, 0, 0))
            led.value = True
            time.sleep(0.5)
            led.value = False
            print("Ready")
        if ble_enabled.value:
            if not ble.connected:
                break
    else:
        continue
    break

There are also a few libraries you'll need to copy over to the Circuit Playground in the bundle lib directory - copy the whole lib directory to CIRCUITPY:

  • neopixel
  • adafruit_pypixelbuf
  • adafruit_hid
  • adafruit_ble

After you've done all that, this is what your CIRCUITPY drive should look like.

Usage

First, you should decide if you want to use BLE or not. If you do, make sure the slide switch is to the left. To disable BLE, slide it to the right.

If BLE is enabled, the central button will flash a few times when it is connected to your device. If it is disabled, then the code should just be ready to run about a second or two after power is supplied.

When you're ready for people to buzz in, press the central button. It should light up to show that it is ready. At this point, the players can buzz in. The Circuit Playground will turn the color of the player who buzzed in and if BLE is enabled, it will also type something to say which buzzer was pressed.

Finally, to reset this, press the central button again and it will flash to say that it is ready to start again.

This guide was first published on Jun 22, 2021. It was last updated on 2021-06-22 16:57:35 -0400.

This page (Code the Controller) was last updated on Jun 29, 2022.

Text editor powered by tinymce.