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:
parent
82c99f67dc
commit
d7283fb8af
1 changed files with 47 additions and 0 deletions
47
Grand_Central_Robot_Xylophone/code.py
Normal file
47
Grand_Central_Robot_Xylophone/code.py
Normal 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)
|
||||
Loading…
Reference in a new issue