Compass Working

Dependencies:   mbed

Revision:
2:ef6778c2438e
Parent:
1:ef74b3428716
Child:
4:92024cb66c9c
--- a/Tap.cpp	Wed Jan 13 22:31:54 2016 +0000
+++ b/Tap.cpp	Thu Jan 14 02:04:19 2016 +0000
@@ -1,84 +1,76 @@
 #include "Tap.h"
-
 I2C acc(p9,p10);
-DigitalIn interrupt(p20);
+DigitalIn int2(p20);
+DigitalOut check(p12);
 BusOut led(LED1,LED2,LED3,LED4);
-
-
+char acc_config[2];
+const int addr=(0x53<<1);
+char data1[1];
+char ack;
 
-char Tap() 
-{
-    
-    
-    
-    cs=0;
-    acc.write(0x31); // data format
-    acc.write(0x0B);  
-    cs=1;
-    
-    cs=0;
-    acc.write(0x2D);  // power control
-    acc.write(0x08);  // measure mode
-    cs=1;
-    
-    cs=0;
-    acc.write(0x1D); // tap threshold
-    acc.write(0x50);  // set to 5g 
-    cs=1;
+float Tap() 
+    {
+    acc.frequency(100000);   
+    acc_config[0]=0x31; acc_config[1]=0x0B;         //data format
+    ack = acc.write(addr,acc_config, 2);
+    wait(0.1);
+      
+    acc_config[0]=0x2D; acc_config[1]=0x08;         //power control->measure mode
+    acc.write(addr,acc_config, 2);   
+    wait(0.1);
     
-    cs=0;
-    acc.write(0x21); // tap duration
-    acc.write(0x10);  // set to 0.01
-    cs=1;
+    acc_config[0]=0x1D; acc_config[1]=0x50;         //Tap Threshold ->5g 
+    acc.write(addr,acc_config, 2); 
+    wait(0.1);
     
-    cs=0;
-    acc.write(0x22); // double tap gap
-    acc.write(0x05);  // set to aprox .006
-    cs=1;
+    acc_config[0]=0x21; acc_config[1]=0x10;         //Tap duration -> 0.01
+    acc.write(addr,acc_config, 2);    
+    wait(0.1);
     
-    cs=0;
-    acc.write(0x23); // double tap window
-    acc.write(0xFF);  // Set to .38175
-    cs=1;
+    acc_config[0]=0x22; acc_config[1]=0x05;         //Tap Gap ~0.006
+    acc.write(addr,acc_config, 2); 
+    wait(0.1);
     
-    cs=0;
-    acc.write(0x2A); // tap axis
-    acc.write(0x07);  // set to enable x,y, and z (00000111) 
-    cs=1;
+    acc_config[0]=0x23; acc_config[1]=0xFF;         //Tap window ->~0.38175
+    acc.write(addr,acc_config, 2);
+    wait(0.1);
+    
+    acc_config[0]=0x2A; acc_config[1]=0x07;         //Axis -> x&y&z
+    acc.write(addr,acc_config, 2);
+    wait(0.1);
     
-    cs=0;
-    acc.write(0x2E); // interupt enable
-    acc.write(0x60);  // set to single and double tap (01100000)
-    cs=1;
+    acc_config[0]=0x2E; acc_config[1]=0x60;         //enable interupt -> single and double tap
+    acc.write(addr,acc_config, 2);
+    wait(0.1);
     
-    cs=0;
-    acc.write(0x2F); // map interupt
-    acc.write(0x60);  // set to 00000111
-    cs=1;
-    
-    char Inbyte;
+    acc_config[0]=0x2F; acc_config[1]=0x60;         //map interupt ->int2
+    acc.write(addr,acc_config, 2);
+    wait(0.1);
     
     while(1)
-    {
+        {
         wait(0.01);
-        Inbyte =0;
         
-        if(interrupt==1)  // Check for interupt signal
+        if(int2.read()==1)  // Check for interupt signal
         {
-            cs=0;
-            acc.write(0x80|0x30);  // Connect to device
-            Inbyte = acc.write(0x00);  // Get interupt information
-            if(Inbyte & 0x40)  // Turn on one LED for one tap
+           
+            acc_config[0]=0x30;
+            acc.write(addr,acc_config,1);             
+            acc.read(addr,data1,1);  // Get interupt information
+            
+            if(data1[0] & 0x40)  // Turn on one LED for one tap
             {
-                led =1;
-                return 1;  
+                led =1; 
             }
-            if(Inbyte & 0x20)  // Turn on two LEDs for two taps
+            if(data1[0] & 0x20)  // Turn on two LEDs for two taps
             {
-                led =3;
-                return 2;
+                led =2;
             }
-            led =0;  // Turn off LED
+            wait(1);
+             // Turn off LED
+             if(led==1){return 1;}
+             if(led ==2){return 2;}
+            
         }     
     }   
 }
\ No newline at end of file