Each time one of the four colored buttons in the Simon game console lights up, a sound tone also plays. In this section I'm going to duplicate this functionality using the tone playback feature of the Circuit Playground Express board.

I have found a very interesting blog post by Simon Inns in which he reverse-engineered a Simon console. Among the extensive details he provides on the console internals, he includes the frequencies of the tones played for each of the four buttons. According to the article, the frequencies are as follows:

  • Green: 415 Hz
  • Red: 310 Hz
  • Yellow: 252 Hz
  • Blue: 209 Hz

This can be represented with a third tuple in the game:

REGION_TONE = (
    252,  # yellow region
    209,  # blue region
    310,  # red region
    415,  # green region
)

Playing a tone with a given frequency cannot be easier on the Circuit Playground Express. The cpx.start_tone() takes a tone frequency in Hertz and starts playing that tone on the on-board speaker. The tone continues to play until the cpx.stop_tone() is called.

With this information, I can now extend the light_region() function from the previous section to play tones to go along with the lights. The place where I called the time.sleep() function can be expanded into three parts, where first the tone is started, then the sleep call is made, and finally the tone is stopped:

    # play a tone for the selected region
    cpx.start_tone(REGION_TONE[region]);
    
    # wait the requested amount of time
    time.sleep(duration)
    
    # stop the tone
    cpx.stop_tone()

Below you can see a complete code.py updated with this change:

import time
from adafruit_circuitplayground.express import cpx

cpx.pixels.brightness = 0.1  # adjust NeoPixel brightness to your liking

REGION_LEDS = (
    (5, 6, 7),  # yellow region
    (2, 3, 4),  # blue region
    (7, 8, 9),  # red region
    (0, 1, 2),  # green region
)
 
REGION_COLOR = (
    (255, 255, 0),  # yellow region
    (0, 0, 255),    # blue region
    (255, 0, 0),    # red region
    (0, 255, 0),    # green region
)

REGION_TONE = (
    252,  # yellow region
    209,  # blue region
    310,  # red region
    415,  # green region
)

def light_region(region, duration=1):
    # turn the LEDs for the selected region on
    for led in REGION_LEDS[region]:
        cpx.pixels[led] = REGION_COLOR[region]
    
    # play a tone for the selected region
    cpx.start_tone(REGION_TONE[region]);
    
    # wait the requested amount of time
    time.sleep(duration)
    
    # stop the tone
    cpx.stop_tone()

    # turn the LEDs for the selected region off
    for led in REGION_LEDS[region]:
        cpx.pixels[led] = (0, 0, 0)


while True:
    light_region(0)
    light_region(1)
    light_region(2)
    light_region(3)

As soon as you save this version of the game and reset the board you will not only see but also hear the four lights!

This guide was first published on Jul 24, 2019. It was last updated on Nov 29, 2023.

This page (Adding Sound) was last updated on Jul 19, 2019.

Text editor powered by tinymce.