Play a tune in a loop with another output
music_note
music_note
music_note
music_note
my_tune = [ ['d5', 0.4], ['d#5', 0.4],
['f5', 0.4], ['d6', 0.4] ]
for note in my_tune:
pink_led.on()
speaker.play(note)
pink_led.off()
sleep(0.2)
lightbulb Use a `for` loop
If you would like to play music in time with another output, such as an LED, you can use a for loop to play the tune and add code for turning an LED on and off.
Play a tune
music_note
music_note
music_note
music_note
my_tune = [ ['d5', 0.4], ['d#5', 0.4],
['f5', 0.4], ['d6', 0.4] ]
speaker.play(my_tune) # play complete tune
print("Tune finished")
speaker.play(my_tune, wait=False) # start tune
print("Tune started")
lightbulb Play a tune
Use a list to store the note and length of each note. Add the name of the list inside the brackets of speaker.play() to play the tune.