code.py
1 # SPDX-FileCopyrightText: 2018 John Edgar Park for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 Plays the Carmen Sandiego theme song 7 Gemma M0 with Piezo on D0 and GND 8 """ 9 10 import time 11 12 import board 13 import pwmio 14 15 piezo = pwmio.PWMOut(board.D0, duty_cycle=0, frequency=440, 16 variable_frequency=True) 17 18 tempo = 1.6 19 # tempo is length of whole note in seconds, e.g. 1.5 20 # set up time signature 21 whole_note = tempo # adjust this to change tempo of everything 22 dotted_whole_note = whole_note * 1.5 23 # these notes are fractions of the whole note 24 half_note = whole_note / 2 25 dotted_half_note = half_note * 1.5 26 quarter_note = whole_note / 4 27 dotted_quarter_note = quarter_note * 1.5 28 eighth_note = whole_note / 8 29 dotted_eighth_note = eighth_note * 1.5 30 sixteenth_note = whole_note / 16 31 32 # set up note values 33 A2 = 110 34 As2 = 117 # 's' stands for sharp: A#2 35 Bb2 = 117 36 B2 = 123 37 38 C3 = 131 39 Cs3 = 139 40 Db3 = 139 41 D3 = 147 42 Ds3 = 156 43 Eb3 = 156 44 E3 = 165 45 F3 = 175 46 Fs3 = 185 47 Gb3 = 185 48 G3 = 196 49 Gs3 = 208 50 Ab3 = 208 51 A3 = 220 52 As3 = 233 53 Bb3 = 233 54 B3 = 247 55 56 C4 = 262 57 Cs4 = 277 58 Db4 = 277 59 D4 = 294 60 Ds4 = 311 61 Eb4 = 311 62 E4 = 330 63 F4 = 349 64 Fs4 = 370 65 Gb4 = 370 66 G4 = 392 67 Gs4 = 415 68 Ab4 = 415 69 A4 = 440 70 As4 = 466 71 Bb4 = 466 72 B4 = 494 73 74 C5 = 523 75 Cs5 = 554 76 Db5 = 554 77 D5 = 587 78 Ds5 = 622 79 Eb5 = 622 80 E5 = 659 81 F5 = 698 82 Fs5 = 740 83 Gb5 = 740 84 G5 = 784 85 Gs5 = 831 86 Ab5 = 831 87 A5 = 880 88 As5 = 932 89 Bb5 = 932 90 B5 = 987 91 92 # here's another way to express the note pitch, double the previous octave 93 C6 = C5 * 2 94 Cs6 = Cs5 * 2 95 Db6 = Db5 * 2 96 D6 = D5 * 2 97 Ds6 = Ds5 * 2 98 Eb6 = Eb5 * 2 99 E6 = E5 * 2 100 F6 = F5 * 2 101 Fs6 = Fs5 * 2 102 Gb6 = Gb5 * 2 103 G6 = G5 * 2 104 Gs6 = Gs5 * 2 105 Ab6 = Ab5 * 2 106 A6 = A5 * 2 107 As6 = As5 * 2 108 Bb6 = Bb5 * 2 109 B6 = B5 * 2 110 111 rst = 24000 # rest is just a tone out of normal hearing range 112 113 carmen01 = [[As4, quarter_note], 114 [As4, eighth_note], 115 [rst, eighth_note], 116 [rst, sixteenth_note], 117 [B4, eighth_note], 118 [rst, sixteenth_note], 119 [B4, eighth_note], 120 [B4, eighth_note]] 121 122 carmen02 = [[Gs4, quarter_note], 123 [Gs4, eighth_note], 124 [rst, eighth_note], 125 [rst, sixteenth_note], 126 [Gs4, eighth_note], 127 [rst, sixteenth_note], 128 [Gs4, eighth_note], 129 [B4, eighth_note]] 130 131 carmen03 = [[Gs4, quarter_note], 132 [Gs4, eighth_note], 133 [rst, eighth_note], 134 [rst, quarter_note], 135 [Cs4, eighth_note], 136 [Cs4, eighth_note]] 137 138 carmen04 = [[As4, eighth_note], 139 [As4, sixteenth_note], 140 [As4, eighth_note], 141 [As4, eighth_note], 142 [B4, sixteenth_note], 143 [B4, quarter_note], 144 [B4, eighth_note], 145 [B4, eighth_note]] 146 147 carmen05 = [[Gs4, eighth_note], 148 [Fs4, eighth_note], 149 [Gs4, eighth_note], 150 [Fs4, sixteenth_note], 151 [Cs5, sixteenth_note], 152 [Cs5, sixteenth_note], 153 [As4, eighth_note], 154 [As4, sixteenth_note], 155 [Fs4, eighth_note], 156 [Fs4, eighth_note]] 157 158 carmen06 = [[Gs4, eighth_note], 159 [Fs4, eighth_note], 160 [Gs4, eighth_note], 161 [Fs4, sixteenth_note], 162 [Gs4, sixteenth_note], 163 [Gs4, sixteenth_note], 164 [Gs4, eighth_note], 165 [rst, eighth_note], 166 [rst, eighth_note], 167 [Fs4, eighth_note]] 168 169 carmen07 = [[Gs4, eighth_note], 170 [Fs4, eighth_note], 171 [Gs4, eighth_note], 172 [Fs4, sixteenth_note], 173 [Cs5, sixteenth_note], 174 [Cs5, sixteenth_note], 175 [As4, eighth_note], 176 [As4, sixteenth_note], 177 [Gs4, eighth_note], 178 [Fs4, eighth_note]] 179 180 carmen08 = [[Fs4, eighth_note], 181 [rst, eighth_note], 182 [Fs4, eighth_note], 183 [Fs4, eighth_note], 184 [Gs4, eighth_note], 185 [rst, eighth_note], 186 [Gs4, eighth_note], 187 [rst, eighth_note], 188 [E3, eighth_note], 189 [E3, eighth_note], 190 [E3, eighth_note], 191 [E3, sixteenth_note], 192 [Fs3, eighth_note], 193 [Fs3, eighth_note], 194 [rst, quarter_note]] 195 196 197 def song_playback(song): 198 for n in range(len(song)): 199 piezo.frequency = (song[n][0]) 200 piezo.duty_cycle = 65536 // 2 # on 50% 201 time.sleep(song[n][1]) # note duration 202 piezo.duty_cycle = 0 # off 203 time.sleep(0.01) 204 205 206 song_playback(carmen01) 207 song_playback(carmen02) 208 song_playback(carmen01) 209 song_playback(carmen03) 210 song_playback(carmen04) 211 song_playback(carmen05) 212 song_playback(carmen04) 213 song_playback(carmen06) 214 song_playback(carmen04) 215 song_playback(carmen07) 216 song_playback(carmen08) 217 song_playback(carmen08)