Schupp

melodie1002

Jan 21st, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. // Anfangdes Arduino-Programms
  2. const byte BUTTONPIN = 2;
  3. const byte SPEAKERPIN = 10;
  4. const byte MELODIES = 100;
  5. ///////////////////
  6. //
  7.  
  8. /////////////////////
  9. void setup()
  10. {
  11. // put your setup code here, to run once:
  12. Serial.begin(115200);
  13. // Serial.println("I\nprogram is up and running...");
  14. pinMode(BUTTONPIN, INPUT_PULLUP);
  15. pinMode(SPEAKERPIN, OUTPUT);
  16. jursTone(SPEAKERPIN, 440, 600);
  17. }
  18. unsigned long startTime = millis();
  19. int jursSteps = 0;
  20. int jursreturn = 0;
  21. bool firstround = true;
  22. unsigned long firstjurstiming = micros();
  23. bool test = true;
  24.  
  25. /////////////////////////////////////////////////////////////////////////////////////
  26. //ORIGINAL
  27. /////////////////////////////////////////////////////////////////////////////////////
  28. void jursTone(const byte pin,const int frequency, const int duration)
  29. {
  30. unsigned long startTime=millis();
  31. int halfPeriod=1000000L/frequency/2;
  32. while(millis()-startTime<duration)
  33. {
  34. digitalWrite(SPEAKERPIN,HIGH);
  35. delayMicroseconds(halfPeriod);
  36. digitalWrite(SPEAKERPIN,LOW);
  37. delayMicroseconds(halfPeriod);
  38. }
  39. }
  40. //////////////////////////////////////////////////////////////////////////////////////
  41. //Versuch ohne delay
  42. //////////////////////////////////////////////////////////////////////////////////////
  43. byte mypin;
  44. int myfrequency;
  45. int myduration;
  46. int jursTone(const byte pin, const int frequency, const int duration)
  47. {
  48. if (firstround) {
  49. startTime = millis();
  50. mypin=pin;
  51. myfrequency=frequency;
  52. myduration=duration;
  53. firstround = false;
  54. jursreturn = 0;
  55. jursSteps=0;
  56. }
  57. Serial.println(jursSteps);
  58. unsigned long halfPeriod = 1000000UL / myfrequency / 2UL;
  59.  
  60. if (millis() - startTime < myduration || jursSteps != 0) {
  61. switch (jursSteps) {
  62. case 0:
  63. digitalWrite(SPEAKERPIN, HIGH);
  64. jursSteps++;
  65. firstjurstiming = micros();
  66. break;
  67. case 1:
  68. if ((micros() - firstjurstiming) > (unsigned long)halfPeriod)
  69. jursSteps++;
  70. break;
  71. case 2:
  72. digitalWrite(SPEAKERPIN, LOW);
  73. jursSteps++;
  74. firstjurstiming = micros();
  75. break;
  76. case 3:
  77. if (micros() - firstjurstiming > (unsigned long) halfPeriod)
  78. jursSteps++;
  79. break;
  80. case 4:
  81. //jursreturn = 0;
  82. jursSteps = 0;
  83. break;
  84. }
  85.  
  86. } else {
  87. jursreturn = 1;
  88.  
  89. }
  90. Serial.println(jursSteps);
  91. return jursreturn;
  92. }
  93.  
  94.  
  95.  
  96.  
  97. uint16_t liedcount;
  98. uint16_t liedende;
  99. uint8_t liednr = 0;
  100. byte lspeed = 0;
  101. uint8_t dauer;
  102. uint8_t hoehe;
  103. uint8_t note ;
  104. void playlied(int liednr)
  105. {
  106. liednr = liednr;
  107. // Serial.print("Melodie Nr.: ");Serial.println(liednr);
  108. if (liednr == MELODIES) liedende = sizeof(lied);
  109. //bei letztem Lied ist Endwert Größe des Arrays
  110. else liedende = liedofs[liednr]; //ansonsten 1 Byte vor nächstem Lied
  111. //Serial.print(liedofs[liednr - 1]); Serial.print(" "); Serial.println(liedende);
  112. // liedcount=liedofs[liednr-1];
  113. lspeed = pgm_read_byte(&lied[liedcount]);
  114. // Serial.print("Speed: ");Serial.print(lspeed);Serial.println();
  115. liedcount = liedofs[liednr - 1] + 1;
  116. }
  117. boolean playSong = false;
  118. int steps = 0;
  119. unsigned long lastloopmillis = millis();
  120. void updatelied()
  121. {
  122. if (liedcount < liedende && playSong)
  123. {
  124. int thisdelay = 0;
  125. if (hoehe) thisdelay = int(dauer) * int(lspeed) * int(20); else lspeed * dauer * 20;
  126. note = pgm_read_byte(&lied[liedcount]); //Note holen
  127. hoehe = note & 0x1f; //Tonhöhe ausmaskieren (5 Bit)
  128. dauer = pgm_read_byte(&duration[note >> 5]); // 5 Bit nach rechts schieben und Zeit holen
  129. switch (steps) {
  130. case 0: lastloopmillis = millis(); if (hoehe) steps = steps + jursTone(SPEAKERPIN, pgm_read_word(&ton[hoehe - 1]), dauer * lspeed * 20); break;
  131. case 1: if (millis() - lastloopmillis > thisdelay) steps++; break;
  132. case 2: liedcount++; steps = 0; firstround = true; break;
  133. }
  134. } else playSong = false;
  135. }
  136.  
  137. byte b, liedNummer;
  138. bool buttonPressed()
  139. {
  140. static byte oldState;
  141. byte newState = digitalRead(BUTTONPIN);
  142. if (newState != oldState && newState == LOW)
  143. {
  144. oldState = newState;
  145. return true;
  146. }
  147. oldState = newState;
  148. return false;
  149. }
  150.  
  151.  
  152. void loop()
  153.  
  154. {
  155. updatelied();
  156. if (buttonPressed())
  157. {
  158. randomSeed(micros());
  159. playlied(1 + random(100));
  160. playSong = true;
  161. }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment