Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #include <Servo.h>
  2. int speakerPin = 9;
  3. int servoPin = 3;
  4. Servo servo1;
  5. //sangen tatt fra https://gist.github.com/ianklatzco/9127560
  6.  
  7. const int button1 = 6; //knapp 1
  8. const int button2 = 5; // knapp 2
  9.  
  10. char notes[] = "gabygabyxzCDxzCDabywabywzCDEzCDEbywFCDEqywFGDEqi azbC"; // a space represents a rest
  11. int length = sizeof(notes); // the number of notes
  12. int beats[] = { 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 2,3,3,16,};
  13. int tempo = 75;
  14.  
  15. void setup() {
  16.  
  17. pinMode(button1, INPUT);
  18. pinMode(button2, INPUT);
  19. pinMode(12,OUTPUT);
  20. pinMode(13,OUTPUT);
  21. pinMode(8,OUTPUT);
  22. servo1.attach(servoPin);
  23. servo1.write(0);
  24. }
  25.  
  26. void loop() {
  27.  
  28. buttons();
  29. }
  30.  
  31. void buttons()
  32. {
  33. if(digitalRead(button1) == LOW && digitalRead(button2) == HIGH)
  34. {
  35.  
  36. playZelda();
  37. digitalWrite(12,HIGH);
  38. delay(200);
  39. digitalWrite(13,HIGH);
  40. delay(200);
  41. digitalWrite(8,HIGH);
  42. servo1.write(180);
  43. delay(4000);
  44. servo1.write(0);
  45. digitalWrite(8,LOW);
  46. delay(200);
  47. digitalWrite(12,LOW);
  48. delay(200);
  49. digitalWrite(13,LOW);
  50. }
  51. if(digitalRead(button2) == LOW && digitalRead(button1) == LOW)
  52. {
  53. servo1.write(189);
  54. delay(200);
  55. servo1.write(100);
  56. delay(200);
  57. servo1.write(0);
  58. delay(200);
  59. servo1.write(200);
  60. delay(200);
  61. servo1.write(69);
  62. delay(200);
  63. servo1.write(23);
  64. delay(200);
  65. servo1.write(0);
  66. delay(200);
  67.  
  68. }
  69. }
  70.  
  71. void playZelda()
  72. {
  73. pinMode(speakerPin, OUTPUT);
  74. for (int i = 0; i < length; i++) {
  75. if (notes[i] == ' ') {
  76. delay(beats[i] * tempo); // rest
  77. } else {
  78. playNote(notes[i], beats[i] * tempo);
  79. }
  80.  
  81. // pause between notes
  82. delay(tempo / 2);
  83. }
  84.  
  85. delay(100);
  86. }
  87. void playTone(int tone, int duration) {
  88. for (long i = 0; i < duration * 1000L; i += tone * 2) {
  89. digitalWrite(speakerPin, HIGH);
  90. delayMicroseconds(tone);
  91. digitalWrite(speakerPin, LOW);
  92. delayMicroseconds(tone);
  93. }
  94. }
  95.  
  96. void playNote(char note, int duration) {
  97. char names[] = { 'c', 'd', 'e', 'f', 'g', 'x', 'a', 'z', 'b', 'C', 'y', 'D', 'w', 'E', 'F', 'q', 'G', 'i' };
  98. // c=C4, C = C5. These values have been tuned.
  99. int tones[] = { 1898, 1690, 1500, 1420, 1265, 1194, 1126, 1063, 1001, 947, 893, 843, 795, 749, 710, 668, 630, 594 };
  100.  
  101. // play the tone corresponding to the note name
  102. for (int i = 0; i < 18; i++) {
  103. if (names[i] == note) {
  104. playTone(tones[i], duration);
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement