Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Summer is Icummen In - Part 5
- By Chris Chungbin
- Plays the first accompanying part of a piece of 13th-century polyphony
- **Also acts as the time synchronization controller**
- Sends a pulse just before the beginning of each "t" period
- */
- unsigned long temp=360; //set note rate in bpm of the GCF of note duration
- unsigned int t=0; //time var in periods of the variable "temp"
- float pitch[]= {349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
- 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
- 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
- 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
- 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
- 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
- 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
- 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00,
- 349.23, 392.00, 349.32, 392.00, 466.16, 523.25, 466.16, 523.25, 000.00 }; //frequency of each note in Hz
- unsigned int time[]= {3, 3, 3, 2, 1, 3, 3, 3, 3,
- 3, 3, 3, 2, 1, 3, 3, 3, 3,
- 3, 3, 3, 2, 1, 3, 3, 3, 3,
- 3, 3, 3, 2, 1, 3, 3, 3, 3,
- 3, 3, 3, 2, 1, 3, 3, 3, 3,
- 3, 3, 3, 2, 1, 3, 3, 3, 3,
- 3, 3, 3, 2, 1, 3, 3, 3, 3,
- 3, 3, 3, 2, 1, 3, 3, 3, 3,
- 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
- void setup() {
- pinMode(8, OUTPUT); //audio out
- pinMode(7, OUTPUT); //sync pulse out to other boards
- pinMode(13, INPUT); //part 1 reply - connected to other boards' ready signal
- pinMode(12, INPUT); //part 2 reply
- pinMode(11, INPUT); //part 3 reply
- pinMode(10, INPUT); //part 4 reply
- pinMode(9, INPUT); //part 6 reply
- digitalWrite(7,LOW);
- temp=60000000/temp; //convert bpm tempo to period in microseconds
- for(int i=0; i<sizeof(pitch)/4; i++) {
- pitch[i]=pitch[i]/2;
- }
- for(int i=sizeof(time)/2-1; i>0; i--) { //this and the next for() convert time[] to start time of note
- time[i]=time[i-1];
- }
- time[0]=0;
- for(int i=1; i<sizeof(time)/2-1; i++) {
- time[i]=time[i-1]+time[i];
- }
- delay(1000); //extra long wait for other boards to be ready. allows inaccuracy in resetting
- while(!(digitalRead(9)&&digitalRead(10)&&digitalRead(11)&&digitalRead(12)&&digitalRead(13))) {
- //do nothing until all boards report HIGH
- }
- digitalWrite(7, HIGH); //tell the other boards to turn off their ready signal and wait for start
- delay(1);
- digitalWrite(7, LOW);
- delay(1);
- }
- void loop() {
- t=0;
- unsigned long s=micros(); //period start time
- unsigned long p=0; //period
- unsigned long tTime=micros(); //time of last t-sync pulse start
- unsigned int start=micros(); //start time recorded
- for (int i=0; i<sizeof(pitch)/4; i++) {
- while(time[i+1]>t) {
- s=micros();
- if(micros()-tTime>temp) { //t update and sync pulse
- t=t+1;
- digitalWrite(7, HIGH);
- tTime=micros();
- }
- if(micros()-tTime>temp/2) {
- digitalWrite(7, LOW);
- }
- if(pitch[i]==0) {
- }
- else {
- p=1000000/pitch[i];
- digitalWrite(8, HIGH);
- delayMicroseconds(p/3);
- digitalWrite(8, LOW);
- while(micros()-s<p) {
- //waits in this incredibly exciting loop until the current period is done
- }
- }
- }
- }
- digitalWrite(7,LOW);
- while(1) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement