Playing around with accelerometer and magnetometer on mbed KL46Z

Dependencies:   MAG3110 MMA8451Q PinDetect mbed TSI

Revision:
15:90d4b8ddc586
Parent:
12:d9d577c25961
Child:
16:8755d6de3b01
--- a/main.cpp	Fri Feb 07 09:46:25 2014 +0000
+++ b/main.cpp	Mon Feb 10 23:05:04 2014 +0000
@@ -18,6 +18,7 @@
 Ticker timerMag;
 Ticker timerLight; 
 Ticker timerTouch;
+Ticker timerADC;
 
 // Declare pointer variables
 float xAcc;
@@ -28,18 +29,21 @@
 int zMag;
 float xLight; 
 float xTouch; 
+float xADC;
 
 // Sampling rates
 float accRate = 0.1;
 float magRate = 0.1;
 float lightRate = 0.1;
 float touchRate = 0.1;
+float adcRate = 0.1;
 
 // Enables
 int accEn = 1;
 int magEn = 1;
 int lightEn = 1;
 int touchEn = 1;
+int adcEn = 1;
 
 // Receiving Data
 const int bufferSize = 255;
@@ -57,6 +61,8 @@
 TSISensor touch;
 // Declare light sensor pin
 AnalogIn light(PTE22);
+// Declare analog input pin
+AnalogIn adc(PTB0);
 
 // Functions
 void init();
@@ -67,6 +73,7 @@
 void magTime();
 void lightTime();
 void touchTime();
+void adcTime();
 
 void init()
 {
@@ -76,6 +83,7 @@
     timerMag.attach(&magTime, magRate);
     timerLight.attach(&lightTime, lightRate);
     timerTouch.attach(&touchTime, touchRate);
+    timerADC.attach(&adcTime, adcRate);
     pc.attach(&receiveHandler, Serial::RxIrq);
     ledred = 0; 
     ledgreen = 0;   
@@ -103,10 +111,10 @@
 
 void printData()
 {
-    pc.printf("/%f/%f/%f/%d/%d/%d/%f/%f/%.3f/%.3f/%.3f/%.3f/%d/%d/%d/%d/\r\n", 
-        xAcc, yAcc, zAcc, xMag, yMag, zMag, xLight, xTouch, 
-        accRate, magRate, lightRate, touchRate, 
-        accEn, magEn, lightEn, touchEn);
+    pc.printf("/%f/%f/%f/%d/%d/%d/%f/%f/%f/%.3f/%.3f/%.3f/%.3f/%.3f/%d/%d/%d/%d/%d/\r\n", 
+        xAcc, yAcc, zAcc, xMag, yMag, zMag, xLight, xTouch, xADC, 
+        accRate, magRate, lightRate, touchRate, adcRate, 
+        accEn, magEn, lightEn, touchEn, adcEn);
         
 }  
 
@@ -173,6 +181,10 @@
                 timerTouch.detach();
                 touchEn = 0;
                 break;
+            case '5':
+                timerADC.detach();
+                adcEn = 0;
+                break;
             default:
                 //pc.printf("incorrect input\r\n");          
                 break;
@@ -199,6 +211,10 @@
                 timerTouch.attach(&touchTime, touchRate);
                 touchEn = 1;
                 break;
+            case '5':
+                timerADC.attach(&adcTime, adcRate);
+                adcEn = 1;
+                break;
             default:
                 //pc.printf("incorrect input\r\n");          
                 break;
@@ -229,6 +245,10 @@
                 timerTouch.detach();
                 timerTouch.attach(&touchTime, touchRate);
                 break;
+            case '5':
+                adcRate = temp;
+                timerADC.detach();
+                timerADC.attach(&adcTime, adcRate);
             default:
                 //pc.printf("incorrect input\r\n");          
                 break;
@@ -265,3 +285,8 @@
 {
     xTouch = 1 - touch.readPercentage();
 }
+
+void adcTime()
+{
+    xADC = adc.read();
+}
\ No newline at end of file