Advertisement
Guest User

mals

a guest
Oct 25th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. const int buzzerPin = 9;
  2.  
  3. // We'll set up an array with the notes we want to play
  4. // change these values to make different songs!
  5.  
  6. // Length must equal the total number of notes and spaces
  7.  
  8. const int songLength = 18;
  9.  
  10. // Notes is an array of text characters corresponding to the notes
  11. // in your song. A space represents a rest (no tone)
  12.  
  13. char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest
  14.  
  15. // Beats is an array of values for each note and rest.
  16. // A "1" represents a quarter-note, 2 a half-note, etc.
  17. // Don't forget that the rests (spaces) need a length as well.
  18.  
  19. int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
  20.  
  21. // The tempo is how fast to play the song.
  22. // To make the song play faster, decrease this value.
  23.  
  24. int tempo = 150;
  25.  
  26.  
  27. void setup()
  28. {
  29. pinMode(buzzerPin, OUTPUT);
  30. }
  31.  
  32.  
  33. void loop()
  34. {
  35. int i, duration;
  36.  
  37. for (i = 0; i < songLength; i++) // step through the song arrays
  38. {
  39. duration = beats[i] * tempo; // length of note/rest in ms
  40.  
  41. if (notes[i] == ' ') // is this a rest?
  42. {
  43. delay(duration); // then pause for a moment
  44. }
  45. else // otherwise, play the note
  46. {
  47. tone(buzzerPin, frequency(notes[i]), duration);
  48. delay(duration); // wait for tone to finish
  49. }
  50. delay(tempo/10); // brief pause between notes
  51. }
  52.  
  53. }
  54.  
  55.  
  56. int frequency(char note)
  57. {
  58. // This function takes a note character (a-g), and returns the
  59. // corresponding frequency in Hz for the tone() function.
  60.  
  61. int i;
  62. const int numNotes = 8; // number of notes we're storing
  63.  
  64. // The following arrays hold the note characters and their
  65. // corresponding frequencies. The last "C" note is uppercase
  66. // to separate it from the first lowercase "c". If you want to
  67. // add more notes, you'll need to use unique characters.
  68.  
  69. // For the "char" (character) type, we put single characters
  70. // in single quotes.
  71.  
  72. char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  73. int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  74.  
  75. // Now we'll search through the letters in the array, and if
  76. // we find it, we'll return the frequency for that note.
  77.  
  78. for (i = 0; i < numNotes; i++) // Step through the notes
  79. {
  80. if (names[i] == note) // Is this the one?
  81. {
  82. return(frequencies[i]); // Yes! Return the frequency
  83. }
  84. }
  85. return(0); // We looked through everything and didn't find it,
  86. // but we still need to return a value, so return 0.
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement