Lab4 code

Dependencies:   MMA8451Q

Fork of Accelerometer_example by William Marsh

Committer:
toh2018
Date:
Fri Feb 16 13:53:36 2018 +0000
Revision:
2:840d7ce4075f
Parent:
0:a1caba5c4e48
Child:
3:38630c6ae590
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WilliamMarshQMUL 0:a1caba5c4e48 1 #include "mbed.h"
WilliamMarshQMUL 0:a1caba5c4e48 2 #include "rtos.h"
WilliamMarshQMUL 0:a1caba5c4e48 3 #include "MMA8451Q.h"
toh2018 2:840d7ce4075f 4
WilliamMarshQMUL 0:a1caba5c4e48 5 PinName const SDA = PTE25;
WilliamMarshQMUL 0:a1caba5c4e48 6 PinName const SCL = PTE24;
toh2018 2:840d7ce4075f 7
WilliamMarshQMUL 0:a1caba5c4e48 8 #define MMA8451_I2C_ADDRESS (0x1d<<1)
toh2018 2:840d7ce4075f 9
WilliamMarshQMUL 0:a1caba5c4e48 10 int main(void)
WilliamMarshQMUL 0:a1caba5c4e48 11 {
WilliamMarshQMUL 0:a1caba5c4e48 12 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
WilliamMarshQMUL 0:a1caba5c4e48 13 PwmOut rled(LED1);
WilliamMarshQMUL 0:a1caba5c4e48 14 PwmOut gled(LED2);
WilliamMarshQMUL 0:a1caba5c4e48 15 PwmOut bled(LED3);
toh2018 2:840d7ce4075f 16
WilliamMarshQMUL 0:a1caba5c4e48 17 Serial pc(USBTX, USBRX); // tx, rx
toh2018 2:840d7ce4075f 18
WilliamMarshQMUL 0:a1caba5c4e48 19 pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI());
toh2018 2:840d7ce4075f 20
WilliamMarshQMUL 0:a1caba5c4e48 21 while (true) {
WilliamMarshQMUL 0:a1caba5c4e48 22 float x, y, z;
toh2018 2:840d7ce4075f 23
WilliamMarshQMUL 0:a1caba5c4e48 24 x = acc.getAccX();
WilliamMarshQMUL 0:a1caba5c4e48 25 y = acc.getAccY();
WilliamMarshQMUL 0:a1caba5c4e48 26 z = acc.getAccZ();
toh2018 2:840d7ce4075f 27
toh2018 2:840d7ce4075f 28 //
WilliamMarshQMUL 0:a1caba5c4e48 29 Thread::wait(300);
toh2018 2:840d7ce4075f 30
toh2018 2:840d7ce4075f 31 // this is flat state
toh2018 2:840d7ce4075f 32 if(z>= 0.9 ){
toh2018 2:840d7ce4075f 33
toh2018 2:840d7ce4075f 34
toh2018 2:840d7ce4075f 35 rled = 0;
toh2018 2:840d7ce4075f 36 gled = 0;
toh2018 2:840d7ce4075f 37 bled =0;
toh2018 2:840d7ce4075f 38 pc.printf("flat \n \r");
toh2018 2:840d7ce4075f 39 //this is the left sate
toh2018 2:840d7ce4075f 40 }else if(y>= 0.9 ){
toh2018 2:840d7ce4075f 41
toh2018 2:840d7ce4075f 42 rled = 0;
toh2018 2:840d7ce4075f 43 gled = 0;
toh2018 2:840d7ce4075f 44 bled =0;
toh2018 2:840d7ce4075f 45 pc.printf("Left \n \r");
toh2018 2:840d7ce4075f 46 //this is the down state
toh2018 2:840d7ce4075f 47 }else if(x>= 0.9){
toh2018 2:840d7ce4075f 48
toh2018 2:840d7ce4075f 49 rled = 0;
toh2018 2:840d7ce4075f 50 gled = 0;
toh2018 2:840d7ce4075f 51 bled =0;
toh2018 2:840d7ce4075f 52 pc.printf("down \n \r");
toh2018 2:840d7ce4075f 53 //this is ver state
toh2018 2:840d7ce4075f 54 }else if(z<= -0.90 ){
toh2018 2:840d7ce4075f 55
toh2018 2:840d7ce4075f 56 rled = 0;
toh2018 2:840d7ce4075f 57 gled = 0;
toh2018 2:840d7ce4075f 58 bled =0;
toh2018 2:840d7ce4075f 59 pc.printf("over \n \r");
toh2018 2:840d7ce4075f 60 //this is the right
toh2018 2:840d7ce4075f 61 }else if(y<= -0.90 ){
toh2018 2:840d7ce4075f 62
toh2018 2:840d7ce4075f 63 rled = 0;
toh2018 2:840d7ce4075f 64 gled = 0;
toh2018 2:840d7ce4075f 65 bled =0;
toh2018 2:840d7ce4075f 66 pc.printf("Right \n \r");
toh2018 2:840d7ce4075f 67 //this is the Up
toh2018 2:840d7ce4075f 68 }else if(x<= -0.90 ){
toh2018 2:840d7ce4075f 69
toh2018 2:840d7ce4075f 70 rled = 0;
toh2018 2:840d7ce4075f 71 gled = 0;
toh2018 2:840d7ce4075f 72 bled =0;
toh2018 2:840d7ce4075f 73 pc.printf("up \n \r");
toh2018 2:840d7ce4075f 74
toh2018 2:840d7ce4075f 75 }
toh2018 2:840d7ce4075f 76 // this is to turn the light off during the turn event
toh2018 2:840d7ce4075f 77 else{
toh2018 2:840d7ce4075f 78
toh2018 2:840d7ce4075f 79 rled = 1;
toh2018 2:840d7ce4075f 80 gled = 1;
toh2018 2:840d7ce4075f 81 bled =1;
toh2018 2:840d7ce4075f 82 pc.printf(" \n \r");
toh2018 2:840d7ce4075f 83 }
toh2018 2:840d7ce4075f 84 //print out the value
toh2018 2:840d7ce4075f 85 pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f \n \r", x, y, z);
WilliamMarshQMUL 0:a1caba5c4e48 86 }
WilliamMarshQMUL 0:a1caba5c4e48 87 }