Plays songs and changes LED Color

Dependencies:   C12832 mbed

main.cpp

Committer:
nadiyah
Date:
2014-02-13
Revision:
1:028b0f4df520
Parent:
0:c6da9c77286d

File content as of revision 1:028b0f4df520:

#include "mbed.h"

//#include "TextLCD.h"



//#define MASTER
#define SLAVE
#include "mbed.h"
PwmOut buzzer(p26);
/*Table 4.3: Frequencies of notes used in tune
Word/syllable Musical note Frequency (Hz) Beats
Oran- E 659 1
ges C# 554 1
and E 659 1
le- C# 554 1
mons, A 440 1
say B 494 ½
the C# 554 ½
bells D 587 1
of B 494 1
St E 659 1
Clem- C# 554 1
ent’s A 440 2
Analog Output 73*/

PwmOut r(p23), g(p24), b(p25);
Serial usbSerial(USBTX,USBRX);
AnalogIn pot(p19); 
AnalogIn pot1(p20);
DigitalIn JS(p14);
//frequency array
//float frequency[]={659,554,659,554,440,494,554,587,494,659,554,440};

#include "C12832.h"


C12832 lcd(p5, p7, p6, p8, p11);

char frequency[]={Gs4,E5,E5,D5,E5,GS4,GS4,A4,A4,C4,A4,A4,GS4,GS4,GS4,E5,E5,D5,E5,Gs4,Gs4,A4,A4,Gs4,C4,E5,D5,C4};
float beat[]={1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2}; //beat array





#ifdef MASTER
I2C i2cPort(p9,p10);
#endif
#ifdef SLAVE
I2CSlave slave(p9,p10);
#endif
char note;
char length;
const int slaveAddress = 0x42;
int received = 0;
int main() {
    
    
    #ifdef SLAVE
    slave.address(slaveAddress);
#endif
    lcd.cls();
    lcd.printf("hello world");
    
    
    
while (1) {
    while(!JS){
r=pot.read();
    g=pot1.read();
    }
    while(JS){
for (int i=0;i<=29;i++) {
buzzer.period(1/(2*frequency[i])); // set PWM period
buzzer=0.5; // set duty cycle
wait(0.7*beat[i]); // hold for beat period

    
     lcd.cls();
    lcd.locate(0,3);
    lcd.printf("Jesus Loves Me");
      r=pot.read();
    g=pot1.read();
}
  while(!JS){
r=pot.read();
    g=pot1.read();
    }
}
/////color change

    
    
#ifdef MASTER
            if(up)
            {//master sends its song to slave
                i2cPort.start();
                //send address
                i2cPort.write(slaveAddress);
                //send tempo (BPM)
                //loop through song
                i2cPort.write(6);
//                for(char j=0;j<10;j++)
//                {
//                    //send frequency
//                    i2cPort.write(j);
//                    //play note
//                    //wait length
//                    wait(1);
//                }
//                //send stop code
//                i2cPort.write(0xFF);
                //close i2c port
                i2cPort.stop();
            }
            else if(down)
            {//slave should send it's song to master
                
            }
#endif
#ifdef SLAVE
            received = slave.receive();
            if(I2CSlave::ReadAddressed==received){//master is requesting slave song
                
            }
            else if(I2CSlave::WriteAddressed==received){//master is sending its song
                received = slave.read();
                lcd.cls();
                lcd.locate(0,0);
                lcd.printf("Sent: %d",received);
            }
#endif
        }
    }
    
    
    







    /* while(1){
    r=pot.read();
    g=pot1.read();
    }*/