Pitch code

Dependencies:   MMA8451Q mbed

Committer:
maruchik
Date:
Mon Sep 22 07:12:23 2014 +0000
Revision:
0:2418caa8b2a4
Pitch Code;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maruchik 0:2418caa8b2a4 1 #include "mbed.h"
maruchik 0:2418caa8b2a4 2 #include "MMA8451Q.h"
maruchik 0:2418caa8b2a4 3
maruchik 0:2418caa8b2a4 4 Serial pc(USBTX, USBRX);
maruchik 0:2418caa8b2a4 5 float pitch, roll;
maruchik 0:2418caa8b2a4 6 int enter, back;
maruchik 0:2418caa8b2a4 7 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
maruchik 0:2418caa8b2a4 8 PinName const SDA = PTE25;
maruchik 0:2418caa8b2a4 9 PinName const SCL = PTE24;
maruchik 0:2418caa8b2a4 10 #elif defined (TARGET_KL05Z)
maruchik 0:2418caa8b2a4 11 PinName const SDA = PTB4;
maruchik 0:2418caa8b2a4 12 PinName const SCL = PTB3;
maruchik 0:2418caa8b2a4 13 #elif defined (TARGET_K20D50M)
maruchik 0:2418caa8b2a4 14 PinName const SDA = PTB1;
maruchik 0:2418caa8b2a4 15 PinName const SCL = PTB0;
maruchik 0:2418caa8b2a4 16 #else
maruchik 0:2418caa8b2a4 17 #error TARGET NOT DEFINED
maruchik 0:2418caa8b2a4 18 #endif
maruchik 0:2418caa8b2a4 19
maruchik 0:2418caa8b2a4 20 #define MMA8451_I2C_ADDRESS (0x1d<<1)
maruchik 0:2418caa8b2a4 21
maruchik 0:2418caa8b2a4 22 DigitalIn RIGHT(D8);
maruchik 0:2418caa8b2a4 23 DigitalIn LEFT(D9);
maruchik 0:2418caa8b2a4 24
maruchik 0:2418caa8b2a4 25 void pc_listen() {
maruchik 0:2418caa8b2a4 26 if (pc.readable()) {
maruchik 0:2418caa8b2a4 27 char input = pc.getc();
maruchik 0:2418caa8b2a4 28
maruchik 0:2418caa8b2a4 29 switch(input) {
maruchik 0:2418caa8b2a4 30 case '1': pc.printf("%1.2f,%1.2f, %d, %d\r\n", roll, pitch, enter, back);
maruchik 0:2418caa8b2a4 31 break;
maruchik 0:2418caa8b2a4 32 }
maruchik 0:2418caa8b2a4 33 }
maruchik 0:2418caa8b2a4 34 }
maruchik 0:2418caa8b2a4 35
maruchik 0:2418caa8b2a4 36 int main(void)
maruchik 0:2418caa8b2a4 37 {
maruchik 0:2418caa8b2a4 38 float x=0, y=0, z=0;
maruchik 0:2418caa8b2a4 39 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
maruchik 0:2418caa8b2a4 40 PwmOut rled(LED1);
maruchik 0:2418caa8b2a4 41 PwmOut gled(LED2);
maruchik 0:2418caa8b2a4 42 PwmOut bled(LED3);
maruchik 0:2418caa8b2a4 43
maruchik 0:2418caa8b2a4 44 // printf("MMA8451 ID: %d\n", acc.getWhoAmI());
maruchik 0:2418caa8b2a4 45
maruchik 0:2418caa8b2a4 46 while (true) {
maruchik 0:2418caa8b2a4 47 x = acc.getAccX();
maruchik 0:2418caa8b2a4 48 y = acc.getAccY();
maruchik 0:2418caa8b2a4 49 z = acc.getAccZ();
maruchik 0:2418caa8b2a4 50
maruchik 0:2418caa8b2a4 51 roll = 180*atan(x / sqrt(z*z + y*y))/3.1415;
maruchik 0:2418caa8b2a4 52 pitch = 180*atan(y / sqrt(x*x + z*z))/3.1415;
maruchik 0:2418caa8b2a4 53
maruchik 0:2418caa8b2a4 54 rled = 1.0f - x;
maruchik 0:2418caa8b2a4 55 gled = 1.0f - y;
maruchik 0:2418caa8b2a4 56 bled = 1.0f - z;
maruchik 0:2418caa8b2a4 57 enter = !RIGHT.read();
maruchik 0:2418caa8b2a4 58 back = !LEFT.read();
maruchik 0:2418caa8b2a4 59
maruchik 0:2418caa8b2a4 60 // wait(0.1f);
maruchik 0:2418caa8b2a4 61 pc_listen();
maruchik 0:2418caa8b2a4 62
maruchik 0:2418caa8b2a4 63 }
maruchik 0:2418caa8b2a4 64 }