Master Code (Doctor)

Dependencies:   mbed

Committer:
ArthurSemjonov
Date:
Tue Feb 18 01:19:44 2014 +0000
Revision:
0:757da73b539a
First draft

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ArthurSemjonov 0:757da73b539a 1 #include "mbed.h"
ArthurSemjonov 0:757da73b539a 2 #include "music.h"
ArthurSemjonov 0:757da73b539a 3 #include "TARDIS_ARCHIVE.h"
ArthurSemjonov 0:757da73b539a 4
ArthurSemjonov 0:757da73b539a 5 /********************************************************/
ArthurSemjonov 0:757da73b539a 6 AnalogIn pitch(p19); //Potentiometer sets up the pitch of the song
ArthurSemjonov 0:757da73b539a 7 AnalogIn tempo(p20); //Potentiometer to set tempo of the song
ArthurSemjonov 0:757da73b539a 8 DigitalIn joyUP(p15); //Play song of the Doctor (Master)
ArthurSemjonov 0:757da73b539a 9 DigitalIn joyDOWN(p12); //Play song of the Companion (Slave)
ArthurSemjonov 0:757da73b539a 10 DigitalIn joyMID(p14); //Stop playing either master or Slave song.
ArthurSemjonov 0:757da73b539a 11 PwmOut speaker(p26); //Speaker on the mbed
ArthurSemjonov 0:757da73b539a 12
ArthurSemjonov 0:757da73b539a 13 /*============SERIAL COMMUNICATION==============*/
ArthurSemjonov 0:757da73b539a 14 I2C doctor_tardis (p9, p10); //mosi/miso/sck of the Doctor
ArthurSemjonov 0:757da73b539a 15 /********************************************************/
ArthurSemjonov 0:757da73b539a 16 Serial pc(USBTX, USBRX);
ArthurSemjonov 0:757da73b539a 17 int send_note; //word we will send
ArthurSemjonov 0:757da73b539a 18 int rec_note; //value received from master
ArthurSemjonov 0:757da73b539a 19 const int addr = 0x52;
ArthurSemjonov 0:757da73b539a 20
ArthurSemjonov 0:757da73b539a 21 /************************************|I Am the Doctor|**********************************/
ArthurSemjonov 0:757da73b539a 22 float frequency[]= {D3, E3, F3, G3,
ArthurSemjonov 0:757da73b539a 23 F3 ,E3, D3, E3, D3
ArthurSemjonov 0:757da73b539a 24 };
ArthurSemjonov 0:757da73b539a 25 float beat[]= {.7,.7,.7,0.3,.7,.7,.7,0.3,0.3, 0.3};
ArthurSemjonov 0:757da73b539a 26 /***************************************************************************************/
ArthurSemjonov 0:757da73b539a 27
ArthurSemjonov 0:757da73b539a 28 int main()
ArthurSemjonov 0:757da73b539a 29 {
ArthurSemjonov 0:757da73b539a 30 while(1) {
ArthurSemjonov 0:757da73b539a 31
ArthurSemjonov 0:757da73b539a 32 if (joyUP) {
ArthurSemjonov 0:757da73b539a 33 playTheDoctor();
ArthurSemjonov 0:757da73b539a 34 wait(0.01);
ArthurSemjonov 0:757da73b539a 35 } else if (joyDOWN) {
ArthurSemjonov 0:757da73b539a 36 playTheCompanion();
ArthurSemjonov 0:757da73b539a 37 wait(0.01);
ArthurSemjonov 0:757da73b539a 38 } else if (joyMID) {
ArthurSemjonov 0:757da73b539a 39 songOfMyPeople(frequency, beat);
ArthurSemjonov 0:757da73b539a 40 //breakConnection(); //Dalek attack
ArthurSemjonov 0:757da73b539a 41 wait(0.01);
ArthurSemjonov 0:757da73b539a 42 }
ArthurSemjonov 0:757da73b539a 43 wait (0.01);
ArthurSemjonov 0:757da73b539a 44 }
ArthurSemjonov 0:757da73b539a 45 }
ArthurSemjonov 0:757da73b539a 46 //========================================================================================================================================
ArthurSemjonov 0:757da73b539a 47 //========================================================================================================================================
ArthurSemjonov 0:757da73b539a 48 //========================================================================================================================================
ArthurSemjonov 0:757da73b539a 49
ArthurSemjonov 0:757da73b539a 50
ArthurSemjonov 0:757da73b539a 51 void playTheCompanion()
ArthurSemjonov 0:757da73b539a 52 {
ArthurSemjonov 0:757da73b539a 53 doctor_tardis.start();
ArthurSemjonov 0:757da73b539a 54 doctor_tardis.write(addr|0x01);
ArthurSemjonov 0:757da73b539a 55 while(doctor_tardis.read(addr)!=0) {
ArthurSemjonov 0:757da73b539a 56 rec_note = doctor_tardis.read(addr);
ArthurSemjonov 0:757da73b539a 57 pc.printf("\n\rReceived note = '%d'", rec_note);
ArthurSemjonov 0:757da73b539a 58 speaker.period(1/(2*notes[rec_note])); // set PWM period
ArthurSemjonov 0:757da73b539a 59 speaker=0.5;
ArthurSemjonov 0:757da73b539a 60 }
ArthurSemjonov 0:757da73b539a 61 doctor_tardis.stop();
ArthurSemjonov 0:757da73b539a 62 return;
ArthurSemjonov 0:757da73b539a 63 }
ArthurSemjonov 0:757da73b539a 64
ArthurSemjonov 0:757da73b539a 65 void playTheDoctor()
ArthurSemjonov 0:757da73b539a 66 {
ArthurSemjonov 0:757da73b539a 67 doctor_tardis.start();
ArthurSemjonov 0:757da73b539a 68 doctor_tardis.write(addr);
ArthurSemjonov 0:757da73b539a 69
ArthurSemjonov 0:757da73b539a 70 for (int i=0; i<=8; i++) {
ArthurSemjonov 0:757da73b539a 71 send_note = frequency[i];
ArthurSemjonov 0:757da73b539a 72 doctor_tardis.write(send_note);
ArthurSemjonov 0:757da73b539a 73
ArthurSemjonov 0:757da73b539a 74 float value = lookupNoteFrq(frequency, i);
ArthurSemjonov 0:757da73b539a 75 speaker.period(1/(2*value)); // set PWM period
ArthurSemjonov 0:757da73b539a 76 speaker=0.5;
ArthurSemjonov 0:757da73b539a 77 wait(0.4*beat[i]);
ArthurSemjonov 0:757da73b539a 78
ArthurSemjonov 0:757da73b539a 79 if (joyMID) {
ArthurSemjonov 0:757da73b539a 80 speaker = 0; //reset speaker duty cycle
ArthurSemjonov 0:757da73b539a 81 doctor_tardis.stop();
ArthurSemjonov 0:757da73b539a 82 wait(2); //delay for inferior humans
ArthurSemjonov 0:757da73b539a 83 return;
ArthurSemjonov 0:757da73b539a 84 } else if(joyDOWN) {
ArthurSemjonov 0:757da73b539a 85 doctor_tardis.stop();
ArthurSemjonov 0:757da73b539a 86 return;
ArthurSemjonov 0:757da73b539a 87 }
ArthurSemjonov 0:757da73b539a 88 }
ArthurSemjonov 0:757da73b539a 89 doctor_tardis.stop();
ArthurSemjonov 0:757da73b539a 90 }
ArthurSemjonov 0:757da73b539a 91
ArthurSemjonov 0:757da73b539a 92 void playTheSlave(int frequency)
ArthurSemjonov 0:757da73b539a 93 {
ArthurSemjonov 0:757da73b539a 94 float frqNote = notes[frequency];
ArthurSemjonov 0:757da73b539a 95 pc.printf("\n\rFrq of the note = '%f", frequency, "\n\rFrq of the note = '%f'", frqNote);
ArthurSemjonov 0:757da73b539a 96 speaker.period(1/(2*frqNote)); // set PWM period
ArthurSemjonov 0:757da73b539a 97 speaker=0.5;
ArthurSemjonov 0:757da73b539a 98 }
ArthurSemjonov 0:757da73b539a 99
ArthurSemjonov 0:757da73b539a 100 /*--------------Play the song of the Doctor--------------*/
ArthurSemjonov 0:757da73b539a 101 void songOfMyPeople(float frequency[], float beat[])
ArthurSemjonov 0:757da73b539a 102 {
ArthurSemjonov 0:757da73b539a 103 while(1) { //run infinetly unless interupted
ArthurSemjonov 0:757da73b539a 104 for (int i=0; i<=8; i++) {
ArthurSemjonov 0:757da73b539a 105 float value = lookupNoteFrq(frequency, i);
ArthurSemjonov 0:757da73b539a 106 speaker.period(1/(2*value)); // set PWM period
ArthurSemjonov 0:757da73b539a 107 speaker=0.5; // set duty cycle
ArthurSemjonov 0:757da73b539a 108 wait(0.4*beat[i]);
ArthurSemjonov 0:757da73b539a 109 if (joyMID) {
ArthurSemjonov 0:757da73b539a 110 speaker = 0; //reset speaker duty cycle
ArthurSemjonov 0:757da73b539a 111 wait(2); //delay for inferior humans
ArthurSemjonov 0:757da73b539a 112 return;
ArthurSemjonov 0:757da73b539a 113 }
ArthurSemjonov 0:757da73b539a 114 }
ArthurSemjonov 0:757da73b539a 115 speaker = 0;
ArthurSemjonov 0:757da73b539a 116 wait(0.25);
ArthurSemjonov 0:757da73b539a 117 }
ArthurSemjonov 0:757da73b539a 118 }
ArthurSemjonov 0:757da73b539a 119 /*--------------Play the song of the Companions--------------*/
ArthurSemjonov 0:757da73b539a 120
ArthurSemjonov 0:757da73b539a 121 void songOfYourPeople(int frequency)
ArthurSemjonov 0:757da73b539a 122 {
ArthurSemjonov 0:757da73b539a 123
ArthurSemjonov 0:757da73b539a 124 }
ArthurSemjonov 0:757da73b539a 125
ArthurSemjonov 0:757da73b539a 126
ArthurSemjonov 0:757da73b539a 127 /** Look up the frequency of the note from music.h file
ArthurSemjonov 0:757da73b539a 128 * @param frq[], array of the song
ArthurSemjonov 0:757da73b539a 129 * @param k, location of the note in the array
ArthurSemjonov 0:757da73b539a 130 */
ArthurSemjonov 0:757da73b539a 131 float lookupNoteFrq(float frq[], int k)
ArthurSemjonov 0:757da73b539a 132 {
ArthurSemjonov 0:757da73b539a 133 int location_of_note = frequency[k];
ArthurSemjonov 0:757da73b539a 134 pc.printf("\n\rLocation of the note = '%d'", location_of_note);
ArthurSemjonov 0:757da73b539a 135
ArthurSemjonov 0:757da73b539a 136 float vNote = notes[location_of_note];
ArthurSemjonov 0:757da73b539a 137 pc.printf("\n\rFrq of the note = '%f'", vNote);
ArthurSemjonov 0:757da73b539a 138 return vNote;
ArthurSemjonov 0:757da73b539a 139 }