/ Grand_Central_Robot_Xylophone / code.py
code.py
1 # SPDX-FileCopyrightText: 2019 Dano Wall for Adafruit Industries 2 # SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries 3 # 4 # SPDX-License-Identifier: MIT 5 6 # Adafruit Grand Central Robot Xylophone Demo Program 7 # Dano Wall and Anne Barela for Adafruit Industries 8 # MIT License 9 10 import time 11 import board 12 from digitalio import DigitalInOut, Direction 13 14 solenoid_count = 8 # Set the total number of solenoids used 15 start_pin = 2 # Start at pin D2 16 17 # Create the input objects list for solenoids 18 solenoid = [] 19 for k in range(start_pin, solenoid_count + start_pin + 1): 20 # get pin # attribute, use string formatting 21 this_solenoid = DigitalInOut(getattr(board, "D{}".format(k))) 22 this_solenoid.direction = Direction.OUTPUT 23 solenoid.append(this_solenoid) 24 25 STRIKE_TIME = 0.01 # Time between initiating a strike and turning it off 26 TIME_BETWEEN = 0.5 # Time between actions in seconds 27 28 song = [3, 4, 5, 4, 3, 3, 3, 4, 4, 4, 3, 3, 3, 3, 4, 5, 4, 3, 3, 3, 2, 2, 3, 4, 5] 29 30 def play(key, time_to_strike): 31 solenoid[key].value = True 32 time.sleep(time_to_strike) 33 solenoid[key].value = False 34 35 def rest(time_to_wait): 36 time.sleep(time_to_wait) 37 38 while True: 39 # Play each of the bars 40 for bar in range(solenoid_count): 41 play(bar, STRIKE_TIME) 42 rest(TIME_BETWEEN) 43 44 time.sleep(1.0) # Wait a bit before playing the song 45 46 # Play the notes defined in song 47 # simple example does not vary time between notes 48 for bar in range(len(song)): 49 play(song[bar], STRIKE_TIME) 50 rest(TIME_BETWEEN) 51 52 time.sleep(1.0)