Playing around with accelerometer and magnetometer on mbed KL46Z

Dependencies:   MAG3110 MMA8451Q PinDetect mbed TSI

Files at this revision

API Documentation at this revision

Comitter:
mohammmo
Date:
Mon Feb 03 00:19:59 2014 +0000
Parent:
1:22fcc3ce3ede
Child:
3:552b7c450b2f
Child:
5:4ccda4b4f345
Commit message:
add light and touch sensors

Changed in this revision

TSI.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TSI.lib	Mon Feb 03 00:19:59 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/vsluiter/code/TSI/#4dc2f5a3a731
--- a/main.cpp	Sun Feb 02 22:08:15 2014 +0000
+++ b/main.cpp	Mon Feb 03 00:19:59 2014 +0000
@@ -2,6 +2,7 @@
 #include "PinDetect.h"
 #include "MMA8451Q.h"
 #include "MAG3110.h"
+#include "TSISensor.h"
 
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
 
@@ -15,6 +16,8 @@
 // Declare timer interrupt
 Ticker timerAcc;
 Ticker timerMag;
+Ticker timerLight; 
+Ticker timerTouch;
 
 // Declare pointer variables
 float xAcc;
@@ -23,22 +26,32 @@
 int xMag;
 int yMag;
 int zMag;
+float xLight; 
+float xTouch; 
 
 // Declare Accelerometer pins and I2C address
 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, 0, 0);
 // Declare Magnetometer pins
 MAG3110 mag(PTE25, PTE24);
+// Declare touch sensor pin
+TSISensor touch;
+// Declare light sensor pin
+AnalogIn light(PTE22);
 
 // Functions
 void init();
 void accTime();
 void magTime();
+void lightTime();
+void touchTime();
 
 void init()
 {
     // Attach timerAcc
     timerAcc.attach(&accTime, 0.5);
     timerMag.attach(&magTime, 0.75);
+    timerLight.attach(&lightTime, 0.5);
+    timerTouch.attach(&touchTime, 0.5);
     ledred = 0; 
     ledgreen = 0;   
 }
@@ -51,11 +64,15 @@
     while(1)
     {
         // Read and print data from accelerometer
-        pc.puts("Accelerometer Data:\r\n");
-        pc.printf("X: %f, Y: %f, Z: %f\r\n", xAcc, yAcc, zAcc);
+        pc.puts("Accelerometer: ");
+        pc.printf("X: %f, Y: %f, Z: %f\t", xAcc, yAcc, zAcc);
         // Read data from magnetometer
-        pc.puts("Magnetometer Data:\r\n");
-        pc.printf("X: %d, Y: %d, Z: %d\r\n", xMag, yMag, zMag);
+        pc.puts("Magnetometer: ");
+        pc.printf("X: %d, Y: %d, Z: %d\t", xMag, yMag, zMag);
+        pc.puts("Light: ");
+        pc.printf(" %f\t", xLight);
+        pc.puts("touch: ");
+        pc.printf(" %f\r\n", xTouch);
         wait(0.5);
     }
 }
@@ -74,4 +91,14 @@
     yMag = mag.getYVal();
     zMag = mag.getZVal();
     ledred = !ledred;
+}
+
+void lightTime()
+{
+    xLight = 1 - light.read();
+}
+
+void touchTime()
+{
+    xTouch = 1 - touch.readPercentage();
 }
\ No newline at end of file