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

Committer:
B50132
Date:
Tue Aug 19 15:07:39 2014 +0000
Revision:
6:8613f791ee14
Parent:
1:51b1b688179a
First revision. Please read description.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 0:0f00f07ebde0 1 #include "mbed.h"
chris 1:51b1b688179a 2 #include "TSISensor.h"
B50132 6:8613f791ee14 3 #include "MMA8451Q.h"
B50132 6:8613f791ee14 4
B50132 6:8613f791ee14 5 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
B50132 6:8613f791ee14 6 PinName const SDA = PTE25;
B50132 6:8613f791ee14 7 PinName const SCL = PTE24;
B50132 6:8613f791ee14 8 #elif defined (TARGET_KL05Z)
B50132 6:8613f791ee14 9 PinName const SDA = PTB4;
B50132 6:8613f791ee14 10 PinName const SCL = PTB3;
B50132 6:8613f791ee14 11 #elif defined (TARGET_K20D50M)
B50132 6:8613f791ee14 12 PinName const SDA = PTB1;
B50132 6:8613f791ee14 13 PinName const SCL = PTB0;
B50132 6:8613f791ee14 14 #else
B50132 6:8613f791ee14 15 #error TARGET NOT DEFINED
B50132 6:8613f791ee14 16 #endif
B50132 6:8613f791ee14 17
B50132 6:8613f791ee14 18 #define MMA8451_I2C_ADDRESS (0x1d<<1)
emilmont 0:0f00f07ebde0 19
emilmont 0:0f00f07ebde0 20 int main(void) {
B50132 6:8613f791ee14 21
B50132 6:8613f791ee14 22 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
B50132 6:8613f791ee14 23
B50132 6:8613f791ee14 24 PwmOut RED(LED1); //Declare RGB LEDs
B50132 6:8613f791ee14 25 PwmOut GREEN(LED2);
B50132 6:8613f791ee14 26 PwmOut BLUE(LED3);
B50132 6:8613f791ee14 27
chris 1:51b1b688179a 28 TSISensor tsi;
emilmont 0:0f00f07ebde0 29
B50132 6:8613f791ee14 30 float just_logged = 0.9; //variable for holding new slider value
B50132 6:8613f791ee14 31 float last_logged = 0.9; //variable for holding previous value
B50132 6:8613f791ee14 32
B50132 6:8613f791ee14 33 while (true) { //accelerometer will be read on every iteration,
B50132 6:8613f791ee14 34 //regardless of slider value.
B50132 6:8613f791ee14 35
B50132 6:8613f791ee14 36 if (tsi.readPercentage()) //is slider being touched?
B50132 6:8613f791ee14 37 {
B50132 6:8613f791ee14 38 just_logged = tsi.readPercentage(); //store new value
B50132 6:8613f791ee14 39
B50132 6:8613f791ee14 40 if (tsi.readPercentage()>0.9) //don't allow value near 1, as LEDs will go off
B50132 6:8613f791ee14 41 {
B50132 6:8613f791ee14 42 just_logged = 0.9;
B50132 6:8613f791ee14 43 }
B50132 6:8613f791ee14 44
B50132 6:8613f791ee14 45 //UPDATE THE LEDS WITH ACCEL. DATA AND NEW SLIDE DATA
B50132 6:8613f791ee14 46 RED = 1.0 - ((abs(acc.getAccX())) - (just_logged));
B50132 6:8613f791ee14 47 RED = (RED > 0.99) ? 0.99 : RED; //here we are ensuring the LEDs never go fully off.
B50132 6:8613f791ee14 48 //this could of course be changed to allow them to
B50132 6:8613f791ee14 49
B50132 6:8613f791ee14 50 GREEN = 1.0 - ((abs(acc.getAccY())) - (just_logged));
B50132 6:8613f791ee14 51 GREEN = (GREEN > 0.99) ? 0.99 : GREEN;
B50132 6:8613f791ee14 52
B50132 6:8613f791ee14 53 BLUE = 1.0 - ((abs(acc.getAccZ())) - (just_logged));
B50132 6:8613f791ee14 54 BLUE = (BLUE > 0.99) ? 0.99 : BLUE;
B50132 6:8613f791ee14 55
B50132 6:8613f791ee14 56 last_logged = tsi.readPercentage();
B50132 6:8613f791ee14 57
chris 1:51b1b688179a 58 wait(0.1);
B50132 6:8613f791ee14 59 }
B50132 6:8613f791ee14 60
B50132 6:8613f791ee14 61 else // no new slider data, therefore use previous
B50132 6:8613f791ee14 62 {
B50132 6:8613f791ee14 63 RED = 1.0 - ((abs(acc.getAccX())) - (last_logged));
B50132 6:8613f791ee14 64 RED = (RED > 0.99) ? 0.99 : RED;
B50132 6:8613f791ee14 65
B50132 6:8613f791ee14 66 GREEN = 1.0 - ((abs(acc.getAccY())) - (last_logged));
B50132 6:8613f791ee14 67 GREEN = (GREEN > 0.99) ? 0.99 : GREEN;
B50132 6:8613f791ee14 68
B50132 6:8613f791ee14 69 BLUE = 1.0 - ((abs(acc.getAccZ())) - (last_logged));
B50132 6:8613f791ee14 70 BLUE = (BLUE > 0.99) ? 0.99 : BLUE;
B50132 6:8613f791ee14 71
B50132 6:8613f791ee14 72 wait(0.1);
B50132 6:8613f791ee14 73 }
B50132 6:8613f791ee14 74
emilmont 0:0f00f07ebde0 75 }
chris 1:51b1b688179a 76 }