Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. /*
  2. LED Alarm Box
  3. Turns on LEDs in a pattern, then turns on an audible alarm.
  4.  
  5.  
  6. This example code is in the public domain.
  7.  
  8. modified 5 Feb 2016
  9. by Dylan Boyd
  10. */
  11. // TONES ==========================================
  12. // Defining the tones used for the alarm
  13. #define C 1911
  14. #define C1 1804
  15. #define D 1703
  16. #define Eb 1607
  17. #define E 1517
  18. #define F 1432
  19. #define F1 1352
  20. #define G 1276
  21. #define Ab 1204
  22. #define A 1136
  23. #define Bb 1073
  24. #define B 1012
  25. #define c 955
  26. #define c1 902
  27. #define d 851
  28. #define eb 803
  29. #define e 758
  30. #define f 716
  31. #define f1 676
  32. #define g 638
  33. #define ab 602
  34. #define a 568
  35. #define bb 536
  36. #define b 506
  37. #define p 0
  38. #define c 3830 // 261 Hz
  39. #define d 3400 // 294 Hz
  40. #define e 3038 // 329 Hz
  41. #define f 2864 // 349 Hz
  42. #define g 2550 // 392 Hz
  43. #define a 2272 // 440 Hz
  44. #define b 2028 // 493 Hz
  45. #define C 1912 // 523 Hz
  46. // Define a special note, 'R', to represent a rest
  47. #define R 0
  48.  
  49. // SETUP ============================================
  50. // Setting up the buzzer and photocell
  51. int speakerOut = 8;
  52. int photocellPin = 0; // the cell and 10K pulldown are connected to a0
  53. int photocellReading; // the analog reading from the analog resistor divider
  54.  
  55. // the setup function runs once when you press reset or power the board
  56. void setup() {
  57. // initialize LED Pins, Speaker Pin, and starting the Serial.
  58. Serial.begin(9600);
  59. pinMode(5, OUTPUT);
  60. pinMode(6, OUTPUT);
  61. pinMode(7, OUTPUT);
  62. pinMode(speakerOut, OUTPUT);
  63. }
  64. // initalizing the beats of the song and the melody
  65. int melody[] = {e, e, e, c, e, g, G, c, G, E, A, B, Bb, A, G, e, g, a, f, g, e, c, d, B, c};
  66. int beats[] = {6, 12, 12, 6, 12, 24, 24, 18, 18, 18, 12, 12, 6, 12, 8, 8, 8, 12, 6, 12, 12, 6, 6, 6, 12};
  67. int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.
  68. // Set overall tempo
  69. long tempo = 10000;
  70. // Set length of pause between notes
  71. int pause = 1000;
  72. // Loop variable to increase Rest length
  73. int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES
  74.  
  75. // Initialize core variables for song
  76. int tone_ = 0;
  77. int beat = 0;
  78. long duration = 0;
  79.  
  80. // PLAY TONE ==============================================
  81. // Pulse the speaker to play a tone for a particular duration
  82. void playTone() {
  83. long elapsed_time = 0;
  84. if (tone_ > 0) { // if this isn't a Rest beat, while the tone has
  85. // played less long than 'duration', pulse speaker HIGH and LOW
  86. while (elapsed_time < duration) {
  87.  
  88. digitalWrite(speakerOut,HIGH);
  89. delayMicroseconds(tone_ / 2);
  90.  
  91. // DOWN
  92. digitalWrite(speakerOut, LOW);
  93. delayMicroseconds(tone_ / 2);
  94.  
  95. // Keep track of how long we pulsed
  96. elapsed_time += (tone_);
  97. }
  98. }
  99. else { // Rest beat; loop times delay
  100. for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
  101. delayMicroseconds(duration);
  102. }
  103. }
  104. }
  105.  
  106. // the loop function runs over and over again forever
  107. void loop() {
  108. photocellReading = analogRead(photocellPin);
  109.  
  110. Serial.print("Analog reading = ");
  111. Serial.print(photocellReading); // the raw analog reading
  112.  
  113. // We'll have a few threshholds, qualitatively determined
  114. if (photocellReading < 10) {
  115. Serial.println(" - Dark");
  116. } else if (photocellReading < 200) {
  117. Serial.println(" - Dim");
  118. } else if (photocellReading < 500) {
  119. Serial.println(" - Light");
  120. } else if (photocellReading < 800) {
  121. Serial.println(" - Bright");
  122. } else {
  123. Serial.println(" - Very bright");
  124. }
  125. delay(1000);
  126.  
  127. if(photocellReading > 500){ //if room isn't dark enough, delay
  128. delay(10000);
  129. }
  130. if (photocellReading < 600) // if room is dark, light LEDs in pattern
  131. { //LED PATTERN:
  132. digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level)
  133. delay(200); // wait for a second
  134. digitalWrite(5, LOW); // turn the LED off by making the voltage LOW
  135. delay(200); // wait for a second
  136. digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)
  137. delay(200); // wait for a second
  138. digitalWrite(6, LOW); // turn the LED off by making the voltage LOW
  139. delay(200); // wait for a second
  140. digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
  141. delay(200); // wait for a second
  142. digitalWrite(7, LOW); // turn the LED off by making the voltage LOW
  143. delay(1000); // wait for a second
  144. digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level)
  145. delay(200); // wait for a second
  146. digitalWrite(5, LOW); // turn the LED off by making the voltage LOW
  147. delay(200); // wait for a second
  148. digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)
  149. delay(200); // wait for a second
  150. digitalWrite(6, LOW); // turn the LED off by making the voltage LOW
  151. delay(200); // wait for a second
  152. digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
  153. delay(200); // wait for a second
  154. digitalWrite(7, LOW); // turn the LED off by making the voltage LOW
  155. delay(1000); // wait for a second
  156. digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level)
  157. delay(200); // wait for a second
  158. digitalWrite(5, LOW); // turn the LED off by making the voltage LOW
  159. delay(200); // wait for a second
  160. digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)
  161. delay(200); // wait for a second
  162. digitalWrite(6, LOW); // turn the LED off by making the voltage LOW
  163. delay(200); // wait for a second
  164. digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
  165. delay(200); // wait for a second
  166. digitalWrite(7, LOW); // turn the LED off by making the voltage LOW
  167. delay(1000); // wait for a second
  168.  
  169. // Set overall tempo
  170. long tempo = 10000;
  171. // Set length of pause between notes
  172. int pause = 1000;
  173. // Loop variable to increase Rest length
  174. int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES
  175.  
  176. // Initialize core variables
  177. int tone_ = 0;
  178. int beat = 0;
  179. long duration = 0;
  180.  
  181. }
  182.  
  183.  
  184. // LET THE WILD RUMPUS BEGIN SUPER MARIO! =============================
  185. // Set up a counter to pull from melody[] and beats[]
  186. for (int i=0; i<MAX_COUNT; i++) {
  187. tone_ = melody[i];
  188. beat = beats[i];
  189.  
  190. duration = beat * tempo; // Set up timing
  191.  
  192. playTone();
  193. // A pause between notes...
  194. delayMicroseconds(pause);
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement