CircuitPython

Onto the final step, the code itself!

For this project, we will be using CircuitPython.

Getting Familiar

CircuitPython is a programming language based on Python, one of the fastest growing programming languages in the world. It is specifically designed to simplify experimenting and learning to code on low-cost microcontroller boards.

CircuitPython is easiest to use within the Mu Editor. If you haven't previously used Mu, this guide will get you started.

If you haven't used Circuit Playground Express with CRICKIT before, make sure you've updated it with the latest special 'seesaw' version of the CPX firmware. This guide will show you how.

Copy that Code!

Copy code.py from the link below and put it in CIRCUITPY root directory. You can work with this code in any text editing application, or open and save with Mu if you prefer. 

import os
import time
import random
import board
from busio import I2C
import audioio
import adafruit_lis3dh
from adafruit_crickit import crickit

# Create accelerometer object for Circuit Playground Express
i2c1 = I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c1, address=0x19)
# Changing RANGE_4_G to RANGE_2_G is more sensitive, RANGE_8_G less sensitive

lis3dh.range = adafruit_lis3dh.RANGE_4_G

# Audio playback object and helper to play a full file
a = audioio.AudioOut(board.A0)

# Find all Wave files on the storage
wavefiles = [file for file in os.listdir("/")
             if (file.endswith(".wav") and not file.startswith("._"))]
print("Audio files found: ", wavefiles)

# mouth servo
mouth_servo = crickit.servo_1
# TowerPro servos like 500/2500 pulsewidths
mouth_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500)

# Servo angles
MOUTH_START = 90
MOUTH_END = 80

# Starting servo location
mouth_servo.angle = MOUTH_START

# Play a wave file and move the mouth while its playing!
def play_file(wavfile):
    print("Playing", wavfile)
    with open(wavfile, "rb") as f:
        wav = audioio.WaveFile(f)
        a.play(wav)
        while a.playing:  # turn servos, motors, etc. during playback
            mouth_servo.angle = MOUTH_END
            time.sleep(0.15)
            mouth_servo.angle = MOUTH_START
            time.sleep(0.15)

while True:
    if lis3dh.shake(shake_threshold=10):  # can also adjust sensitivity here
        print("Shake detected!")
        play_file(random.choice(wavefiles))
    # hang out for tiny bit
    time.sleep(0.05)

Talking Turkey

Now it's time to give your turkey something to say. This public domain turkey gobble sound is a good place to start (file courtesy of Soundbible.com).

Download the WAV file, the drag and drop that WAV file onto your CIRCUITPY drive.

Your CIRCUITPY drive should now contain the code.py file and WAV audio file.

Once you are at this stage you are ready to move on to the next step: testing!

This guide was first published on Nov 21, 2018. It was last updated on Nov 21, 2018. This page (CircuitPython) was last updated on Dec 12, 2018.