Jacob Dahleen
/
I2CSong
Project for playing a song on 2 meeds sending notes over I2C
Revision 6:84c34161559e, committed 2014-02-13
- Comitter:
- jad19925
- Date:
- Thu Feb 13 23:18:39 2014 +0000
- Parent:
- 5:432203c226ed
- Child:
- 7:b8f4ab589d16
- Commit message:
- Final project, uncomment either MASTER or SLAVE to run as that
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lovesme.h Thu Feb 13 23:18:39 2014 +0000 @@ -0,0 +1,7 @@ +#include "music.h" +#ifndef LOVE +#define LOVE +const float lovesMeTempo = 120; +char loveFrequency[]={G4,E4,E4,D4,E4,G4,G4,A4,A4,C5,A4,A4,G4,G4,G4,E4,E4,D4,E4,G4,G4,A4,A4,G4,C4,E4,D4,C4}; +float loveBeat[]={12,12,12,12,12,12,24,12,12,12,12,12,12,24,12,12,12,12,12,12,24,12,12,12,12,12,12,24}; //beat array +#endif \ No newline at end of file
--- a/main.cpp Thu Feb 13 22:54:56 2014 +0000 +++ b/main.cpp Thu Feb 13 23:18:39 2014 +0000 @@ -1,6 +1,7 @@ #include "mbed.h" #include "C12832.h" #include "mario.h" +#include "lovesme.h" #define MASTER //#define SLAVE @@ -23,6 +24,7 @@ char length; const int slaveAddress = 0x42; char received = 0; +float waitBase=1; int main() { @@ -35,15 +37,16 @@ while(1) { if (fire) { leds=0xf; + waitBase = 60.0/marioTempo; for(int i=0;i<(sizeof(marioNotes)/sizeof(NoteName));i++){ if(marioNotes[i] == R) { spkr=0.0; - wait(.7*marioBeats[i]/12); + wait(waitBase*marioBeats[i]/12); } else{ spkr.period(1/(notes[marioNotes[i]])); spkr=.5; - wait(.7*marioBeats[i]/12); + wait(waitBase*marioBeats[i]/12); spkr=0.0; } } @@ -52,6 +55,7 @@ } else { leds=joy; #ifdef MASTER + waitBase = 60.0/marioTempo; if(up) { //master sends its song to slave i2cPort.start(); @@ -59,17 +63,17 @@ i2cPort.write(slaveAddress); //send tempo (BPM) //loop through song - for(int i=0;i<50/*(sizeof(marioNotes)/sizeof(NoteName))*/;i++){ + for(int i=0;i<(sizeof(marioNotes)/sizeof(NoteName));i++){ if(marioNotes[i] == R) { i2cPort.write(R); spkr=0.0; - wait(.7*marioBeats[i]/12); + wait(waitBase*marioBeats[i]/12); } else{ i2cPort.write(marioNotes[i]); spkr.period(1/(notes[marioNotes[i]])); spkr=.5; - wait(.7*marioBeats[i]/12); + wait(waitBase*marioBeats[i]/12); spkr=0.0; } } @@ -107,19 +111,20 @@ } #endif #ifdef SLAVE + waitBase = 60.0/lovesMeTempo; received = slave.receive(); if(I2CSlave::ReadAddressed==received) { //master is requesting slave song - for(int i=0;i<50/*(sizeof(marioNotes)/sizeof(NoteName))*/;i++){ - if(marioNotes[i] == R) { + for(int i=0;i<(sizeof(loveFrequency)/sizeof(NoteName));i++){ + if(loveFrequency[i] == R) { slave.write(R); spkr=0.0; - wait(.7*marioBeats[i]/12); + wait(waitBase*loveBeat[i]/12); } else{ - slave.write(marioNotes[i]); - spkr.period(1/(notes[marioNotes[i]])); + slave.write(loveFrequency[i]); + spkr.period(1/(notes[loveFrequency[i]])); spkr=.5; - wait(.7*marioBeats[i]/12); + wait(waitBase*loveBeat[i]/12); spkr=0.0; } }
--- a/mario.h Thu Feb 13 22:54:56 2014 +0000 +++ b/mario.h Thu Feb 13 23:18:39 2014 +0000 @@ -1,5 +1,7 @@ #include "music.h" -const int TEMPO = 100; +#ifndef MARIO +#define MARIO +const float marioTempo = 100; //mario theme song NoteName marioNotes[]={E5,E5,E5,R,C5,E5,G5,R,G4,R, @@ -88,4 +90,5 @@ 3,6,3,6,6,3,6,3,12, 3,6,3,4,4,4,3,6,3,12, 9,9,6,4,4,4,4,4,4, - 3,3,18}; \ No newline at end of file + 3,3,18}; +#endif \ No newline at end of file
--- a/music.h Thu Feb 13 22:54:56 2014 +0000 +++ b/music.h Thu Feb 13 23:18:39 2014 +0000 @@ -1,4 +1,5 @@ - +#ifndef MUSIC +#define MUSIC enum NoteName {C0=0,Cs0,D0,Eb0,E0,F0,Fs0,G0,Gs0,A0,Bb0,B0, C1,Cs1,D1,Eb1,E1,F1,Fs1,G1,Gs1,A1,Bb1,B1, C2,Cs2,D2,Eb2,E2,F2,Fs2,G2,Gs2,A2,Bb2,B2, @@ -18,3 +19,4 @@ 1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976, 2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951, 4186,4435,4699,4978,5274,5588,5920,6272,6645,7040,7459,7902,0}; +#endif \ No newline at end of file