Advertisement
esha492

WE WISH YOU A MERRY CHRISTMAS - FINAL

Nov 7th, 2017
1,330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1.  
  2.  
  3. int Piezo = 5;
  4. int length = 31;
  5. char notes[] = "dggagfeeeaabagfddbbCbageddeafg";
  6. int beats[] = {2,2,1,1,1,1,2,2,2,2,1,1,1,1,2,2,2,2,1,1,1,1,2,2,1,1,2,2,2,4};
  7.  
  8. int tempo = 190;
  9. void playTone(int tone, int duration) {
  10. for (long i = 0; i < duration * 1000L; i += tone * 2) {
  11. digitalWrite(Piezo, HIGH);
  12. delayMicroseconds(tone);
  13. digitalWrite(Piezo, LOW);
  14. delayMicroseconds(tone);
  15. }
  16. }
  17. void playNote(char note, int duration) {
  18. char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B' };
  19. int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 851, 758, 716, 636, 568, 506 };
  20.  
  21. // these are the "sharp" versions of each note e.g. the first value is for "c#"
  22. char names_sharp[] = { 'c', 'd', 'f', 'g', 'a', 'C', 'D', 'F', 'G', 'A' };
  23. int tones_sharp[] = { 1804, 1607, 1351, 1204, 1073, 902, 804, 676, 602, 536 };
  24.  
  25. // play the tone corresponding to the note name
  26. for (int i = 0; i < 8; i++) {
  27. if (names[i] == note) {
  28. playTone(tones[i], duration);
  29. }
  30. }
  31. }
  32. void setup() {
  33. pinMode(Piezo, OUTPUT);
  34. }
  35. void loop() {
  36. for (int i = 0; i < length; i++) {
  37. if (notes[i] == ' ') {
  38. delay(beats[i] * tempo); // rest
  39. } else {
  40. playNote(notes[i], beats[i] * tempo);
  41. }
  42.  
  43. // pause between notes
  44. delay(tempo / 15);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement