Arthur Semjonov
/
Serial_Communication
Master Code (Doctor)
main.cpp
- Committer:
- ArthurSemjonov
- Date:
- 2014-02-18
- Revision:
- 0:757da73b539a
File content as of revision 0:757da73b539a:
#include "mbed.h" #include "music.h" #include "TARDIS_ARCHIVE.h" /********************************************************/ AnalogIn pitch(p19); //Potentiometer sets up the pitch of the song AnalogIn tempo(p20); //Potentiometer to set tempo of the song DigitalIn joyUP(p15); //Play song of the Doctor (Master) DigitalIn joyDOWN(p12); //Play song of the Companion (Slave) DigitalIn joyMID(p14); //Stop playing either master or Slave song. PwmOut speaker(p26); //Speaker on the mbed /*============SERIAL COMMUNICATION==============*/ I2C doctor_tardis (p9, p10); //mosi/miso/sck of the Doctor /********************************************************/ Serial pc(USBTX, USBRX); int send_note; //word we will send int rec_note; //value received from master const int addr = 0x52; /************************************|I Am the Doctor|**********************************/ float frequency[]= {D3, E3, F3, G3, F3 ,E3, D3, E3, D3 }; float beat[]= {.7,.7,.7,0.3,.7,.7,.7,0.3,0.3, 0.3}; /***************************************************************************************/ int main() { while(1) { if (joyUP) { playTheDoctor(); wait(0.01); } else if (joyDOWN) { playTheCompanion(); wait(0.01); } else if (joyMID) { songOfMyPeople(frequency, beat); //breakConnection(); //Dalek attack wait(0.01); } wait (0.01); } } //======================================================================================================================================== //======================================================================================================================================== //======================================================================================================================================== void playTheCompanion() { doctor_tardis.start(); doctor_tardis.write(addr|0x01); while(doctor_tardis.read(addr)!=0) { rec_note = doctor_tardis.read(addr); pc.printf("\n\rReceived note = '%d'", rec_note); speaker.period(1/(2*notes[rec_note])); // set PWM period speaker=0.5; } doctor_tardis.stop(); return; } void playTheDoctor() { doctor_tardis.start(); doctor_tardis.write(addr); for (int i=0; i<=8; i++) { send_note = frequency[i]; doctor_tardis.write(send_note); float value = lookupNoteFrq(frequency, i); speaker.period(1/(2*value)); // set PWM period speaker=0.5; wait(0.4*beat[i]); if (joyMID) { speaker = 0; //reset speaker duty cycle doctor_tardis.stop(); wait(2); //delay for inferior humans return; } else if(joyDOWN) { doctor_tardis.stop(); return; } } doctor_tardis.stop(); } void playTheSlave(int frequency) { float frqNote = notes[frequency]; pc.printf("\n\rFrq of the note = '%f", frequency, "\n\rFrq of the note = '%f'", frqNote); speaker.period(1/(2*frqNote)); // set PWM period speaker=0.5; } /*--------------Play the song of the Doctor--------------*/ void songOfMyPeople(float frequency[], float beat[]) { while(1) { //run infinetly unless interupted for (int i=0; i<=8; i++) { float value = lookupNoteFrq(frequency, i); speaker.period(1/(2*value)); // set PWM period speaker=0.5; // set duty cycle wait(0.4*beat[i]); if (joyMID) { speaker = 0; //reset speaker duty cycle wait(2); //delay for inferior humans return; } } speaker = 0; wait(0.25); } } /*--------------Play the song of the Companions--------------*/ void songOfYourPeople(int frequency) { } /** Look up the frequency of the note from music.h file * @param frq[], array of the song * @param k, location of the note in the array */ float lookupNoteFrq(float frq[], int k) { int location_of_note = frequency[k]; pc.printf("\n\rLocation of the note = '%d'", location_of_note); float vNote = notes[location_of_note]; pc.printf("\n\rFrq of the note = '%f'", vNote); return vNote; }