├── README.md └── TetrisThemeArduino.ino /README.md: -------------------------------------------------------------------------------- 1 | Tetris Theme Arduino 2 | ==================== 3 | 4 | Plays the Tetris theme song from a digital arduino pin. Just connect a piezo or 5 | speaker to **PIN 13** & **GND**. 6 | 7 | 8 | License (MIT) 9 | ------------- 10 | 11 | The following license applies only to the software contained in this repository. 12 | The Tetris theme song copyright is held by Tetris Holding, LLC. 13 | 14 | 15 | Copyright (c) 2015 Luke Cyca 16 | 17 | Permission is hereby granted, free of charge, to any person obtaining a copy of 18 | this software and associated documentation files (the "Software"), to deal in 19 | the Software without restriction, including without limitation the rights to 20 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 21 | the Software, and to permit persons to whom the Software is furnished to do so, 22 | subject to the following conditions: 23 | 24 | The above copyright notice and this permission notice shall be included in all 25 | copies or substantial portions of the Software. 26 | 27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 29 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 30 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 31 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 32 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 33 | 34 | -------------------------------------------------------------------------------- /TetrisThemeArduino.ino: -------------------------------------------------------------------------------- 1 | // By Luke Cyca 2 | // https://lukecyca.com 3 | // https://github.com/lukecyca/TetrisThemeArduino 4 | 5 | #define PIEZO_PIN (13) 6 | 7 | // A rest 8 | #define _R (0) 9 | 10 | // Note frequencies based on http://www.phy.mtu.edu/~suits/notefreqs.html 11 | #define _C0 (16.35) 12 | #define _CS0 (17.32) 13 | #define _D0 (18.35) 14 | #define _DS0 (19.45) 15 | #define _E0 (20.60) 16 | #define _F0 (21.83) 17 | #define _FS0 (23.12) 18 | #define _G0 (24.50) 19 | #define _GS0 (25.96) 20 | #define _A0 (27.50) 21 | #define _AS0 (29.14) 22 | #define _B0 (30.87) 23 | #define _C1 (32.70) 24 | #define _CS1 (34.65) 25 | #define _D1 (36.71) 26 | #define _DS1 (38.89) 27 | #define _E1 (41.20) 28 | #define _F1 (43.65) 29 | #define _FS1 (46.25) 30 | #define _G1 (49.00) 31 | #define _GS1 (51.91) 32 | #define _A1 (55.00) 33 | #define _AS1 (58.27) 34 | #define _B1 (61.74) 35 | #define _C2 (65.41) 36 | #define _CS2 (69.30) 37 | #define _D2 (73.42) 38 | #define _DS2 (77.78) 39 | #define _E2 (82.41) 40 | #define _F2 (87.31) 41 | #define _FS2 (92.50) 42 | #define _G2 (98.00) 43 | #define _GS2 (103.83) 44 | #define _A2 (110.00) 45 | #define _AS2 (116.54) 46 | #define _B2 (123.47) 47 | #define _C3 (130.81) 48 | #define _CS3 (138.59) 49 | #define _D3 (146.83) 50 | #define _DS3 (155.56) 51 | #define _E3 (164.81) 52 | #define _F3 (174.61) 53 | #define _FS3 (185.00) 54 | #define _G3 (196.00) 55 | #define _GS3 (207.65) 56 | #define _A3 (220.00) 57 | #define _AS3 (233.08) 58 | #define _B3 (246.94) 59 | #define _C4 (261.63) 60 | #define _CS4 (277.18) 61 | #define _D4 (293.66) 62 | #define _DS4 (311.13) 63 | #define _E4 (329.63) 64 | #define _F4 (349.23) 65 | #define _FS4 (369.99) 66 | #define _G4 (392.00) 67 | #define _GS4 (415.30) 68 | #define _A4 (440.00) 69 | #define _AS4 (466.16) 70 | #define _B4 (493.88) 71 | #define _C5 (523.25) 72 | #define _CS5 (554.37) 73 | #define _D5 (587.33) 74 | #define _DS5 (622.25) 75 | #define _E5 (659.25) 76 | #define _F5 (698.46) 77 | #define _FS5 (739.99) 78 | #define _G5 (783.99) 79 | #define _GS5 (830.61) 80 | #define _A5 (880.00) 81 | #define _AS5 (932.33) 82 | #define _B5 (987.77) 83 | #define _C6 (1046.50) 84 | #define _CS6 (1108.73) 85 | #define _D6 (1174.66) 86 | #define _DS6 (1244.51) 87 | #define _E6 (1318.51) 88 | #define _F6 (1396.91) 89 | #define _FS6 (1479.98) 90 | #define _G6 (1567.98) 91 | #define _GS6 (1661.22) 92 | #define _A6 (1760.00) 93 | #define _AS6 (1864.66) 94 | #define _B6 (1975.53) 95 | #define _C7 (2093.00) 96 | #define _CS7 (2217.46) 97 | #define _D7 (2349.32) 98 | #define _DS7 (2489.02) 99 | #define _E7 (2637.02) 100 | #define _F7 (2793.83) 101 | #define _FS7 (2959.96) 102 | #define _G7 (3135.96) 103 | #define _GS7 (3322.44) 104 | #define _A7 (3520.00) 105 | #define _AS7 (3729.31) 106 | #define _B7 (3951.07) 107 | #define _C8 (4186.01) 108 | #define _CS8 (4434.92) 109 | #define _D8 (4698.63) 110 | #define _DS8 (4978.03) 111 | #define _E8 (5274.04) 112 | #define _F8 (5587.65) 113 | #define _FS8 (5919.91) 114 | #define _G8 (6271.93) 115 | #define _GS8 (6644.88) 116 | #define _A8 (7040.00) 117 | #define _AS8 (7458.62) 118 | #define _B8 (7902.13) 119 | 120 | // beats per minute 121 | #define BPM (120.0) 122 | 123 | 124 | float lead_notes[] = { 125 | // part 1 126 | _E5, _B4, _C5, _D5, _C5, _B4, _A4, _A4, _C5, _E5, _D5, _C5, _B4, _B4, _C5, _D5, _E5, _C5, _A4, _A4, _R, 127 | _D5, _F5, _A5, _G5, _F5, _E5, _C5, _E5, _D5, _C5, _B4, _B4, _C5, _D5, _E5, _C5, _A4, _A4, _R, 128 | 129 | // part 2 130 | _E4, _C4, _D4, _B3, _C4, _A3, _GS3, _B3, 131 | _E4, _C4, _D4, _B3, _C4, _E4, _A4, _A4, _GS4, _R 132 | 133 | }; 134 | float lead_times[] = { 135 | // part 1 136 | 1.0, 0.5, 0.5, 1.0, 0.5, 0.5, 1.0, 0.5, 0.5, 1.0, 0.5, 0.5, 1.0, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 137 | 1.5, 0.5, 1.0, 0.5, 0.5, 1.5, 0.5, 1.0, 0.5, 0.5, 1.0, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 138 | 139 | // part 2 140 | 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 141 | 2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 3.0, 1.0 142 | 143 | }; 144 | 145 | float bass_notes[] = { 146 | // part 1 147 | _E2, _E3, _E2, _E3, _E2, _E3, _E2, _E3, _A1, _A2, _A1, _A2, _A1, _A2, _A1, _A2, _GS1, _GS2, _GS1, _GS2, _GS1, _GS2, _GS1, _GS2, _A1, _A2, _A1, _A2, _A1, _B2, _C3, _E3, 148 | _D2, _D3, _D2, _D3, _D2, _D3, _D2, _D3, _C2, _C3, _C2, _C3, _C2, _C3, _C2, _C3, _B1, _B2, _B1, _B2, _B1, _B2, _B1, _B2, _A1, _A2, _A1, _A2, _A1, _A2, _A1, _A2, 149 | 150 | // part 2 151 | _A1, _E2, _A1, _E2, _A1, _E2, _A1, _E2, _GS1, _E2, _GS1, _E2, _GS1, _E2, _GS1, _E2, _A1, _E2, _A1, _E2, _A1, _E2, _A1, _E2, _GS1, _E2, _GS1, _E2, _GS1, _E2, _GS1, _E2, 152 | _A1, _E2, _A1, _E2, _A1, _E2, _A1, _E2, _GS1, _E2, _GS1, _E2, _GS1, _E2, _GS1, _E2, _A1, _E2, _A1, _E2, _A1, _E2, _A1, _E2, _GS1, _E2, _GS1, _E2, _GS1, _E2, _GS1, _E2 153 | 154 | 155 | }; 156 | float bass_times[] = { 157 | // part 1 158 | 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 159 | 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 160 | 161 | // part 2 162 | 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 163 | 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 164 | 165 | }; 166 | 167 | 168 | // duration is in microseconds 169 | void play_one_note(float frequency, unsigned long duration) { 170 | unsigned long period = 1000000.0/frequency; 171 | 172 | for (unsigned int cycles=duration/period; cycles>0; cycles--) { 173 | // half the time on 174 | digitalWrite(PIEZO_PIN, HIGH); 175 | delayMicroseconds( period/2 ); 176 | 177 | // half the time off 178 | digitalWrite(PIEZO_PIN, LOW); 179 | delayMicroseconds( period/2 ); 180 | } 181 | 182 | // If the duration wasn't a multiple of the period, delay the remainder 183 | delayMicroseconds(duration % period); 184 | } 185 | 186 | // Time (in microseconds) to spend on each note while simulating polyphony 187 | // If this is too small, low frequency notes will be inaudible. 188 | #define POLY_DELTA (14400) 189 | 190 | // duration is in microseconds 191 | void play_two_notes(float freq1, float freq2, unsigned long duration) { 192 | for (unsigned long t=0; t 0 && bass_freq > 0) { 221 | play_two_notes(lead_freq, bass_freq, duration); 222 | } else if (lead_freq > 0) { 223 | play_one_note(lead_freq, duration); 224 | } else if (bass_freq > 0) { 225 | play_one_note(bass_freq, duration); 226 | } else { 227 | delay( duration/1000 ); 228 | } 229 | 230 | // Advance lead note 231 | curr_lead_note_time_remaining -= note_value; 232 | if (curr_lead_note_time_remaining < 0.001) { 233 | curr_lead_note++; 234 | curr_lead_note_time_remaining = lead_times[curr_lead_note]; 235 | } 236 | 237 | // Advance bass note 238 | curr_bass_note_time_remaining -= note_value; 239 | if (curr_bass_note_time_remaining < 0.001) { 240 | curr_bass_note++; 241 | curr_bass_note_time_remaining = bass_times[curr_bass_note]; 242 | } 243 | } 244 | 245 | } 246 | 247 | 248 | --------------------------------------------------------------------------------