Advertisement
esha492

JINGLE BELLS ARDUINO

Oct 31st, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1.  
  2. int Piezo = 5;
  3. int length = 26;
  4. char notes[] = "eeeeeeegcde fffffeeeeddedg";
  5. int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};
  6.  
  7. int tempo = 300;
  8. void playTone(int tone, int duration) {
  9. for (long i = 0; i < duration * 1000L; i += tone * 2) {
  10. digitalWrite(Piezo, HIGH);
  11. delayMicroseconds(tone);
  12. digitalWrite(Piezo, LOW);
  13. delayMicroseconds(tone);
  14. }
  15. }
  16. void playNote(char note, int duration) {
  17. char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  18. int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  19.  
  20. // play the tone corresponding to the note name
  21. for (int i = 0; i < 8; i++) {
  22. if (names[i] == note) {
  23. playTone(tones[i], duration);
  24. }
  25. }
  26. }
  27. void setup() {
  28. pinMode(Piezo, OUTPUT);
  29. }
  30. void loop() {
  31. for (int i = 0; i < length; i++) {
  32. if (notes[i] == ' ') {
  33. delay(beats[i] * tempo); // rest
  34. } else {
  35. playNote(notes[i], beats[i] * tempo);
  36. }
  37.  
  38. // pause between notes
  39. delay(tempo / 2);
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement