Dependencies:   mbed

main.cpp

Committer:
MPybus
Date:
2013-05-02
Revision:
0:2b7fd84b9e13
Child:
1:331861c90192

File content as of revision 0:2b7fd84b9e13:

#include "mbed.h"              
 
Serial pc(USBTX, USBRX); // tx, rx
AnalogIn sound(PTB3);     //Input from microphone
DigitalOut micpower(PTA13);

 
int main()
{
    PwmOut bled(LED_BLUE);
  
    signed int a;         //this is to be the input audio
    signed int maxa;
    signed int diff;
    signed int holdmax=10000;
    signed int lastfive[5];
    signed int avrg;
    unsigned char flag0=0x00;
    unsigned char flag1=0x00;
    unsigned char flag2=0x00;
    unsigned char flag3=0x00;
    unsigned char flag4=0x00;
    unsigned char typingflag=0x00;
    micpower=1;
           
    pc.printf("Listening\n"); 
    
    lastfive[4]=300;
    lastfive[3]=300;
    lastfive[2]=300;
    lastfive[1]=300;
    lastfive[0]=300;      
    
    
    while(1)
    {  
        maxa=0;
      for(int x=0; x<10000; x++)
        {
            a=((sound)*2000);
            a=a*a*a;
            a=(a/260000)-3500;
                        
            if(a>maxa)
            {
            maxa=a;
            }
        }
        
              
       diff=maxa-holdmax; 
            
       lastfive[4]=lastfive[3];
       lastfive[3]=lastfive[2];
       lastfive[2]=lastfive[1];
       lastfive[1]=lastfive[0];
       lastfive[0]=holdmax;
  
       holdmax=maxa;  
  
       pc.printf("%d ",diff );       //print what is recorded to the PC to make it clear what's going on.
       pc.printf("%d ",maxa );
       
   avrg=(lastfive[0]+lastfive[1]+lastfive[2]+lastfive[3]+lastfive[4])/5;
   
   if((flag0+flag1+flag2+flag3+flag4)<3)
    {
      if(maxa>(avrg+40))
        {
            typingflag=0x01;
        }
      else
        {
            typingflag=0x00;
        }
    }    
   else
    {
       if(maxa>(avrg-60))
        {
            typingflag=0x01;
        }
       else
        {
            typingflag=0x00;
        }
    }
   
        flag4=flag3;
        flag3=flag2;
        flag2=flag1;
        flag1=flag0;
        flag0=typingflag;
        
        if(typingflag==0x01)
            {
            bled=0.5;
            }
        else
            {
            bled=1;
            }
   

}
}