Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. int speakerPin = 9;
  2.  
  3. int length = 28;
  4.  
  5. char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";
  6.  
  7. int beats[] = { 2, 2, 8, 8, 8, 16, 1, 2, 2, 8, 8,8, 16, 1, 2,2,8,8,8,8,16, 1,2,2,8,8,8,16 };
  8.  
  9. int tempo = 150;
  10.  
  11. void playTone(int tone, int duration) {
  12.  
  13. for (long i = 0; i < duration * 1000L; i += tone * 2) {
  14.  
  15. digitalWrite(speakerPin, HIGH);
  16.  
  17. delayMicroseconds(tone);
  18.  
  19. digitalWrite(speakerPin, LOW);
  20.  
  21. delayMicroseconds(tone);
  22.  
  23. }
  24.  
  25. }
  26.  
  27. void playNote(char note, int duration) {
  28.  
  29. char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B',
  30.  
  31. 'c', 'd', 'e', 'f', 'g', 'a', 'b',
  32.  
  33. 'x', 'y' };
  34.  
  35. int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014,
  36.  
  37. 956, 834, 765, 593, 468, 346, 224,
  38.  
  39. 655 , 715 };
  40.  
  41. int SPEE = 5;
  42.  
  43. for (int i = 0; i < 17; i++) {
  44.  
  45. if (names[i] == note) {
  46. int newduration = duration/SPEE;
  47. playTone(tones[i], newduration);
  48.  
  49. }
  50.  
  51. }
  52.  
  53. }
  54.  
  55. void setup() {
  56.  
  57. pinMode(speakerPin, OUTPUT);
  58.  
  59. }
  60.  
  61. void loop() {
  62.  
  63. for (int i = 0; i < length; i++) {
  64.  
  65. if (notes[i] == ' ') {
  66.  
  67. delay(beats[i] * tempo);
  68.  
  69. } else {
  70.  
  71. playNote(notes[i], beats[i] * tempo);
  72.  
  73. }
  74.  
  75.  
  76.  
  77. delay(tempo);
  78.  
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement