Components and supplies
Resistor 560 ohm
Photo resistor
Arduino UNO
Resistor 10k ohm
LED (generic)
Single Turn Potentiometer- 10k ohms
Pushbutton switch 12mm
Breadboard (generic)
Jumper wires (generic)
Buzzer
Project description
Code
Rick Roll Box Code
arduino
Remember to calibrate the light sensor by keeping box open and exposed to ambient light conditions during initial setup() after upload.
1/* RickRollBoxCode 2 3 AUTHOR: Samantha Lagestee 4 Copyright 2017 samilagestee at gmail dot com 5 6 This program is free software: you can redistribute it and/or 7 modify it under the terms of the GNU General Public License as 8 published by the Free Software Foundation, either version 3 of 9 the License, or (at your option) any later version. 10 11 DISCLAIMER: The song "Never Gonna Give You Up" by Rick Astley 12 is not the creative property of the author. This code simply 13 plays a Piezo buzzer rendition of the song. 14*/ 15 16#define a3f 208 // 208 Hz 17#define b3f 233 // 233 Hz 18#define b3 247 // 247 Hz 19#define c4 261 // 261 Hz MIDDLE C 20#define c4s 277 // 277 Hz 21#define e4f 311 // 311 Hz 22#define f4 349 // 349 Hz 23#define a4f 415 // 415 Hz 24#define b4f 466 // 466 Hz 25#define b4 493 // 493 Hz 26#define c5 523 // 523 Hz 27#define c5s 554 // 554 Hz 28#define e5f 622 // 622 Hz 29#define f5 698 // 698 Hz 30#define f5s 740 // 740 Hz 31#define a5f 831 // 831 Hz 32 33#define rest -1 34 35int piezo = 7; 36int led = 9; 37int button = 2; 38int sensor = A0; 39 40volatile int beatlength = 100; // determines tempo 41float beatseparationconstant = 0.3; 42 43int threshold; 44 45int a; // part index 46int b; // song index 47int c; // lyric index 48 49boolean flag; 50 51// Parts 1 and 2 (Intro) 52 53int song1_intro_melody[] = 54{c5s, e5f, e5f, f5, a5f, f5s, f5, e5f, c5s, e5f, rest, a4f, a4f}; 55 56int song1_intro_rhythmn[] = 57{6, 10, 6, 6, 1, 1, 1, 1, 6, 10, 4, 2, 10}; 58 59// Parts 3 or 5 (Verse 1) 60 61int song1_verse1_melody[] = 62{ rest, c4s, c4s, c4s, c4s, e4f, rest, c4, b3f, a3f, 63 rest, b3f, b3f, c4, c4s, a3f, a4f, a4f, e4f, 64 rest, b3f, b3f, c4, c4s, b3f, c4s, e4f, rest, c4, b3f, b3f, a3f, 65 rest, b3f, b3f, c4, c4s, a3f, a3f, e4f, e4f, e4f, f4, e4f, 66 c4s, e4f, f4, c4s, e4f, e4f, e4f, f4, e4f, a3f, 67 rest, b3f, c4, c4s, a3f, rest, e4f, f4, e4f 68}; 69 70int song1_verse1_rhythmn[] = 71{ 2, 1, 1, 1, 1, 2, 1, 1, 1, 5, 72 1, 1, 1, 1, 3, 1, 2, 1, 5, 73 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 74 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 4, 75 5, 1, 1, 1, 1, 1, 1, 1, 2, 2, 76 2, 1, 1, 1, 3, 1, 1, 1, 3 77}; 78 79char* lyrics_verse1[] = 80{ "We're ", "no ", "strangers ", "", "to ", "love ", "", "\ \ 81", 82 "You ", "know ", "the ", "rules ", "and ", "so ", "do ", "I\ \ 83", 84 "A ", "full ", "commitment's ", "", "", "what ", "I'm ", "thinking ", "", "of", "\ \ 85", 86 "You ", "wouldn't ", "", "get ", "this ", "from ", "any ", "", "other ", "", "guy\ \ 87", 88 "I ", "just ", "wanna ", "", "tell ", "you ", "how ", "I'm ", "feeling", "\ \ 89", 90 "Gotta ", "", "make ", "you ", "understand", "", "\ \ 91" 92}; 93 94// Parts 4 or 6 (Chorus) 95 96int song1_chorus_melody[] = 97{ b4f, b4f, a4f, a4f, 98 f5, f5, e5f, b4f, b4f, a4f, a4f, e5f, e5f, c5s, c5, b4f, 99 c5s, c5s, c5s, c5s, 100 c5s, e5f, c5, b4f, a4f, a4f, a4f, e5f, c5s, 101 b4f, b4f, a4f, a4f, 102 f5, f5, e5f, b4f, b4f, a4f, a4f, a5f, c5, c5s, c5, b4f, 103 c5s, c5s, c5s, c5s, 104 c5s, e5f, c5, b4f, a4f, rest, a4f, e5f, c5s, rest 105}; 106 107int song1_chorus_rhythmn[] = 108{ 1, 1, 1, 1, 109 3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2, 110 1, 1, 1, 1, 111 3, 3, 3, 1, 2, 2, 2, 4, 8, 112 1, 1, 1, 1, 113 3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2, 114 1, 1, 1, 1, 115 3, 3, 3, 1, 2, 2, 2, 4, 8, 4 116}; 117 118char* lyrics_chorus[] = 119{ "Never ", "", "gonna ", "", "give ", "you ", "up\ \ 120", 121 "Never ", "", "gonna ", "", "let ", "you ", "down", "", "\ \ 122", 123 "Never ", "", "gonna ", "", "run ", "around ", "", "", "", "and ", "desert ", "", "you\ \ 124", 125 "Never ", "", "gonna ", "", "make ", "you ", "cry\ \ 126", 127 "Never ", "", "gonna ", "", "say ", "goodbye ", "", "", "\ \ 128", 129 "Never ", "", "gonna ", "", "tell ", "a ", "lie ", "", "", "and ", "hurt ", "you\ \ 130" 131}; 132 133void setup() 134{ 135 pinMode(piezo, OUTPUT); 136 pinMode(led, OUTPUT); 137 pinMode(button, INPUT_PULLUP); 138 pinMode(sensor, INPUT); 139 attachInterrupt(digitalPinToInterrupt(button), getFaster, FALLING); 140 digitalWrite(led, LOW); 141 Serial.begin(9600); 142 flag = false; 143 a = 4; 144 b = 0; 145 c = 0; 146 threshold = analogRead(sensor) + 250; 147} 148 149void loop() 150{ 151 int sensorreading = analogRead(sensor); 152 if (sensorreading < threshold) { // if bright, play 153 flag = true; 154 } 155 else if (sensorreading > threshold) { // if dark, pause 156 flag = false; 157 } 158 159 // play next step in song 160 if (flag == true) { 161 play(); 162 } 163} 164 165void play() { 166 int notelength; 167 if (a == 1 || a == 2) { 168 // intro 169 notelength = beatlength * song1_intro_rhythmn[b]; 170 if (song1_intro_melody[b] > 0) { 171 digitalWrite(led, HIGH); 172 tone(piezo, song1_intro_melody[b], notelength); 173 } 174 b++; 175 if (b >= sizeof(song1_intro_melody) / sizeof(int)) { 176 a++; 177 b = 0; 178 c = 0; 179 } 180 } 181 else if (a == 3 || a == 5) { 182 // verse 183 notelength = beatlength * 2 * song1_verse1_rhythmn[b]; 184 if (song1_verse1_melody[b] > 0) { 185 digitalWrite(led, HIGH); 186 Serial.print(lyrics_verse1[c]); 187 tone(piezo, song1_verse1_melody[b], notelength); 188 c++; 189 } 190 b++; 191 if (b >= sizeof(song1_verse1_melody) / sizeof(int)) { 192 a++; 193 b = 0; 194 c = 0; 195 } 196 } 197 else if (a == 4 || a == 6) { 198 // chorus 199 notelength = beatlength * song1_chorus_rhythmn[b]; 200 if (song1_chorus_melody[b] > 0) { 201 digitalWrite(led, HIGH); 202 Serial.print(lyrics_chorus[c]); 203 tone(piezo, song1_chorus_melody[b], notelength); 204 c++; 205 } 206 b++; 207 if (b >= sizeof(song1_chorus_melody) / sizeof(int)) { 208 Serial.println(""); 209 a++; 210 b = 0; 211 c = 0; 212 } 213 } 214 delay(notelength); 215 noTone(piezo); 216 digitalWrite(led, LOW); 217 delay(notelength * beatseparationconstant); 218 if (a == 7) { // loop back around to beginning of song 219 a = 1; 220 } 221} 222 223void getFaster() { // decrease beat length in order to increase tempo 224 beatlength = beatlength / 2; 225 if (beatlength < 20) { // loop back to original tempo 226 beatlength = 100; 227 } 228} 229 230
Rick Roll Box Code
arduino
Remember to calibrate the light sensor by keeping box open and exposed to ambient light conditions during initial setup() after upload.
1/* RickRollBoxCode 2 3 AUTHOR: Samantha Lagestee 4 Copyright 2017 samilagestee at gmail dot com 5 6 This program is free software: you can redistribute it and/or 7 modify it under the terms of the GNU General Public License as 8 published by the Free Software Foundation, either version 3 of 9 the License, or (at your option) any later version. 10 11 DISCLAIMER: The song "Never Gonna Give You Up" by Rick Astley 12 is not the creative property of the author. This code simply 13 plays a Piezo buzzer rendition of the song. 14*/ 15 16#define a3f 208 // 208 Hz 17#define b3f 233 // 233 Hz 18#define b3 247 // 247 Hz 19#define c4 261 // 261 Hz MIDDLE C 20#define c4s 277 // 277 Hz 21#define e4f 311 // 311 Hz 22#define f4 349 // 349 Hz 23#define a4f 415 // 415 Hz 24#define b4f 466 // 466 Hz 25#define b4 493 // 493 Hz 26#define c5 523 // 523 Hz 27#define c5s 554 // 554 Hz 28#define e5f 622 // 622 Hz 29#define f5 698 // 698 Hz 30#define f5s 740 // 740 Hz 31#define a5f 831 // 831 Hz 32 33#define rest -1 34 35int piezo = 7; 36int led = 9; 37int button = 2; 38int sensor = A0; 39 40volatile int beatlength = 100; // determines tempo 41float beatseparationconstant = 0.3; 42 43int threshold; 44 45int a; // part index 46int b; // song index 47int c; // lyric index 48 49boolean flag; 50 51// Parts 1 and 2 (Intro) 52 53int song1_intro_melody[] = 54{c5s, e5f, e5f, f5, a5f, f5s, f5, e5f, c5s, e5f, rest, a4f, a4f}; 55 56int song1_intro_rhythmn[] = 57{6, 10, 6, 6, 1, 1, 1, 1, 6, 10, 4, 2, 10}; 58 59// Parts 3 or 5 (Verse 1) 60 61int song1_verse1_melody[] = 62{ rest, c4s, c4s, c4s, c4s, e4f, rest, c4, b3f, a3f, 63 rest, b3f, b3f, c4, c4s, a3f, a4f, a4f, e4f, 64 rest, b3f, b3f, c4, c4s, b3f, c4s, e4f, rest, c4, b3f, b3f, a3f, 65 rest, b3f, b3f, c4, c4s, a3f, a3f, e4f, e4f, e4f, f4, e4f, 66 c4s, e4f, f4, c4s, e4f, e4f, e4f, f4, e4f, a3f, 67 rest, b3f, c4, c4s, a3f, rest, e4f, f4, e4f 68}; 69 70int song1_verse1_rhythmn[] = 71{ 2, 1, 1, 1, 1, 2, 1, 1, 1, 5, 72 1, 1, 1, 1, 3, 1, 2, 1, 5, 73 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 74 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 4, 75 5, 1, 1, 1, 1, 1, 1, 1, 2, 2, 76 2, 1, 1, 1, 3, 1, 1, 1, 3 77}; 78 79char* lyrics_verse1[] = 80{ "We're ", "no ", "strangers ", "", "to ", "love ", "", "\ \ 81", 82 "You ", "know ", "the ", "rules ", "and ", "so ", "do ", "I\ \ 83", 84 "A ", "full ", "commitment's ", "", "", "what ", "I'm ", "thinking ", "", "of", "\ \ 85", 86 "You ", "wouldn't ", "", "get ", "this ", "from ", "any ", "", "other ", "", "guy\ \ 87", 88 "I ", "just ", "wanna ", "", "tell ", "you ", "how ", "I'm ", "feeling", "\ \ 89", 90 "Gotta ", "", "make ", "you ", "understand", "", "\ \ 91" 92}; 93 94// Parts 4 or 6 (Chorus) 95 96int song1_chorus_melody[] = 97{ b4f, b4f, a4f, a4f, 98 f5, f5, e5f, b4f, b4f, a4f, a4f, e5f, e5f, c5s, c5, b4f, 99 c5s, c5s, c5s, c5s, 100 c5s, e5f, c5, b4f, a4f, a4f, a4f, e5f, c5s, 101 b4f, b4f, a4f, a4f, 102 f5, f5, e5f, b4f, b4f, a4f, a4f, a5f, c5, c5s, c5, b4f, 103 c5s, c5s, c5s, c5s, 104 c5s, e5f, c5, b4f, a4f, rest, a4f, e5f, c5s, rest 105}; 106 107int song1_chorus_rhythmn[] = 108{ 1, 1, 1, 1, 109 3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2, 110 1, 1, 1, 1, 111 3, 3, 3, 1, 2, 2, 2, 4, 8, 112 1, 1, 1, 1, 113 3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2, 114 1, 1, 1, 1, 115 3, 3, 3, 1, 2, 2, 2, 4, 8, 4 116}; 117 118char* lyrics_chorus[] = 119{ "Never ", "", "gonna ", "", "give ", "you ", "up\ \ 120", 121 "Never ", "", "gonna ", "", "let ", "you ", "down", "", "\ \ 122", 123 "Never ", "", "gonna ", "", "run ", "around ", "", "", "", "and ", "desert ", "", "you\ \ 124", 125 "Never ", "", "gonna ", "", "make ", "you ", "cry\ \ 126", 127 "Never ", "", "gonna ", "", "say ", "goodbye ", "", "", "\ \ 128", 129 "Never ", "", "gonna ", "", "tell ", "a ", "lie ", "", "", "and ", "hurt ", "you\ \ 130" 131}; 132 133void setup() 134{ 135 pinMode(piezo, OUTPUT); 136 pinMode(led, OUTPUT); 137 pinMode(button, INPUT_PULLUP); 138 pinMode(sensor, INPUT); 139 attachInterrupt(digitalPinToInterrupt(button), getFaster, FALLING); 140 digitalWrite(led, LOW); 141 Serial.begin(9600); 142 flag = false; 143 a = 4; 144 b = 0; 145 c = 0; 146 threshold = analogRead(sensor) + 250; 147} 148 149void loop() 150{ 151 int sensorreading = analogRead(sensor); 152 if (sensorreading < threshold) { // if bright, play 153 flag = true; 154 } 155 else if (sensorreading > threshold) { // if dark, pause 156 flag = false; 157 } 158 159 // play next step in song 160 if (flag == true) { 161 play(); 162 } 163} 164 165void play() { 166 int notelength; 167 if (a == 1 || a == 2) { 168 // intro 169 notelength = beatlength * song1_intro_rhythmn[b]; 170 if (song1_intro_melody[b] > 0) { 171 digitalWrite(led, HIGH); 172 tone(piezo, song1_intro_melody[b], notelength); 173 } 174 b++; 175 if (b >= sizeof(song1_intro_melody) / sizeof(int)) { 176 a++; 177 b = 0; 178 c = 0; 179 } 180 } 181 else if (a == 3 || a == 5) { 182 // verse 183 notelength = beatlength * 2 * song1_verse1_rhythmn[b]; 184 if (song1_verse1_melody[b] > 0) { 185 digitalWrite(led, HIGH); 186 Serial.print(lyrics_verse1[c]); 187 tone(piezo, song1_verse1_melody[b], notelength); 188 c++; 189 } 190 b++; 191 if (b >= sizeof(song1_verse1_melody) / sizeof(int)) { 192 a++; 193 b = 0; 194 c = 0; 195 } 196 } 197 else if (a == 4 || a == 6) { 198 // chorus 199 notelength = beatlength * song1_chorus_rhythmn[b]; 200 if (song1_chorus_melody[b] > 0) { 201 digitalWrite(led, HIGH); 202 Serial.print(lyrics_chorus[c]); 203 tone(piezo, song1_chorus_melody[b], notelength); 204 c++; 205 } 206 b++; 207 if (b >= sizeof(song1_chorus_melody) / sizeof(int)) { 208 Serial.println(""); 209 a++; 210 b = 0; 211 c = 0; 212 } 213 } 214 delay(notelength); 215 noTone(piezo); 216 digitalWrite(led, LOW); 217 delay(notelength * beatseparationconstant); 218 if (a == 7) { // loop back around to beginning of song 219 a = 1; 220 } 221} 222 223void getFaster() { // decrease beat length in order to increase tempo 224 beatlength = beatlength / 2; 225 if (beatlength < 20) { // loop back to original tempo 226 beatlength = 100; 227 } 228} 229 230
Downloadable files
Fritzing Diagram
Fritzing Diagram
Comments
Only logged in users can leave comments
slagestee
0 Followers
•0 Projects
Table of contents
Intro
27
0