Advertisement
Guest User

RickRoll

a guest
Aug 22nd, 2018
2,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. /* Rick Roll Code
  2. AUTHOR: Samantha Lagestee
  3. Copyright 2017 samilagestee at gmail dot com
  4.  
  5. This program is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation, either version 3 of
  8. the License, or (at your option) any later version.
  9.  
  10. DISCLAIMER: The song "Never Gonna Give You Up" by Rick Astley
  11. is not the creative property of the author. This code simply
  12. plays a Piezo buzzer rendition of the song.
  13. */
  14.  
  15. #define a3f 208 // 208 Hz
  16. #define b3f 233 // 233 Hz
  17. #define b3 247 // 247 Hz
  18. #define c4 261 // 261 Hz MIDDLE C
  19. #define c4s 277 // 277 Hz
  20. #define e4f 311 // 311 Hz
  21. #define f4 349 // 349 Hz
  22. #define a4f 415 // 415 Hz
  23. #define b4f 466 // 466 Hz
  24. #define b4 493 // 493 Hz
  25. #define c5 523 // 523 Hz
  26. #define c5s 554 // 554 Hz
  27. #define e5f 622 // 622 Hz
  28. #define f5 698 // 698 Hz
  29. #define f5s 740 // 740 Hz
  30. #define a5f 831 // 831 Hz
  31.  
  32. #define rest -1
  33.  
  34. // change these pins according to your setup
  35. int piezo = 7;
  36. int led = 9;
  37.  
  38. volatile int beatlength = 100; // determines tempo
  39. float beatseparationconstant = 0.3;
  40.  
  41. int a; // part index
  42. int b; // song index
  43. int c; // lyric index
  44.  
  45. boolean flag; // play/pause
  46.  
  47. // Parts 1 and 2 (Intro)
  48.  
  49. int song1_intro_melody[] =
  50. {c5s, e5f, e5f, f5, a5f, f5s, f5, e5f, c5s, e5f, rest, a4f, a4f};
  51.  
  52. int song1_intro_rhythmn[] =
  53. {6, 10, 6, 6, 1, 1, 1, 1, 6, 10, 4, 2, 10};
  54.  
  55. // Parts 3 or 5 (Verse 1)
  56.  
  57. int song1_verse1_melody[] =
  58. { rest, c4s, c4s, c4s, c4s, e4f, rest, c4, b3f, a3f,
  59. rest, b3f, b3f, c4, c4s, a3f, a4f, a4f, e4f,
  60. rest, b3f, b3f, c4, c4s, b3f, c4s, e4f, rest, c4, b3f, b3f, a3f,
  61. rest, b3f, b3f, c4, c4s, a3f, a3f, e4f, e4f, e4f, f4, e4f,
  62. c4s, e4f, f4, c4s, e4f, e4f, e4f, f4, e4f, a3f,
  63. rest, b3f, c4, c4s, a3f, rest, e4f, f4, e4f
  64. };
  65.  
  66. int song1_verse1_rhythmn[] =
  67. { 2, 1, 1, 1, 1, 2, 1, 1, 1, 5,
  68. 1, 1, 1, 1, 3, 1, 2, 1, 5,
  69. 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3,
  70. 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 4,
  71. 5, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  72. 2, 1, 1, 1, 3, 1, 1, 1, 3
  73. };
  74.  
  75. char* lyrics_verse1[] =
  76. { "We're ", "no ", "strangers ", "", "to ", "love ", "", "\r\n",
  77. "You ", "know ", "the ", "rules ", "and ", "so ", "do ", "I\r\n",
  78. "A ", "full ", "commitment's ", "", "", "what ", "I'm ", "thinking ", "", "of", "\r\n",
  79. "You ", "wouldn't ", "", "get ", "this ", "from ", "any ", "", "other ", "", "guy\r\n",
  80. "I ", "just ", "wanna ", "", "tell ", "you ", "how ", "I'm ", "feeling", "\r\n",
  81. "Gotta ", "", "make ", "you ", "understand", "", "\r\n"
  82. };
  83.  
  84. // Parts 4 or 6 (Chorus)
  85.  
  86. int song1_chorus_melody[] =
  87. { b4f, b4f, a4f, a4f,
  88. f5, f5, e5f, b4f, b4f, a4f, a4f, e5f, e5f, c5s, c5, b4f,
  89. c5s, c5s, c5s, c5s,
  90. c5s, e5f, c5, b4f, a4f, a4f, a4f, e5f, c5s,
  91. b4f, b4f, a4f, a4f,
  92. f5, f5, e5f, b4f, b4f, a4f, a4f, a5f, c5, c5s, c5, b4f,
  93. c5s, c5s, c5s, c5s,
  94. c5s, e5f, c5, b4f, a4f, rest, a4f, e5f, c5s, rest
  95. };
  96.  
  97. int song1_chorus_rhythmn[] =
  98. { 1, 1, 1, 1,
  99. 3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2,
  100. 1, 1, 1, 1,
  101. 3, 3, 3, 1, 2, 2, 2, 4, 8,
  102. 1, 1, 1, 1,
  103. 3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2,
  104. 1, 1, 1, 1,
  105. 3, 3, 3, 1, 2, 2, 2, 4, 8, 4
  106. };
  107.  
  108. char* lyrics_chorus[] =
  109. { "Never ", "", "gonna ", "", "give ", "you ", "up\r\n",
  110. "Never ", "", "gonna ", "", "let ", "you ", "down", "", "\r\n",
  111. "Never ", "", "gonna ", "", "run ", "around ", "", "", "", "and ", "desert ", "", "you\r\n",
  112. "Never ", "", "gonna ", "", "make ", "you ", "cry\r\n",
  113. "Never ", "", "gonna ", "", "say ", "goodbye ", "", "", "\r\n",
  114. "Never ", "", "gonna ", "", "tell ", "a ", "lie ", "", "", "and ", "hurt ", "you\r\n"
  115. };
  116.  
  117. void setup()
  118. {
  119. pinMode(piezo, OUTPUT);
  120. pinMode(led, OUTPUT);
  121. digitalWrite(led, LOW);
  122. Serial.begin(9600);
  123. flag = true;
  124. a = 4;
  125. b = 0;
  126. c = 0;
  127. }
  128.  
  129. void loop()
  130. {
  131. // edit code here to define play conditions
  132. /*
  133. if (CONDITION 1) { // play
  134. flag = true;
  135. }
  136. else if (CONDITION2) { // pause
  137. flag = false;
  138. }
  139. */
  140.  
  141. // play next step in song
  142. if (flag == true) {
  143. play();
  144. }
  145. }
  146.  
  147. void play() {
  148. int notelength;
  149. if (a == 1 || a == 2) { // Intro
  150. // intro
  151. notelength = beatlength * song1_intro_rhythmn[b];
  152. if (song1_intro_melody[b] > 0) { // if not a rest, play note
  153. digitalWrite(led, HIGH);
  154. tone(piezo, song1_intro_melody[b], notelength);
  155. }
  156. b++;
  157. if (b >= sizeof(song1_intro_melody) / sizeof(int)) {
  158. a++;
  159. b = 0;
  160. c = 0;
  161. }
  162. }
  163. else if (a == 3 || a == 5) { // Verse 1
  164. // verse
  165. notelength = beatlength * 2 * song1_verse1_rhythmn[b];
  166. if (song1_verse1_melody[b] > 0) {
  167. digitalWrite(led, HIGH);
  168. Serial.print(lyrics_verse1[c]);
  169. tone(piezo, song1_verse1_melody[b], notelength);
  170. c++;
  171. }
  172. b++;
  173. if (b >= sizeof(song1_verse1_melody) / sizeof(int)) {
  174. a++;
  175. b = 0;
  176. c = 0;
  177. }
  178. }
  179. else if (a == 4 || a == 6) { //chorus
  180. // chorus
  181. notelength = beatlength * song1_chorus_rhythmn[b];
  182. if (song1_chorus_melody[b] > 0) {
  183. digitalWrite(led, HIGH);
  184. Serial.print(lyrics_chorus[c]);
  185. tone(piezo, song1_chorus_melody[b], notelength);
  186. c++;
  187. }
  188. b++;
  189. if (b >= sizeof(song1_chorus_melody) / sizeof(int)) {
  190. Serial.println("");
  191. a++;
  192. b = 0;
  193. c = 0;
  194. }
  195. }
  196. delay(notelength); // necessary because piezo is on independent timer
  197. noTone(piezo);
  198. digitalWrite(led, LOW);
  199. delay(notelength * beatseparationconstant); // create separation between notes
  200. if (a == 7) { // loop back around to beginning of song
  201. a = 1;
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement