From d7283fb8af1971600408dc10aa564a1a59808dca Mon Sep 17 00:00:00 2001 From: Mike Barela Date: Fri, 1 Feb 2019 16:47:11 -0500 Subject: [PATCH] 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. --- Grand_Central_Robot_Xylophone/code.py | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Grand_Central_Robot_Xylophone/code.py diff --git a/Grand_Central_Robot_Xylophone/code.py b/Grand_Central_Robot_Xylophone/code.py new file mode 100644 index 00000000..b5e2e2b1 --- /dev/null +++ b/Grand_Central_Robot_Xylophone/code.py @@ -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)