All pastes #2105521 Raw Edit

Unnamed

public cpp v1 · immutable
#2105521 ·published 2012-01-23 22:44 UTC
rendered paste body
//Melody - Will play a melody //Constants#define speakerPin 14 //Pin the speaker is connected to#define noteLength 300 //How long the notes last#define A 440 //Frequency of lower A (lowest playable note) in Hzfloat* glob; //to hold a pointer so I can pass an array from setup() to loop()int numNotes; //how many notes there are in the melody. Also needs to be passed from setup() to loop()//Functions:float switcher(int m, float di) { //convert the multiplier to a frequency  float ret = A + di * m; //return value is lower A + the difference per note * the note multiplier  return ret;}float switchNote(char* x, float d) { //I am sure there's a better way to do this  int multi;    if (strcmp(x, "An") == 0) {    multi = 0; // the multiplier for this letter pair  }  else if (strcmp(x, "As") == 0) {    multi = 1;  }  else if (strcmp(x, "Bb") == 0) {    multi = 1;  }  else if (strcmp(x, "Bn") == 0) {    multi = 2;  }  else if (strcmp(x, "Bs") == 0) {    multi = 3;  }  else if (strcmp(x, "Cb") == 0) {    multi = 2;  }  else if (strcmp(x, "Cn") == 0) {    multi = 3;  }  else if (strcmp(x, "Cs") == 0) {    multi = 4;  }  else if (strcmp(x, "Db") == 0) {    multi = 4;   }  else if (strcmp(x, "Dn") == 0) {    multi = 5;  }  else if (strcmp(x, "Ds") == 0) {    multi = 6;  }  else if (strcmp(x, "Eb") == 0) {    multi = 6;  }  else if (strcmp(x, "En") == 0) {    multi = 7;   }  else if (strcmp(x, "Es") == 0) {    multi = 8;   }  else if (strcmp(x, "Fb") == 0) {    multi = 7;   }  else if (strcmp(x, "Fn") == 0) {    multi = 8;   }  else if (strcmp(x, "Fs") == 0) {    multi = 9;   }  else if (strcmp(x, "Gb") == 0) {    multi = 9;   }  else if (strcmp(x, "Gn") == 0) {    multi = 10;   }  else if (strcmp(x, "Gs") == 0) {    multi = 11;   }  else if (strcmp(x, "Ab") == 0) {    multi = 11;  }  else {    multi = random(0, 12);    //tone(speakerPin, 880, 500);    //delay(500);  }  Serial.print("multi=");  Serial.println(multi);  return switcher(multi, d); //Calculate and return the frequency}//The standard functionsvoid setup() {      randomSeed(analogRead(0));  pinMode(speakerPin, OUTPUT);  Serial.begin(9600);  //The melody to play goes here  char melody[] = "Cn,Gn,En,An,Bn,As,An,Gn,En,Gn,An,Fn,Gn,En,Cn,Dn,Bn,"; //Must end with a comma, so length is multiple of 3  //array for the frequencies  numNotes = strlen(melody) / 3;  float* freqs = (float*) malloc(numNotes*sizeof(float)); //C  //float* freqs = new float [numNotes]; // C++  float diff = A/12;  //find note interval in Hz so it can be passed down through the loop    for(int i=0; i<numNotes; i++) { //separate the melody out into notes    char currNote[2];    currNote[0] = melody[3*i];    currNote[1] = melody[(3*i)+1];    freqs[i] = switchNote(currNote, diff); //convert the note to a frequency and store it in freqs  }  glob = freqs;}void loop() {  for(int i=0; i<numNotes; i++) {    tone(speakerPin, glob[i], noteLength);    delay(500);  }  delay(750);}