Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Dec 26th, 2012  |  syntax: None  |  size: 7.00 KB  |  hits: 53  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #define NOTE_B0  31
  2. #define NOTE_C1  33
  3. #define NOTE_CS1 35
  4. #define NOTE_D1  37
  5. #define NOTE_DS1 39
  6. #define NOTE_E1  41
  7. #define NOTE_F1  44
  8. #define NOTE_FS1 46
  9. #define NOTE_G1  49
  10. #define NOTE_GS1 52
  11. #define NOTE_A1  55
  12. #define NOTE_AS1 58
  13. #define NOTE_B1  62
  14. #define NOTE_C2  65
  15. #define NOTE_CS2 69
  16. #define NOTE_D2  73
  17. #define NOTE_DS2 78
  18. #define NOTE_E2  82
  19. #define NOTE_F2  87
  20. #define NOTE_FS2 93
  21. #define NOTE_G2  98
  22. #define NOTE_GS2 104
  23. #define NOTE_A2  110
  24. #define NOTE_AS2 117
  25. #define NOTE_B2  123
  26. #define NOTE_C3  131
  27. #define NOTE_CS3 139
  28. #define NOTE_D3  147
  29. #define NOTE_DS3 156
  30. #define NOTE_E3  165
  31. #define NOTE_F3  175
  32. #define NOTE_FS3 185
  33. #define NOTE_G3  196
  34. #define NOTE_GS3 208
  35. #define NOTE_A3  220
  36. #define NOTE_AS3 233
  37. #define NOTE_B3  247
  38. #define NOTE_C4  262
  39. #define NOTE_CS4 277
  40. #define NOTE_D4  294
  41. #define NOTE_DS4 311
  42. #define NOTE_E4  330
  43. #define NOTE_F4  349
  44. #define NOTE_FS4 370
  45. #define NOTE_G4  392
  46. #define NOTE_GS4 415
  47. #define NOTE_A4  440
  48. #define NOTE_AS4 466
  49. #define NOTE_B4  494
  50. #define NOTE_C5  523
  51. #define NOTE_CS5 554
  52. #define NOTE_D5  587
  53. #define NOTE_DS5 622
  54. #define NOTE_E5  659
  55. #define NOTE_F5  698
  56. #define NOTE_FS5 740
  57. #define NOTE_G5  784
  58. #define NOTE_GS5 831
  59. #define NOTE_A5  880
  60. #define NOTE_AS5 932
  61. #define NOTE_B5  988
  62. #define NOTE_C6  1047
  63. #define NOTE_CS6 1109
  64. #define NOTE_D6  1175
  65. #define NOTE_DS6 1245
  66. #define NOTE_E6  1319
  67. #define NOTE_F6  1397
  68. #define NOTE_FS6 1480
  69. #define NOTE_G6  1568
  70. #define NOTE_GS6 1661
  71. #define NOTE_A6  1760
  72. #define NOTE_AS6 1865
  73. #define NOTE_B6  1976
  74. #define NOTE_C7  2093
  75. #define NOTE_CS7 2217
  76. #define NOTE_D7  2349
  77. #define NOTE_DS7 2489
  78. #define NOTE_E7  2637
  79. #define NOTE_F7  2794
  80. #define NOTE_FS7 2960
  81. #define NOTE_G7  3136
  82. #define NOTE_GS7 3322
  83. #define NOTE_A7  3520
  84. #define NOTE_AS7 3729
  85. #define NOTE_B7  3951
  86. #define NOTE_C8  4186
  87. #define NOTE_CS8 4435
  88. #define NOTE_D8  4699
  89. #define NOTE_DS8 4978
  90.  
  91. int sensorPin = 0;
  92. int speakerPin = 9;
  93. int ledPin = 13;
  94. int rPin = 6;
  95. int gPin = 5;
  96. int bPin = 3;
  97.  
  98.  
  99. void setup()
  100. {
  101.   pinMode(rPin, OUTPUT);
  102.   pinMode(gPin, OUTPUT);
  103.   pinMode(bPin, OUTPUT);
  104.   pinMode(speakerPin, OUTPUT);
  105.   pinMode(ledPin, OUTPUT);  
  106.  
  107. }
  108.  
  109. void loop()
  110. {
  111.   showSpectrum();
  112.  
  113.   song();
  114.  
  115. }
  116.  
  117. void song()  //here is where all the notes for the song are played.
  118. {        
  119.   beep(speakerPin, NOTE_D5, 100);  //   beep( -PIN OF SPEAKER-, -THE NOTE WANTING TO BE PLAYED-, -DURATION OF THE NOTE IN MILISECONDS- )
  120.   delay(80);
  121.   beep(speakerPin, NOTE_F5, 100);
  122.   delay(80);
  123.   beep(speakerPin, NOTE_D6, 500);
  124.   delay(250);
  125.  
  126.   beep(speakerPin, NOTE_D5, 100);
  127.   delay(80);
  128.   beep(speakerPin, NOTE_F5, 100);
  129.   delay(80);
  130.   beep(speakerPin, NOTE_D6, 500);
  131.   delay(250);
  132.  
  133.   beep(speakerPin, NOTE_E6, 500);
  134.   delay(100);
  135.   beep(speakerPin, NOTE_F6, 100);
  136.   delay(100);
  137.   beep(speakerPin, NOTE_E6, 100);
  138.   delay(80);
  139.   beep(speakerPin, NOTE_F6, 100);
  140.   delay(80);
  141.   beep(speakerPin, NOTE_E6, 100);
  142.   delay(80);
  143.   beep(speakerPin, NOTE_C6, 100);
  144.   delay(80);
  145.   beep(speakerPin, NOTE_A5, 300);
  146.   delay(300);
  147.  
  148.   beep(speakerPin, NOTE_A5, 200);
  149.   delay(100);
  150.   beep(speakerPin, NOTE_D5, 200);
  151.   delay(100);
  152.   beep(speakerPin, NOTE_F5, 100);
  153.   delay(100);
  154.   beep(speakerPin, NOTE_G5, 100);
  155.   delay(100);
  156.   beep(speakerPin, NOTE_A5, 300);
  157.   delay(500);
  158.  
  159.   beep(speakerPin, NOTE_A5, 200);
  160.   delay(100);
  161.   beep(speakerPin, NOTE_D5, 200);
  162.   delay(100);
  163.   beep(speakerPin, NOTE_F5, 100);
  164.   delay(100);
  165.   beep(speakerPin, NOTE_G5, 100);
  166.   delay(100);
  167.   beep(speakerPin, NOTE_E5, 300);
  168.   delay(500);
  169.  
  170.  
  171.  
  172.   beep(speakerPin, NOTE_D5, 100);
  173.   delay(80);
  174.   beep(speakerPin, NOTE_F5, 100);
  175.   delay(80);
  176.   beep(speakerPin, NOTE_D6, 400);
  177.   delay(250);
  178.  
  179.   beep(speakerPin, NOTE_D5, 100);
  180.   delay(80);
  181.   beep(speakerPin, NOTE_F5, 100);
  182.   delay(80);
  183.   beep(speakerPin, NOTE_D6, 400);
  184.   delay(250);
  185.  
  186.   beep(speakerPin, NOTE_E6, 200);
  187.   delay(200);
  188.   beep(speakerPin, NOTE_F6, 100);
  189.   delay(100);
  190.   beep(speakerPin, NOTE_E6, 100);
  191.   delay(80);
  192.   beep(speakerPin, NOTE_F6, 100);
  193.   delay(80);
  194.   beep(speakerPin, NOTE_E6, 100);
  195.   delay(80);
  196.   beep(speakerPin, NOTE_C6, 100);
  197.   delay(80);
  198.   beep(speakerPin, NOTE_A5, 300);
  199.   delay(300);
  200.  
  201.   beep(speakerPin, NOTE_A5, 200);
  202.   delay(100);
  203.   beep(speakerPin, NOTE_D5, 200);
  204.   delay(100);
  205.   beep(speakerPin, NOTE_F5, 100);
  206.   delay(100);
  207.   beep(speakerPin, NOTE_G5, 100);
  208.   delay(100);
  209.   beep(speakerPin, NOTE_A5, 500);
  210.   delay(100);
  211.   beep(speakerPin, NOTE_A5, 200);
  212.   delay(100);
  213.   beep(speakerPin, NOTE_D5, 500);
  214.   delay(2000);
  215.  
  216. }
  217.  
  218. void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)  //code for working out the rate at which each note plays and the frequency.
  219. {
  220.   digitalWrite(ledPin, HIGH);
  221.   int x;      
  222.   long delayAmount = (long)(1000000/frequencyInHertz);
  223.   long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
  224.   for (x=0;x<loopTime;x++)    
  225.   {    
  226.     digitalWrite(speakerPin,HIGH);
  227.     delayMicroseconds(delayAmount);
  228.     digitalWrite(speakerPin,LOW);
  229.     delayMicroseconds(delayAmount);
  230.   }    
  231.   digitalWrite(ledPin, LOW);
  232.   delay(20);
  233. }
  234.  
  235. void showSpectrum()
  236. {
  237.   int x;  
  238.  
  239.   // Now we'll use a for() loop to make x count from 0 to 767
  240.   // (Note that there's no semicolon after this line!
  241.   // That's because the for() loop will repeat the next
  242.   // "statement", which in this case is everything within
  243.   // the following brackets {} )
  244.  
  245.   for (x = 0; x < 768; x++)
  246.  
  247.   // Each time we loop (with a new value of x), do the following:
  248.  
  249.   {
  250.     showRGB(x);  // Call RGBspectrum() with our new x
  251.     delay(10);   // Delay for 10 ms (1/100th of a second)
  252.   }
  253. }
  254.  
  255. void showRGB(int color)
  256. {
  257.   int redIntensity;
  258.   int greenIntensity;
  259.   int blueIntensity;
  260.  
  261.   // Here we'll use an "if / else" statement to determine which
  262.   // of the three (R,G,B) zones x falls into. Each of these zones
  263.   // spans 255 because analogWrite() wants a number from 0 to 255.
  264.  
  265.   // In each of these zones, we'll calculate the brightness
  266.   // for each of the red, green, and blue LEDs within the RGB LED.
  267.  
  268.   if (color <= 255)          // zone 1
  269.   {
  270.     redIntensity = 255 - color;    // red goes from on to off
  271.     greenIntensity = color;        // green goes from off to on
  272.     blueIntensity = 0;             // blue is always off
  273.   }
  274.   else if (color <= 511)     // zone 2
  275.   {
  276.     redIntensity = 0;                     // red is always off
  277.     greenIntensity = 255 - (color - 256); // green on to off
  278.     blueIntensity = (color - 256);        // blue off to on
  279.   }
  280.   else // color >= 512       // zone 3
  281.   {
  282.     redIntensity = (color - 512);         // red off to on
  283.     greenIntensity = 0;                   // green is always off
  284.     blueIntensity = 255 - (color - 512);  // blue on to off
  285.   }
  286.  
  287.   // Now that the brightness values have been set, command the LED
  288.   // to those values
  289.  
  290.   analogWrite(rPin, redIntensity);
  291.   analogWrite(bPin, blueIntensity);
  292.   analogWrite(gPin, greenIntensity);
  293. }