Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Summer is Icummen In - Part 6
- By Chris Chungbin
- Plays the second accompanying part of a piece of 13th-century polyphony
- */
- 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[]= {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, 349.23, 392.00, 349.32, 0}; //frequency of each note in Hz
- unsigned int time[]= {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, 3, 3, 3, 3, 1,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, INPUT); //sync pulse from syncing board
- pinMode(6, OUTPUT); //ready signal out to syncing board
- digitalWrite(6, LOW);
- temp=60000000/temp; //convert 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];
- }
- digitalWrite(6, HIGH);
- }
- void loop() {
- t=0;
- unsigned long s=micros(); //period start time
- unsigned long p=0; //period
- boolean in=0; //input
- boolean waiting=0; //ready to wait for next pulse?
- while(!in) {
- in=digitalRead(7);
- }
- digitalWrite(6,LOW);
- in=0;
- while(in) {
- in=digitalRead(7);
- }
- while(!in) {
- in=digitalRead(7);
- }
- unsigned long start=micros(); //start time recorded
- for (int i=0; i<sizeof(pitch)/4; i++) {
- while(time[i+1]>t) {
- s=micros();
- in=digitalRead(7);
- if(waiting&&in) {
- t=t+1;
- waiting=0;
- }
- if(!(waiting||in)) {
- waiting=1;
- }
- 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
- }
- }
- }
- }
- while(1) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement