la4-2(unfinished)

Dependencies:   MMA8451Q

Fork of Accelerometer_example by William Marsh

Committer:
Peilingyi
Date:
Fri Feb 16 23:37:53 2018 +0000
Revision:
2:eebefb7c9690
Parent:
0:a1caba5c4e48
Child:
3:374bdcd8ee4c
lab4-2(unfinished)

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"
WilliamMarshQMUL 0:a1caba5c4e48 4
WilliamMarshQMUL 0:a1caba5c4e48 5 PinName const SDA = PTE25;
WilliamMarshQMUL 0:a1caba5c4e48 6 PinName const SCL = PTE24;
WilliamMarshQMUL 0:a1caba5c4e48 7
WilliamMarshQMUL 0:a1caba5c4e48 8 #define MMA8451_I2C_ADDRESS (0x1d<<1)
WilliamMarshQMUL 0:a1caba5c4e48 9
Peilingyi 2:eebefb7c9690 10 Thread thread1;
Peilingyi 2:eebefb7c9690 11 DigitalOut ledr(LED1);
Peilingyi 2:eebefb7c9690 12 DigitalOut ledg(LED2);
Peilingyi 2:eebefb7c9690 13 timer t;
Peilingyi 2:eebefb7c9690 14
Peilingyi 2:eebefb7c9690 15 void stationary
Peilingyi 2:eebefb7c9690 16 {
Peilingyi 2:eebefb7c9690 17 while(1)
Peilingyi 2:eebefb7c9690 18 {
Peilingyi 2:eebefb7c9690 19 if ((abs((x+y+z)-1))<threshold)
Peilingyi 2:eebefb7c9690 20 {
Peilingyi 2:eebefb7c9690 21 switch (state)
Peilingyi 2:eebefb7c9690 22 case 0:
Peilingyi 2:eebefb7c9690 23 ledr=1;
Peilingyi 2:eebefb7c9690 24 ledg=1;
Peilingyi 2:eebefb7c9690 25 state = 1;
Peilingyi 2:eebefb7c9690 26 break;
Peilingyi 2:eebefb7c9690 27 case 1:
Peilingyi 2:eebefb7c9690 28 if ((abs(z-1)<=errorrange))//flat
Peilingyi 2:eebefb7c9690 29 {
Peilingyi 2:eebefb7c9690 30 if (countflag == 0)
Peilingyi 2:eebefb7c9690 31 {
Peilingyi 2:eebefb7c9690 32 pc.printf("flat");
Peilingyi 2:eebefb7c9690 33 countflag = filter;
Peilingyi 2:eebefb7c9690 34 ledg = 0;
Peilingyi 2:eebefb7c9690 35 t.start();
Peilingyi 2:eebefb7c9690 36 // state = 1;//for flat
Peilingyi 2:eebefb7c9690 37 }
Peilingyi 2:eebefb7c9690 38 else
Peilingyi 2:eebefb7c9690 39 {
Peilingyi 2:eebefb7c9690 40 countflag--;
Peilingyi 2:eebefb7c9690 41 }
Peilingyi 2:eebefb7c9690 42 }
Peilingyi 2:eebefb7c9690 43 else
Peilingyi 2:eebefb7c9690 44 {
Peilingyi 2:eebefb7c9690 45 t.stop();
Peilingyi 2:eebefb7c9690 46 temp_t = t.read();
Peilingyi 2:eebefb7c9690 47 if (temp_t>10)
Peilingyi 2:eebefb7c9690 48 {
Peilingyi 2:eebefb7c9690 49 state = 2;
Peilingyi 2:eebefb7c9690 50 steps = 1;
Peilingyi 2:eebefb7c9690 51 ledg = 0;
Peilingyi 2:eebefb7c9690 52 ledr = 1;
Peilingyi 2:eebefb7c9690 53 }
Peilingyi 2:eebefb7c9690 54 else
Peilingyi 2:eebefb7c9690 55 {
Peilingyi 2:eebefb7c9690 56 ledg = 1;
Peilingyi 2:eebefb7c9690 57 ledr = 0;
Peilingyi 2:eebefb7c9690 58 state =0;
Peilingyi 2:eebefb7c9690 59 pc.printf("error!");
Peilingyi 2:eebefb7c9690 60 }
Peilingyi 2:eebefb7c9690 61
Peilingyi 2:eebefb7c9690 62 }
Peilingyi 2:eebefb7c9690 63
Peilingyi 2:eebefb7c9690 64 }
Peilingyi 2:eebefb7c9690 65 break;
Peilingyi 2:eebefb7c9690 66 case 2:
Peilingyi 2:eebefb7c9690 67 {
Peilingyi 2:eebefb7c9690 68 if (abs(x+1.0f)<=errorrange)
Peilingyi 2:eebefb7c9690 69 {
Peilingyi 2:eebefb7c9690 70 if (countflag == 0)
Peilingyi 2:eebefb7c9690 71 {
Peilingyi 2:eebefb7c9690 72 pc.printf("right");
Peilingyi 2:eebefb7c9690 73 countflag = filter;
Peilingyi 2:eebefb7c9690 74 t.start();
Peilingyi 2:eebefb7c9690 75
Peilingyi 2:eebefb7c9690 76 }
Peilingyi 2:eebefb7c9690 77 else
Peilingyi 2:eebefb7c9690 78 {
Peilingyi 2:eebefb7c9690 79 countflag--;
Peilingyi 2:eebefb7c9690 80 }
Peilingyi 2:eebefb7c9690 81 }
Peilingyi 2:eebefb7c9690 82 else
Peilingyi 2:eebefb7c9690 83 {
Peilingyi 2:eebefb7c9690 84 t.stop();
Peilingyi 2:eebefb7c9690 85 temp_t = t.read();
Peilingyi 2:eebefb7c9690 86 if ((temp_t>=2) && (temp_t<=6))
Peilingyi 2:eebefb7c9690 87 {
Peilingyi 2:eebefb7c9690 88 state = 3;
Peilingyi 2:eebefb7c9690 89 steps = 2;
Peilingyi 2:eebefb7c9690 90 }
Peilingyi 2:eebefb7c9690 91 else
Peilingyi 2:eebefb7c9690 92 {
Peilingyi 2:eebefb7c9690 93 ledg = 1;
Peilingyi 2:eebefb7c9690 94 ledr = 0;
Peilingyi 2:eebefb7c9690 95 pc.printf("error!");
Peilingyi 2:eebefb7c9690 96 }
Peilingyi 2:eebefb7c9690 97
Peilingyi 2:eebefb7c9690 98 }
Peilingyi 2:eebefb7c9690 99 }
Peilingyi 2:eebefb7c9690 100 break;
Peilingyi 2:eebefb7c9690 101 case 3:
Peilingyi 2:eebefb7c9690 102 {
Peilingyi 2:eebefb7c9690 103 if (abs(x+1.0f)<=errorrange)
Peilingyi 2:eebefb7c9690 104 {
Peilingyi 2:eebefb7c9690 105 if (countflag == 0)
Peilingyi 2:eebefb7c9690 106 {
Peilingyi 2:eebefb7c9690 107 pc.printf("up");
Peilingyi 2:eebefb7c9690 108 countflag = filter;
Peilingyi 2:eebefb7c9690 109 t.start();
Peilingyi 2:eebefb7c9690 110 }
Peilingyi 2:eebefb7c9690 111 else
Peilingyi 2:eebefb7c9690 112 {
Peilingyi 2:eebefb7c9690 113 countflag--;
Peilingyi 2:eebefb7c9690 114 }
Peilingyi 2:eebefb7c9690 115 }
Peilingyi 2:eebefb7c9690 116 else
Peilingyi 2:eebefb7c9690 117 {
Peilingyi 2:eebefb7c9690 118 t.stop();
Peilingyi 2:eebefb7c9690 119 temp_t = t.read();
Peilingyi 2:eebefb7c9690 120 if ((temp_t>=4) && (temp_t<=8))
Peilingyi 2:eebefb7c9690 121 {
Peilingyi 2:eebefb7c9690 122 state = 0;
Peilingyi 2:eebefb7c9690 123 }
Peilingyi 2:eebefb7c9690 124 else
Peilingyi 2:eebefb7c9690 125 {
Peilingyi 2:eebefb7c9690 126 ledg = 1;
Peilingyi 2:eebefb7c9690 127 ledr = 0;
Peilingyi 2:eebefb7c9690 128 pc.printf("error!");
Peilingyi 2:eebefb7c9690 129 }
Peilingyi 2:eebefb7c9690 130
Peilingyi 2:eebefb7c9690 131 }
Peilingyi 2:eebefb7c9690 132 }
Peilingyi 2:eebefb7c9690 133
Peilingyi 2:eebefb7c9690 134
Peilingyi 2:eebefb7c9690 135
Peilingyi 2:eebefb7c9690 136 }
Peilingyi 2:eebefb7c9690 137 }
Peilingyi 2:eebefb7c9690 138
WilliamMarshQMUL 0:a1caba5c4e48 139 int main(void)
WilliamMarshQMUL 0:a1caba5c4e48 140 {
Peilingyi 2:eebefb7c9690 141
Peilingyi 2:eebefb7c9690 142
WilliamMarshQMUL 0:a1caba5c4e48 143 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
WilliamMarshQMUL 0:a1caba5c4e48 144 PwmOut rled(LED1);
WilliamMarshQMUL 0:a1caba5c4e48 145 PwmOut gled(LED2);
WilliamMarshQMUL 0:a1caba5c4e48 146 PwmOut bled(LED3);
WilliamMarshQMUL 0:a1caba5c4e48 147 Serial pc(USBTX, USBRX); // tx, rx
Peilingyi 2:eebefb7c9690 148 ledg = 1;
Peilingyi 2:eebefb7c9690 149 ledr = 1;
Peilingyi 2:eebefb7c9690 150
WilliamMarshQMUL 0:a1caba5c4e48 151
Peilingyi 2:eebefb7c9690 152 thread1.start(callback(stationary ));
Peilingyi 2:eebefb7c9690 153
WilliamMarshQMUL 0:a1caba5c4e48 154 pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI());
WilliamMarshQMUL 0:a1caba5c4e48 155
WilliamMarshQMUL 0:a1caba5c4e48 156 while (true) {
WilliamMarshQMUL 0:a1caba5c4e48 157 float x, y, z;
Peilingyi 2:eebefb7c9690 158
WilliamMarshQMUL 0:a1caba5c4e48 159 x = acc.getAccX();
WilliamMarshQMUL 0:a1caba5c4e48 160 y = acc.getAccY();
WilliamMarshQMUL 0:a1caba5c4e48 161 z = acc.getAccZ();
WilliamMarshQMUL 0:a1caba5c4e48 162 rled = 1.0f - abs(x);
WilliamMarshQMUL 0:a1caba5c4e48 163 gled = 1.0f - abs(y);
WilliamMarshQMUL 0:a1caba5c4e48 164 bled = 1.0f - abs(z);
WilliamMarshQMUL 0:a1caba5c4e48 165 Thread::wait(300);
Peilingyi 2:eebefb7c9690 166
Peilingyi 2:eebefb7c9690 167
WilliamMarshQMUL 0:a1caba5c4e48 168 pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z);
Peilingyi 2:eebefb7c9690 169
WilliamMarshQMUL 0:a1caba5c4e48 170 }
WilliamMarshQMUL 0:a1caba5c4e48 171 }