Advertisement
asteroidsteam

Untitled

Aug 15th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 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 = 3;
  36. int led = 13;
  37. int trigPin = 4; //Trig - green Jumper
  38. int echoPin = 2; //Echo - yellow Jumper
  39. long duration, cm, inches;
  40.  
  41. volatile int beatlength = 100; // determines tempo
  42. float beatseparationconstant = 0.3;
  43.  
  44. int a; // part index
  45. int b; // song index
  46. int c; // lyric index
  47.  
  48. boolean flag; // play/pause
  49.  
  50. // Parts 1 and 2 (Intro)
  51.  
  52. int song1_intro_melody[] =
  53. {c5s, e5f, e5f, f5, a5f, f5s, f5, e5f, c5s, e5f, rest, a4f, a4f};
  54.  
  55. int song1_intro_rhythmn[] =
  56. {6, 10, 6, 6, 1, 1, 1, 1, 6, 10, 4, 2, 10};
  57.  
  58. // Parts 3 or 5 (Verse 1)
  59.  
  60. int song1_verse1_melody[] =
  61. { rest, c4s, c4s, c4s, c4s, e4f, rest, c4, b3f, a3f,
  62. rest, b3f, b3f, c4, c4s, a3f, a4f, a4f, e4f,
  63. rest, b3f, b3f, c4, c4s, b3f, c4s, e4f, rest, c4, b3f, b3f, a3f,
  64. rest, b3f, b3f, c4, c4s, a3f, a3f, e4f, e4f, e4f, f4, e4f,
  65. c4s, e4f, f4, c4s, e4f, e4f, e4f, f4, e4f, a3f,
  66. rest, b3f, c4, c4s, a3f, rest, e4f, f4, e4f
  67. };
  68.  
  69. int song1_verse1_rhythmn[] =
  70. { 2, 1, 1, 1, 1, 2, 1, 1, 1, 5,
  71. 1, 1, 1, 1, 3, 1, 2, 1, 5,
  72. 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3,
  73. 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 4,
  74. 5, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  75. 2, 1, 1, 1, 3, 1, 1, 1, 3
  76. };
  77.  
  78. char* lyrics_verse1[] =
  79. { "We're ", "no ", "strangers ", "", "to ", "love ", "", "\r\n",
  80. "You ", "know ", "the ", "rules ", "and ", "so ", "do ", "I\r\n",
  81. "A ", "full ", "commitment's ", "", "", "what ", "I'm ", "thinking ", "", "of", "\r\n",
  82. "You ", "wouldn't ", "", "get ", "this ", "from ", "any ", "", "other ", "", "guy\r\n",
  83. "I ", "just ", "wanna ", "", "tell ", "you ", "how ", "I'm ", "feeling", "\r\n",
  84. "Gotta ", "", "make ", "you ", "understand", "", "\r\n"
  85. };
  86.  
  87. // Parts 4 or 6 (Chorus)
  88.  
  89. int song1_chorus_melody[] =
  90. { b4f, b4f, a4f, a4f,
  91. f5, f5, e5f, b4f, b4f, a4f, a4f, e5f, e5f, c5s, c5, b4f,
  92. c5s, c5s, c5s, c5s,
  93. c5s, e5f, c5, b4f, a4f, a4f, a4f, e5f, c5s,
  94. b4f, b4f, a4f, a4f,
  95. f5, f5, e5f, b4f, b4f, a4f, a4f, a5f, c5, c5s, c5, b4f,
  96. c5s, c5s, c5s, c5s,
  97. c5s, e5f, c5, b4f, a4f, rest, a4f, e5f, c5s, rest
  98. };
  99.  
  100. int song1_chorus_rhythmn[] =
  101. { 1, 1, 1, 1,
  102. 3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2,
  103. 1, 1, 1, 1,
  104. 3, 3, 3, 1, 2, 2, 2, 4, 8,
  105. 1, 1, 1, 1,
  106. 3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2,
  107. 1, 1, 1, 1,
  108. 3, 3, 3, 1, 2, 2, 2, 4, 8, 4
  109. };
  110.  
  111. char* lyrics_chorus[] =
  112. { "Never ", "", "gonna ", "", "give ", "you ", "up\r\n",
  113. "Never ", "", "gonna ", "", "let ", "you ", "down", "", "\r\n",
  114. "Never ", "", "gonna ", "", "run ", "around ", "", "", "", "and ", "desert ", "", "you\r\n",
  115. "Never ", "", "gonna ", "", "make ", "you ", "cry\r\n",
  116. "Never ", "", "gonna ", "", "say ", "goodbye ", "", "", "\r\n",
  117. "Never ", "", "gonna ", "", "tell ", "a ", "lie ", "", "", "and ", "hurt ", "you\r\n"
  118. };
  119.  
  120. void setup()
  121. {
  122. pinMode(piezo, OUTPUT);
  123. pinMode(led, OUTPUT);
  124. digitalWrite(led, LOW);
  125. flag = true;
  126. a = 4;
  127. b = 0;
  128. c = 0;
  129. //Serial Port begin
  130. Serial.begin (9600);
  131. //Define inputs and outputs
  132. pinMode(trigPin, OUTPUT);
  133. pinMode(echoPin, INPUT);
  134. }
  135.  
  136. void loop()
  137. {
  138. // edit code here to define play conditions
  139. /*
  140. if (CONDITION 1) { // play
  141. flag = true;
  142. }
  143. else if (CONDITION2) { // pause
  144. flag = false;
  145. }
  146. */
  147. digitalWrite(trigPin, LOW);
  148. delayMicroseconds(5);
  149. digitalWrite(trigPin, HIGH);
  150. delayMicroseconds(10);
  151. digitalWrite(trigPin, LOW);
  152.  
  153. // Read the signal from the sensor: a HIGH pulse whose
  154. // duration is the time (in microseconds) from the sending
  155. // of the ping to the reception of its echo off of an object.
  156. pinMode(echoPin, INPUT);
  157. duration = pulseIn(echoPin, HIGH);
  158.  
  159. // convert the time into a distance
  160. cm = (duration/2) / 29.1;
  161. inches = (duration/2) / 74;
  162.  
  163. Serial.print(inches);
  164. Serial.print("in, ");
  165. Serial.print(cm);
  166. Serial.print("cm");
  167. Serial.println();
  168. // play next step in song
  169. if (inches == 95 || inches == 94 || inches == 93) {
  170. flag = false;
  171. } else {
  172. flag = true;
  173. }
  174. if (flag == true) {
  175. play();
  176. }
  177. }
  178.  
  179. void play() {
  180. int notelength;
  181. if (a == 1 || a == 2) { // Intro
  182. // intro
  183. notelength = beatlength * song1_intro_rhythmn[b];
  184. if (song1_intro_melody[b] > 0) { // if not a rest, play note
  185. digitalWrite(led, HIGH);
  186. tone(piezo, song1_intro_melody[b], notelength);
  187. }
  188. b++;
  189. if (b >= sizeof(song1_intro_melody) / sizeof(int)) {
  190. a++;
  191. b = 0;
  192. c = 0;
  193. }
  194. }
  195. else if (a == 3 || a == 5) { // Verse 1
  196. // verse
  197. notelength = beatlength * 2 * song1_verse1_rhythmn[b];
  198. if (song1_verse1_melody[b] > 0) {
  199. digitalWrite(led, HIGH);
  200. tone(piezo, song1_verse1_melody[b], notelength);
  201. c++;
  202. }
  203. b++;
  204. if (b >= sizeof(song1_verse1_melody) / sizeof(int)) {
  205. a++;
  206. b = 0;
  207. c = 0;
  208. }
  209. }
  210. else if (a == 4 || a == 6) { //chorus
  211. // chorus
  212. notelength = beatlength * song1_chorus_rhythmn[b];
  213. if (song1_chorus_melody[b] > 0) {
  214. digitalWrite(led, HIGH);
  215. tone(piezo, song1_chorus_melody[b], notelength);
  216. c++;
  217. }
  218. b++;
  219. if (b >= sizeof(song1_chorus_melody) / sizeof(int)) {
  220. a++;
  221. b = 0;
  222. c = 0;
  223. }
  224. }
  225. delay(notelength); // necessary because piezo is on independent timer
  226. noTone(piezo);
  227. digitalWrite(led, LOW);
  228. delay(notelength * beatseparationconstant); // create separation between notes
  229. if (a == 7) { // loop back around to beginning of song
  230. a = 1;
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement