Advertisement
esha492

SANTA CLAUS CHORUS

Oct 31st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1.  
  2.  
  3. int Piezo = 5;
  4. int length = 29;
  5. char notes[] = "gefgg gabCC efggagff egcedffce";
  6. int beats[] = {2,1,1,2,3,.5,1,1,1,2,3.5,1,1,1,2,2,1,1,1,4,2,2,2,2,1,2,2,2,2.5};
  7.  
  8. int tempo = 300;
  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. // play the tone corresponding to the note name
  22. for (int i = 0; i < 8; i++) {
  23. if (names[i] == note) {
  24. playTone(tones[i], duration);
  25. }
  26. }
  27. }
  28. void setup() {
  29. pinMode(Piezo, OUTPUT);
  30. }
  31. void loop() {
  32. for (int i = 0; i < length; i++) {
  33. if (notes[i] == ' ') {
  34. delay(beats[i] * tempo); // rest
  35. } else {
  36. playNote(notes[i], beats[i] * tempo);
  37. }
  38.  
  39. // pause between notes
  40. delay(tempo / 210);
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement