suso es ackfldhjhsjgshkñlahbndgvjbnPUTOvjkfcbkljfwaubjiouorituiofubguwv

Dependencies:   mbed MMA8451Q

Committer:
GabiLopez
Date:
Tue Apr 16 12:24:08 2019 +0000
Revision:
0:5e6207055faf
putana

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GabiLopez 0:5e6207055faf 1 #include "mbed.h"
GabiLopez 0:5e6207055faf 2 #include "MMA8451Q.h"
GabiLopez 0:5e6207055faf 3 #include "math.h"
GabiLopez 0:5e6207055faf 4 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
GabiLopez 0:5e6207055faf 5 PinName const SDA = PTE25;
GabiLopez 0:5e6207055faf 6 PinName const SCL = PTE24;
GabiLopez 0:5e6207055faf 7 #elif defined (TARGET_KL05Z)
GabiLopez 0:5e6207055faf 8 PinName const SDA = PTB4;
GabiLopez 0:5e6207055faf 9 PinName const SCL = PTB3;
GabiLopez 0:5e6207055faf 10 #elif defined (TARGET_K20D50M)
GabiLopez 0:5e6207055faf 11 PinName const SDA = PTB1;
GabiLopez 0:5e6207055faf 12 PinName const SCL = PTB0;
GabiLopez 0:5e6207055faf 13 #else
GabiLopez 0:5e6207055faf 14 #error TARGET NOT DEFINED
GabiLopez 0:5e6207055faf 15 #endif
GabiLopez 0:5e6207055faf 16
GabiLopez 0:5e6207055faf 17 #define MMA8451_I2C_ADDRESS (0x1d<<1)
GabiLopez 0:5e6207055faf 18 float x, y, z,horl;
GabiLopez 0:5e6207055faf 19 Ticker timer1;
GabiLopez 0:5e6207055faf 20 void imprimo();
GabiLopez 0:5e6207055faf 21
GabiLopez 0:5e6207055faf 22 int main(void)
GabiLopez 0:5e6207055faf 23 {
GabiLopez 0:5e6207055faf 24 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
GabiLopez 0:5e6207055faf 25 PwmOut rled(LED1);
GabiLopez 0:5e6207055faf 26 PwmOut gled(LED2);
GabiLopez 0:5e6207055faf 27 PwmOut bled(LED3);
GabiLopez 0:5e6207055faf 28 timer1.attach(&imprimo, 1);
GabiLopez 0:5e6207055faf 29 printf("MMA8451 ID: %d\n", acc.getWhoAmI());
GabiLopez 0:5e6207055faf 30
GabiLopez 0:5e6207055faf 31 while (true) {
GabiLopez 0:5e6207055faf 32 x = abs(acc.getAccX());
GabiLopez 0:5e6207055faf 33 y = abs(acc.getAccY());
GabiLopez 0:5e6207055faf 34 z = abs(acc.getAccZ());
GabiLopez 0:5e6207055faf 35 rled = 1.0f - x;
GabiLopez 0:5e6207055faf 36 gled = 1.0f - y;
GabiLopez 0:5e6207055faf 37 bled = 1.0f - z;
GabiLopez 0:5e6207055faf 38 wait(0.1f);
GabiLopez 0:5e6207055faf 39
GabiLopez 0:5e6207055faf 40 }
GabiLopez 0:5e6207055faf 41 }
GabiLopez 0:5e6207055faf 42 void imprimo ()
GabiLopez 0:5e6207055faf 43 {
GabiLopez 0:5e6207055faf 44 printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z);
GabiLopez 0:5e6207055faf 45 horl=atan(x/sqrt(pow(y,2)+pow(z,2)))*60.28;
GabiLopez 0:5e6207055faf 46 //horl=atan(-x/z);
GabiLopez 0:5e6207055faf 47 printf("%f\n",horl);
GabiLopez 0:5e6207055faf 48 }