small program that prints the acceleration on 3 axes and slider value and plays with the RGB led in the same time.

Dependencies:   mbed tsi_sensor MMA8451Q

Committer:
diaady
Date:
Tue Jan 08 14:01:52 2019 +0000
Revision:
10:8ef196baf733
Parent:
9:d4bffe27a7bf
filter acc , print on separate lines; add slider

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 2:41db78380a6e 1 #include "mbed.h"
chris 2:41db78380a6e 2 #include "MMA8451Q.h"
diaady 10:8ef196baf733 3 #include "tsi_sensor.h"
chris 2:41db78380a6e 4
sam_grove 8:d797bfa9f76e 5 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
sam_grove 8:d797bfa9f76e 6 PinName const SDA = PTE25;
sam_grove 8:d797bfa9f76e 7 PinName const SCL = PTE24;
sam_grove 8:d797bfa9f76e 8 #elif defined (TARGET_KL05Z)
sam_grove 8:d797bfa9f76e 9 PinName const SDA = PTB4;
sam_grove 8:d797bfa9f76e 10 PinName const SCL = PTB3;
sam_grove 9:d4bffe27a7bf 11 #elif defined (TARGET_K20D50M)
sam_grove 9:d4bffe27a7bf 12 PinName const SDA = PTB1;
sam_grove 9:d4bffe27a7bf 13 PinName const SCL = PTB0;
sam_grove 8:d797bfa9f76e 14 #else
sam_grove 8:d797bfa9f76e 15 #error TARGET NOT DEFINED
sam_grove 8:d797bfa9f76e 16 #endif
sam_grove 8:d797bfa9f76e 17
diaady 10:8ef196baf733 18
diaady 10:8ef196baf733 19
chris 2:41db78380a6e 20 #define MMA8451_I2C_ADDRESS (0x1d<<1)
chris 2:41db78380a6e 21
diaady 10:8ef196baf733 22
diaady 10:8ef196baf733 23
diaady 10:8ef196baf733 24 /* This defines will be replaced by PinNames soon */
diaady 10:8ef196baf733 25 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
diaady 10:8ef196baf733 26 #define ELEC0 9
diaady 10:8ef196baf733 27 #define ELEC1 10
diaady 10:8ef196baf733 28 #elif defined (TARGET_KL05Z)
diaady 10:8ef196baf733 29 #define ELEC0 9
diaady 10:8ef196baf733 30 #define ELEC1 8
diaady 10:8ef196baf733 31 #else
diaady 10:8ef196baf733 32 #error TARGET NOT DEFINED
diaady 10:8ef196baf733 33 #endif
diaady 10:8ef196baf733 34
sam_grove 9:d4bffe27a7bf 35 int main(void)
sam_grove 9:d4bffe27a7bf 36 {
sam_grove 8:d797bfa9f76e 37 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
diaady 10:8ef196baf733 38 TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
sam_grove 8:d797bfa9f76e 39 PwmOut rled(LED1);
sam_grove 8:d797bfa9f76e 40 PwmOut gled(LED2);
sam_grove 8:d797bfa9f76e 41 PwmOut bled(LED3);
sam_grove 9:d4bffe27a7bf 42
sam_grove 8:d797bfa9f76e 43 printf("MMA8451 ID: %d\n", acc.getWhoAmI());
chris 4:367de1084ea9 44
emilmont 5:bf5becf7469c 45 while (true) {
diaady 10:8ef196baf733 46 float x, y, z, slide;
sam_grove 9:d4bffe27a7bf 47 x = abs(acc.getAccX());
sam_grove 9:d4bffe27a7bf 48 y = abs(acc.getAccY());
sam_grove 9:d4bffe27a7bf 49 z = abs(acc.getAccZ());
diaady 10:8ef196baf733 50 slide = tsi.readPercentage();
sam_grove 9:d4bffe27a7bf 51 rled = 1.0f - x;
sam_grove 9:d4bffe27a7bf 52 gled = 1.0f - y;
sam_grove 9:d4bffe27a7bf 53 bled = 1.0f - z;
sam_grove 9:d4bffe27a7bf 54 wait(0.1f);
diaady 10:8ef196baf733 55 if(x > 0.1f) {printf("X: %1.2f\n",x);}
diaady 10:8ef196baf733 56 if(y > 0.1f) {printf("Y: %1.2f\n ",y);}
diaady 10:8ef196baf733 57 if(z > 0.1f) {printf("Z: %1.2f\n",z);}
diaady 10:8ef196baf733 58 if(slide > 0) {printf("Slide: %1.2f\n",slide);}
diaady 10:8ef196baf733 59
diaady 10:8ef196baf733 60 //printf("X: %1.2f, Y: %1.2f, Z: %1.2f Slider: %1.2f\n", x, y, z,tsi.readPercentage());
chris 2:41db78380a6e 61 }
chris 2:41db78380a6e 62 }