A recreation of the original, out of the packet demo code that runs on the FRDM-KL25Z board, using pre-existing libraries on mbed. The board will alter its RGB LED colours based on accelerometer data, and the capacitance slider will alter the brightness. * Fully Commented *

Dependencies:   MMA8451Q TSI mbed

Fork of FRDM_TSI by mbed official

Files at this revision

API Documentation at this revision

Comitter:
B50132
Date:
Tue Aug 19 15:07:39 2014 +0000
Parent:
5:b44f5f02b879
Commit message:
First revision. Please read description.

Changed in this revision

MMA8451Q.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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8451Q.lib	Tue Aug 19 15:07:39 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/B50132/code/MMA8451Q/#204db61f9c8f
--- a/main.cpp	Thu May 09 09:26:46 2013 +0000
+++ b/main.cpp	Tue Aug 19 15:07:39 2014 +0000
@@ -1,12 +1,76 @@
 #include "mbed.h"
 #include "TSISensor.h"
+#include "MMA8451Q.h"
+
+#if   defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
+  PinName const SDA = PTE25;
+  PinName const SCL = PTE24;
+#elif defined (TARGET_KL05Z)
+  PinName const SDA = PTB4;
+  PinName const SCL = PTB3;
+#elif defined (TARGET_K20D50M)
+  PinName const SDA = PTB1;
+  PinName const SCL = PTB0;
+#else
+  #error TARGET NOT DEFINED
+#endif
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
 
 int main(void) {
-    PwmOut led(LED_GREEN);
+    
+    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+        
+    PwmOut RED(LED1);   //Declare RGB LEDs
+    PwmOut GREEN(LED2);
+    PwmOut BLUE(LED3);
+    
     TSISensor tsi;
     
-    while (true) {
-        led = 1.0 - tsi.readPercentage();
+    float just_logged = 0.9; //variable for holding new slider value
+    float last_logged = 0.9; //variable for holding previous value
+    
+    while (true) {    //accelerometer will be read on every iteration,
+                      //regardless of slider value.
+    
+       if (tsi.readPercentage()) //is slider being touched?
+        {
+            just_logged = tsi.readPercentage(); //store new value
+            
+            if (tsi.readPercentage()>0.9) //don't allow value near 1, as LEDs will go off
+            {
+                just_logged = 0.9;
+            }
+            
+        //UPDATE THE LEDS WITH ACCEL. DATA AND NEW SLIDE DATA    
+        RED = 1.0 - ((abs(acc.getAccX())) - (just_logged));
+        RED = (RED > 0.99) ? 0.99 : RED; //here we are ensuring the LEDs never go fully off.
+                                         //this could of course be changed to allow them to
+        
+        GREEN = 1.0 - ((abs(acc.getAccY())) - (just_logged));
+        GREEN = (GREEN > 0.99) ? 0.99 : GREEN;
+        
+        BLUE = 1.0 - ((abs(acc.getAccZ())) - (just_logged));
+        BLUE = (BLUE > 0.99) ? 0.99 : BLUE;
+        
+        last_logged = tsi.readPercentage();
+        
         wait(0.1);
+        }
+        
+        else // no new slider data, therefore use previous
+        {
+        RED = 1.0 - ((abs(acc.getAccX())) - (last_logged));
+        RED = (RED > 0.99) ? 0.99 : RED;
+        
+        GREEN = 1.0 - ((abs(acc.getAccY())) - (last_logged));
+        GREEN = (GREEN > 0.99) ? 0.99 : GREEN;
+        
+        BLUE = 1.0 - ((abs(acc.getAccZ())) - (last_logged));
+        BLUE = (BLUE > 0.99) ? 0.99 : BLUE;
+        
+        wait(0.1);
+        }
+        
     }
 }
\ No newline at end of file
--- a/mbed.bld	Thu May 09 09:26:46 2013 +0000
+++ b/mbed.bld	Tue Aug 19 15:07:39 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/b3110cd2dd17
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013
\ No newline at end of file