Initial code commit

This program demonstrates playing each note and then hammers out a very simple tune to show the basics of using the xylophone. True music will have a key strike for a time then a defined rest between keys, or even play a couple keys at the same time. The true musical part is up to you. This uses Grand Central pins D2-D9 (8 solenoids) but you can do many more with the right circuit and a bit more amperage to make sure.
This commit is contained in:
Mike Barela 2019-02-01 16:47:11 -05:00 committed by GitHub
parent 82c99f67dc
commit d7283fb8af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,47 @@
# Adafruit Grand Central Robot Xylophone Demo Program
# Dano Wall and Mike Barela for Adafruit Industries
# MIT License
import time
import board
from digitalio import DigitalInOut, Direction
solenoid_count = 8 # Set the total number of solenoids used
start_pin = 2 # Start at pin D2
# Create the input objects list for solenoids
solenoid = []
for k in range(start_pin, solenoid_count + start_pin + 1):
# get pin # attribute, use string formatting
this_solenoid = DigitalInOut(getattr(board, "D{}".format(k)))
this_solenoid.direction = Direction.OUTPUT
solenoid.append(this_solenoid)
STRIKE_TIME = 0.01 # Time between initiating a strike and turning it off
TIME_BETWEEN = 0.5 # Time between actions in seconds
song = [4, 3, 2, 3, 4, 4, 4, 3, 3, 3, 4, 4, 4, 4, 3, 2, 3, 4, 4, 4, 5, 5, 4, 3, 2]
def play(key, time_to_strike):
solenoid[key].value = True
time.sleep(time_to_strike)
solenoid[key].value = False
def rest(time_to_wait):
time.sleep(time_to_wait)
while True:
# Play each of the bars
for bar in range(solenoid_count):
play(bar, STRIKE_TIME)
rest(TIME_BETWEEN)
time.sleep(1.0) # Wait a bit before playing the song
# Play the notes defined in song
# simple example does not vary time between notes
for bar in range(len(song)):
play(song[bar], STRIKE_TIME)
rest(TIME_BETWEEN)
time.sleep(1.0)