Lab4 code

Dependencies:   MMA8451Q

Fork of Accelerometer_example by William Marsh

Committer:
toh2018
Date:
Thu Mar 01 13:35:25 2018 +0000
Revision:
3:38630c6ae590
Parent:
2:840d7ce4075f
add part two code

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 }
toh2018 3:38630c6ae590 88
toh2018 3:38630c6ae590 89 /*****************************************
toh2018 3:38630c6ae590 90 Second part code please uncomment and turn above codes to comment to run
toh2018 3:38630c6ae590 91 #include "mbed.h"
toh2018 3:38630c6ae590 92 #include "rtos.h"
toh2018 3:38630c6ae590 93 #include "MMA8451Q.h"
toh2018 3:38630c6ae590 94
toh2018 3:38630c6ae590 95 PinName const SDA = PTE25;
toh2018 3:38630c6ae590 96 PinName const SCL = PTE24;
toh2018 3:38630c6ae590 97
toh2018 3:38630c6ae590 98
toh2018 3:38630c6ae590 99
toh2018 3:38630c6ae590 100 Timer timer;
toh2018 3:38630c6ae590 101
toh2018 3:38630c6ae590 102 #define MMA8451_I2C_ADDRESS (0x1d<<1)
toh2018 3:38630c6ae590 103
toh2018 3:38630c6ae590 104 int main(void)
toh2018 3:38630c6ae590 105 {
toh2018 3:38630c6ae590 106 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
toh2018 3:38630c6ae590 107 PwmOut rled(LED1);
toh2018 3:38630c6ae590 108 PwmOut gled(LED2);
toh2018 3:38630c6ae590 109 PwmOut bled(LED3);
toh2018 3:38630c6ae590 110
toh2018 3:38630c6ae590 111 Serial pc(USBTX, USBRX); // tx, rx
toh2018 3:38630c6ae590 112
toh2018 3:38630c6ae590 113 pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI());
toh2018 3:38630c6ae590 114
toh2018 3:38630c6ae590 115 while (true) {
toh2018 3:38630c6ae590 116 float x, y, z;
toh2018 3:38630c6ae590 117
toh2018 3:38630c6ae590 118 x = acc.getAccX();
toh2018 3:38630c6ae590 119 y = acc.getAccY();
toh2018 3:38630c6ae590 120 z = acc.getAccZ();
toh2018 3:38630c6ae590 121
toh2018 3:38630c6ae590 122 // set the time
toh2018 3:38630c6ae590 123
toh2018 3:38630c6ae590 124 timer.start();
toh2018 3:38630c6ae590 125 //timer.reset();
toh2018 3:38630c6ae590 126
toh2018 3:38630c6ae590 127
toh2018 3:38630c6ae590 128
toh2018 3:38630c6ae590 129 //pc.printf("time is: %f \n \r",timer.read());
toh2018 3:38630c6ae590 130 Thread::wait(300);
toh2018 3:38630c6ae590 131
toh2018 3:38630c6ae590 132 //check the time
toh2018 3:38630c6ae590 133
toh2018 3:38630c6ae590 134
toh2018 3:38630c6ae590 135
toh2018 3:38630c6ae590 136
toh2018 3:38630c6ae590 137 // this is flat state
toh2018 3:38630c6ae590 138 if(timer.read() <=10 && z>= 0.9 ){
toh2018 3:38630c6ae590 139
toh2018 3:38630c6ae590 140 gled= 0;
toh2018 3:38630c6ae590 141 rled= 1;
toh2018 3:38630c6ae590 142 bled= 1;
toh2018 3:38630c6ae590 143
toh2018 3:38630c6ae590 144 pc.printf("flat \n \r");
toh2018 3:38630c6ae590 145
toh2018 3:38630c6ae590 146 pc.printf("time is: %f \n \r",timer.read());
toh2018 3:38630c6ae590 147
toh2018 3:38630c6ae590 148
toh2018 3:38630c6ae590 149
toh2018 3:38630c6ae590 150 }else if(timer.read()<=6 && y<= -0.90 ){
toh2018 3:38630c6ae590 151
toh2018 3:38630c6ae590 152 gled= 0;
toh2018 3:38630c6ae590 153 rled= 1;
toh2018 3:38630c6ae590 154 bled= 1;
toh2018 3:38630c6ae590 155
toh2018 3:38630c6ae590 156 pc.printf("Right \n \r");
toh2018 3:38630c6ae590 157 pc.printf("time is: %f \n \r",timer.read());
toh2018 3:38630c6ae590 158
toh2018 3:38630c6ae590 159
toh2018 3:38630c6ae590 160 //this is the Up
toh2018 3:38630c6ae590 161 }else if(timer.read()<=6 && x<= -0.90 ){
toh2018 3:38630c6ae590 162
toh2018 3:38630c6ae590 163 gled= 0;
toh2018 3:38630c6ae590 164 rled= 1;
toh2018 3:38630c6ae590 165 bled= 1;
toh2018 3:38630c6ae590 166
toh2018 3:38630c6ae590 167 pc.printf("up \n \r");
toh2018 3:38630c6ae590 168 pc.printf("time is: %f \n \r",timer.read());
toh2018 3:38630c6ae590 169
toh2018 3:38630c6ae590 170
toh2018 3:38630c6ae590 171 }
toh2018 3:38630c6ae590 172
toh2018 3:38630c6ae590 173 else{
toh2018 3:38630c6ae590 174
toh2018 3:38630c6ae590 175 pc.printf("Error message \n \r");
toh2018 3:38630c6ae590 176 rled = 0;
toh2018 3:38630c6ae590 177 timer.reset();
toh2018 3:38630c6ae590 178 gled= 1;
toh2018 3:38630c6ae590 179 rled= 0;
toh2018 3:38630c6ae590 180 bled= 1;
toh2018 3:38630c6ae590 181
toh2018 3:38630c6ae590 182 }
toh2018 3:38630c6ae590 183
toh2018 3:38630c6ae590 184
toh2018 3:38630c6ae590 185 // this is to turn the light off during the turn event
toh2018 3:38630c6ae590 186
toh2018 3:38630c6ae590 187 }
toh2018 3:38630c6ae590 188 }
toh2018 3:38630c6ae590 189
toh2018 3:38630c6ae590 190
toh2018 3:38630c6ae590 191 *******************************************/