esha492

deck the halls - final

Nov 7th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1.  
  2.  
  3. int Piezo = 5;
  4. int length = 38;
  5. char notes[] = "gfedcdecdefdedcBcc gfedcdecdefdedcBcc";
  6. int beats[] = {3,1,2,2,2,2,2,2,1,1,1,1,3,1,2,2,2,2,4,3,1,2,2,2,2,2,2,1,1,1,1,3,1,2,2,2,2,4};
  7.  
  8. int tempo = 200;
  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','B'};
  19. int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 27.5};
  20.  
  21. char names_sharp[] = { 'c', 'd', 'f', 'g', 'a', 'C', 'D', 'F', 'G', 'A' };
  22. int tones_sharp[] = { 1804, 1607, 1351, 1204, 1073, 902, 804, 676, 602, 536 };
  23.  
  24. // play the tone corresponding to the note name
  25. for (int i = 0; i < 8; i++) {
  26. if (names[i] == note) {
  27. playTone(tones[i], duration);
  28. }
  29. }
  30. }
  31. void setup() {
  32. pinMode(Piezo, OUTPUT);
  33. }
  34. void loop() {
  35. for (int i = 0; i < length; i++) {
  36. if (notes[i] == ' ') {
  37. delay(beats[i] * tempo); // rest
  38. } else {
  39. playNote(notes[i], beats[i] * tempo);
  40. }
  41.  
  42. // pause between notes
  43. delay(tempo / 310);
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment