jack
Published

Rats Rock!

Learn the truth about rats! They are not evil monsters but hungry critters that mean no harm to people.

BeginnerShowcase (no instructions)203
Rats Rock!

Things used in this project

Hardware components

Circuit Playground Express
Adafruit Circuit Playground Express
×1
Alligator Clips
Alligator Clips
×1
Battery Holder, 3 x AAA
Battery Holder, 3 x AAA
×1
Speaker: 3W, 4 ohms
Speaker: 3W, 4 ohms
×1

Software apps and online services

CircuitPython

Story

Read more

Custom parts and enclosures

20201011_183134_pyRPprOobO.jpg

Schematics

20201013_125620_daTeEGxDQg.jpg

Code

code.py

Python
# Circuit Playground 808 Drum machine
import time
import board
import touchio
import digitalio
from random import *


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!

bpm = 120  # Beats per minute, change this to suit your tempo

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

# Make the input capacitive touchpads
capPins = (board.A1, board.A2, board.A3, board.A4, board.A5,
           board.A6, board.TX)

touchPad = []
for i in range(7):
    touchPad.append(touchio.TouchIn(capPins[i]))

# The seven files assigned to the touchpads
audiofiles = ["1.wav", "2.wav", "3.wav",
              "4.wav", "5.wav", "6.wav",
              "7.wav","8.wav","9.wav"]

audio = AudioOut(board.SPEAKER)


def play_file(filename):
    print("playing file " + filename)
    file = open(filename, "rb")
    wave = WaveFile(file)
    audio.play(wave)
    time.sleep(bpm / 100)  # Sixteenth note delay


while True:


        if touchPad[2].value:
            x = randint(0, 2)
            play_file(audiofiles[x])

        if touchPad[3].value:
            x = randint(3, 5)
            play_file(audiofiles[x])

        if touchPad[6].value:
            x = randint(6, 8)
            play_file(audiofiles[x])

Credits

jack

jack

4 projects • 0 followers

Comments