Advertisement
Guest User

Sumer_is_Icummen_In_3.ino

a guest
Jun 12th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. /*
  2. Summer is Icummen In - Part 3
  3. By Chris Chungbin
  4. Plays the third part of a piece of 13th-century polyphony
  5. */
  6.  
  7. unsigned long temp=360; //set note rate in bpm of the GCF of note duration
  8. unsigned int t=0; //time var in periods of the variable "temp"
  9.  
  10. float pitch[]= {000.00,
  11. 698.46, 659.26, 587.33, 659.26, 698.46, 698.46, 659.26, 587.33, 523.25,
  12. 440.00, 440.00, 466.16, 392.00, 440.00, 000.00,
  13. 349.23, 440.00, 392.00, 466.16, 440.00, 440.00, 392.00, 349.23,
  14. 440.00, 523.25, 587.33, 587.33, 523.25, 000.00,
  15. 698.46, 587.33, 698.46, 000.00,
  16. 523.25, 440.00, 466.16, 392.00, 440.00, 523.25, 466.16, 440.00,
  17. 349.23, 440.00, 392.00, 329.63, 349.23, 000.00,
  18. 440.00, 440.00, 392.00, 466.16, 523.25, 523.25, 587.33, 659.26,
  19. 698.46, 659.26, 587.33, 659.26, 698.46, 000.00,
  20. 523.25, 587.33, 523.25, 466.16, 0,
  21. 349.23, 440.00, 466.16, 392.00, 440.00, 466.16, 523.25,
  22. 440.00, 523.25, 392.00, 329.63, 349.23, 000.00}; //frequency of each note in Hz
  23. unsigned int time[]= {48,
  24. 2, 1, 2, 1, 2, 1, 1, 1, 1,
  25. 2, 1, 2, 1, 3, 3,
  26. 2, 1, 2, 1, 2, 1, 2, 1,
  27. 2, 1, 2, 1, 3, 3,
  28. 3, 3, 3, 3,
  29. 2, 1, 2, 1, 2, 1, 2, 1,
  30. 2, 1, 2, 1, 3, 3,
  31. 2, 1, 2, 1, 2, 1, 2, 1,
  32. 2, 1, 2, 1, 3, 3,
  33. 3, 3, 3, 2, 1,
  34. 2, 1, 2, 1, 3, 2, 1,
  35. 2, 1, 2, 1, 3, 3, 1, 1}; //duration of note in time rate periods - multiple of shortest note value - add 2 other elements after the last duration to allow index shifting later
  36.  
  37. void setup() {
  38. pinMode(8, OUTPUT); //audio out
  39. pinMode(7, INPUT); //sync pulse from syncing board
  40. pinMode(6, OUTPUT); //ready signal out to syncing board
  41. digitalWrite(6, LOW);
  42.  
  43.  
  44. temp=60000000/temp; //convert tempo to period in microseconds
  45.  
  46. for(int i=sizeof(time)/2-1; i>0; i--) { //this and the next for() convert time[] to start time of note
  47. time[i]=time[i-1];
  48. }
  49. time[0]=0;
  50. for(int i=1; i<sizeof(time)/2-1; i++) {
  51. time[i]=time[i-1]+time[i];
  52. }
  53. digitalWrite(6, HIGH);
  54. }
  55.  
  56. void loop() {
  57. t=0;
  58. unsigned long s=micros(); //period start time
  59. unsigned long p=0; //period
  60. boolean in=0; //input
  61. boolean waiting=0; //ready to wait for next pulse?
  62. while(!in) {
  63. in=digitalRead(7);
  64. }
  65. digitalWrite(6,LOW);
  66. in=0;
  67. while(in) {
  68. in=digitalRead(7);
  69. }
  70. while(!in) {
  71. in=digitalRead(7);
  72. }
  73. unsigned long start=micros(); //start time recorded
  74.  
  75. for (int i=0; i<sizeof(pitch)/4; i++) {
  76. while(time[i+1]>t) {
  77. s=micros();
  78. in=digitalRead(7);
  79. if(waiting&&in) {
  80. t=t+1;
  81. waiting=0;
  82. }
  83. if(!(waiting||in)) {
  84. waiting=1;
  85. }
  86. if(pitch[i]==0) {
  87. }
  88. else {
  89. p=1000000/pitch[i];
  90. digitalWrite(8, HIGH);
  91. delayMicroseconds(p/3);
  92. digitalWrite(8, LOW);
  93. while(micros()-s<p) {
  94. //waits in this incredibly exciting loop until the current period is done
  95. }
  96. }
  97. }
  98. }
  99.  
  100. while(1) {
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement