Keyboard listening program. Detects extra vibrations.

Dependencies:   mbed

Revision:
1:331861c90192
Parent:
0:2b7fd84b9e13
--- a/main.cpp	Thu May 02 16:00:22 2013 +0000
+++ b/main.cpp	Fri May 03 11:14:52 2013 +0000
@@ -1,35 +1,36 @@
 #include "mbed.h"              
  
 Serial pc(USBTX, USBRX); // tx, rx
-AnalogIn sound(PTB3);     //Input from microphone
-DigitalOut micpower(PTA13);
+AnalogIn sound(PTB3);     //Input coming from microphone
+DigitalOut micpower(PTA13); //Power for the microphone
+Serial wire(PTD3,PTD2);
 
  
 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;
+    signed int a;         //this is to be the input audio.
+    signed int maxa;        //this is the maximum recorded input over a period of time.
+    signed int diff;            //this is how much the value of maxa has changed since the last sampling.
+    signed int holdmax=10000;   //this is the previous value of maxa;
+    signed int lastfive[5];     //this array holds the previous five values of maxa
+    signed int avrg;                //this is to be an average of the values in the lastfive array.
+    unsigned char flag0=0x00;       
+    unsigned char flag1=0x00;   
+    unsigned char flag2=0x00;       //these flags show whether the user was typing for the last five samples.
     unsigned char flag3=0x00;
     unsigned char flag4=0x00;
-    unsigned char typingflag=0x00;
+    char typingflag=0;      //this flag shows whether the user is typing currently.
     micpower=1;
            
     pc.printf("Listening\n"); 
     
-    lastfive[4]=300;
-    lastfive[3]=300;
-    lastfive[2]=300;
-    lastfive[1]=300;
-    lastfive[0]=300;      
+    lastfive[4]=350;            //these values are to ensure that the average is too high for the program to
+    lastfive[3]=350;            //think it is typing to begin with.
+    lastfive[2]=350;
+    lastfive[1]=350;
+    lastfive[0]=350;      
     
     
     while(1)
@@ -39,11 +40,11 @@
         {
             a=((sound)*2000);
             a=a*a*a;
-            a=(a/260000)-3500;
+            a=(a/260000)-3500;          //these lines produce a useful value of a indicating how much the microphone is picking up on.
                         
             if(a>maxa)
             {
-            maxa=a;
+            maxa=a;                 //this selects the highest output over the course of a sample.
             }
         }
         
@@ -56,51 +57,51 @@
        lastfive[1]=lastfive[0];
        lastfive[0]=holdmax;
   
-       holdmax=maxa;  
+       holdmax=maxa;                //this updates the lastfive array and the holdmax variable.
   
        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;
+   avrg=(lastfive[0]+lastfive[1]+lastfive[2]+lastfive[3]+lastfive[4])/5;        //find the average of the lastfive array.
    
-   if((flag0+flag1+flag2+flag3+flag4)<3)
+   if((flag0+flag1+flag2+flag3+flag4)<3)            //if the user hasn't been typing recently.
     {
-      if(maxa>(avrg+40))
+      if(maxa>(avrg+30))
         {
-            typingflag=0x01;
+            typingflag=1;            //set typingflag on
         }
-      else
+      else 
         {
-            typingflag=0x00;
+            typingflag=0;        //set typingflag off
         }
     }    
-   else
+   else                 //if they haven't been typing recently
     {
-       if(maxa>(avrg-60))
+       if(maxa>(avrg-40))
         {
-            typingflag=0x01;
+            typingflag=1;
         }
        else
         {
-            typingflag=0x00;
+            typingflag=0;
         }
     }
    
-        flag4=flag3;
+        flag4=flag3;                //update various flags
         flag3=flag2;
         flag2=flag1;
         flag1=flag0;
         flag0=typingflag;
         
-        if(typingflag==0x01)
+        if(typingflag==1jk,u)
             {
             bled=0.5;
             }
         else
             {
-            bled=1;
+            bled=1;                 //have the blue led show whether typing is being picked up.
             }
    
-
-}
+        wire.putc(typingflag);
+}           //end of while loop.
 }
\ No newline at end of file