Guest User

Untitled

a guest
Aug 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. int speakerPin = 9;
  2. int played = 0;
  3.  
  4. int cicada_length = 331; // the number of notes
  5. char cicada_notes[] = " 1212121212121212121212121211212 1212121212121212121212121211212 1212121212121212121212121211212 1212121212121212121212121211212 1212121212122121212121212122111 1212121212121212121212121211212 1212121212121212121212121211212 1212121212121212121212121211212 1212121212121212121212121211212 1212121212122121212121212122111 "; // a space represents a rest
  6. int cicada_beats[] = {1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,2,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,
  7. 1,2,1,1,1,1,1,1,1,1,1,1,1,2,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,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,
  8. 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,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  9. 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,1,1,1,1,1,1,1,1,1,1,1,2,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,
  10. 1,2,1,1,1,1,1,1,1,1,1,1,1,2,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,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,
  11. 1,1,1,1,1,1,1,1,1,1,1};
  12. int cicada_tempo = 4;
  13. int cicada_repeat = 2;
  14.  
  15. int wasp_length = 156;
  16. char wasp_notes[] = "33334443333444444333344443333444433344443333 3333344444433334444333334444433444344 44433334444333334444433444344 3333444433333444443344434444434343433 ";
  17. int wasp_beats[] = {1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,2,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,
  18. 1,2,1,1,1,1,1,1,1,1,1,1,1,2,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,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,
  19. 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,1,1,1,1};
  20. int wasp_tempo = 3;
  21. int wasp_repeat = 3;
  22.  
  23.  
  24. void playTone(int tone, int duration) {
  25. for (long i = 0; i < duration * 1000L; i += tone * 2) {
  26. digitalWrite(speakerPin, HIGH);
  27. delayMicroseconds(tone);
  28. digitalWrite(speakerPin, LOW);
  29. delayMicroseconds(tone);
  30. }
  31. }
  32.  
  33. void playNote(char note, int duration) {
  34. char names[] = { '1', '2' , '3', '4' };
  35. int tones[] = { 175, 125, 75, 60 };
  36.  
  37. // play the tone corresponding to the note name
  38. for (int i = 0; i < 4; i++) {
  39. if (names[i] == note) {
  40. playTone(tones[i], duration);
  41. }
  42. }
  43. }
  44.  
  45. void setup() {
  46. pinMode(speakerPin, OUTPUT);
  47. }
  48.  
  49. void loop() {
  50. // play cicada sound
  51. for (int i = 0; i < cicada_length * cicada_repeat && played==0; i++) {
  52. if (cicada_notes[i] == ' ') {
  53. delay(cicada_beats[i%cicada_length] * cicada_tempo); // rest
  54. } else {
  55. playNote(cicada_notes[i%cicada_length], cicada_beats[i%cicada_length] * cicada_tempo);
  56. }
  57.  
  58. // pause between notes
  59. delay(cicada_tempo / 2);
  60.  
  61. }
  62.  
  63. delay(1000);
  64.  
  65. // play wasp sound
  66. for (int i = 0; i < wasp_length * wasp_repeat && played==0; i++) {
  67. if (wasp_notes[i] == ' ') {
  68. delay(wasp_beats[i%wasp_length] * wasp_tempo); // rest
  69. } else {
  70. playNote(wasp_notes[i%wasp_length], wasp_beats[i%wasp_length] * wasp_tempo);
  71. }
  72.  
  73. // pause between notes
  74. delay(wasp_tempo / 2);
  75.  
  76. }
  77.  
  78. played = 1;
  79.  
  80. }
Add Comment
Please, Sign In to add comment