Advertisement
Guest User

Sumer_Is_Icummen_In_5.ino

a guest
Jun 12th, 2013
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. /*
  2. Summer is Icummen In - Part 5
  3. By Chris Chungbin
  4. Plays the first accompanying part of a piece of 13th-century polyphony
  5. **Also acts as the time synchronization controller**
  6. Sends a pulse just before the beginning of each "t" period
  7. */
  8.  
  9. unsigned long temp=360; //set note rate in bpm of the GCF of note duration
  10. unsigned int t=0; //time var in periods of the variable "temp"
  11.  
  12. float pitch[]= {349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
  13. 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
  14. 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
  15. 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
  16. 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
  17. 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
  18. 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
  19. 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
  20. 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00 }; //frequency of each note in Hz
  21. unsigned int time[]= {3, 3, 3, 2, 1, 3, 3, 3, 3,
  22. 3, 3, 3, 2, 1, 3, 3, 3, 3,
  23. 3, 3, 3, 2, 1, 3, 3, 3, 3,
  24. 3, 3, 3, 2, 1, 3, 3, 3, 3,
  25. 3, 3, 3, 2, 1, 3, 3, 3, 3,
  26. 3, 3, 3, 2, 1, 3, 3, 3, 3,
  27. 3, 3, 3, 2, 1, 3, 3, 3, 3,
  28. 3, 3, 3, 2, 1, 3, 3, 3, 3,
  29. 3, 3, 3, 2, 1, 3, 3, 3, 6, 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
  30.  
  31. void setup() {
  32. pinMode(8, OUTPUT); //audio out
  33. pinMode(7, OUTPUT); //sync pulse out to other boards
  34. pinMode(13, INPUT); //part 1 reply - connected to other boards' ready signal
  35. pinMode(12, INPUT); //part 2 reply
  36. pinMode(11, INPUT); //part 3 reply
  37. pinMode(10, INPUT); //part 4 reply
  38. pinMode(9, INPUT); //part 6 reply
  39. digitalWrite(7,LOW);
  40.  
  41. temp=60000000/temp; //convert bpm tempo to period in microseconds
  42.  
  43. for(int i=0; i<sizeof(pitch)/4; i++) {
  44. pitch[i]=pitch[i]/2;
  45. }
  46.  
  47. for(int i=sizeof(time)/2-1; i>0; i--) { //this and the next for() convert time[] to start time of note
  48. time[i]=time[i-1];
  49. }
  50. time[0]=0;
  51. for(int i=1; i<sizeof(time)/2-1; i++) {
  52. time[i]=time[i-1]+time[i];
  53. }
  54.  
  55.  
  56. delay(1000); //extra long wait for other boards to be ready. allows inaccuracy in resetting
  57.  
  58. while(!(digitalRead(9)&&digitalRead(10)&&digitalRead(11)&&digitalRead(12)&&digitalRead(13))) {
  59. //do nothing until all boards report HIGH
  60. }
  61.  
  62. digitalWrite(7, HIGH); //tell the other boards to turn off their ready signal and wait for start
  63. delay(1);
  64. digitalWrite(7, LOW);
  65. delay(1);
  66.  
  67. }
  68.  
  69. void loop() {
  70. t=0;
  71. unsigned long s=micros(); //period start time
  72. unsigned long p=0; //period
  73. unsigned long tTime=micros(); //time of last t-sync pulse start
  74. unsigned int start=micros(); //start time recorded
  75.  
  76. for (int i=0; i<sizeof(pitch)/4; i++) {
  77. while(time[i+1]>t) {
  78. s=micros();
  79. if(micros()-tTime>temp) { //t update and sync pulse
  80. t=t+1;
  81. digitalWrite(7, HIGH);
  82. tTime=micros();
  83. }
  84. if(micros()-tTime>temp/2) {
  85. digitalWrite(7, LOW);
  86. }
  87. if(pitch[i]==0) {
  88. }
  89. else {
  90. p=1000000/pitch[i];
  91. digitalWrite(8, HIGH);
  92. delayMicroseconds(p/3);
  93. digitalWrite(8, LOW);
  94. while(micros()-s<p) {
  95. //waits in this incredibly exciting loop until the current period is done
  96. }
  97. }
  98. }
  99. }
  100. digitalWrite(7,LOW);
  101. while(1) {
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement