
school project
main.cpp@2:3278e4fd8fc2, 2016-01-12 (annotated)
- Committer:
- xlizne01
- Date:
- Tue Jan 12 17:02:19 2016 +0000
- Revision:
- 2:3278e4fd8fc2
- Parent:
- 1:b23831b703fe
- Child:
- 3:94bfc4de4ab1
FUNKCNI OVLADANI SMERU
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xlizne01 | 0:ae588eb31707 | 1 | #include "mbed.h" |
xlizne01 | 1:b23831b703fe | 2 | #include "MMA8451Q.h" |
xlizne01 | 1:b23831b703fe | 3 | |
xlizne01 | 1:b23831b703fe | 4 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
xlizne01 | 2:3278e4fd8fc2 | 5 | #define MOVE_ANGLE 45 // > degrees for move |
xlizne01 | 2:3278e4fd8fc2 | 6 | |
xlizne01 | 2:3278e4fd8fc2 | 7 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
xlizne01 | 2:3278e4fd8fc2 | 8 | |
xlizne01 | 2:3278e4fd8fc2 | 9 | void accelerometer(void); |
xlizne01 | 2:3278e4fd8fc2 | 10 | |
xlizne01 | 2:3278e4fd8fc2 | 11 | int i,j,k; |
xlizne01 | 2:3278e4fd8fc2 | 12 | int RED, GREEN, BLUE; |
xlizne01 | 2:3278e4fd8fc2 | 13 | int RledA, RledB, GledA, GledB, BledA, BledB; |
xlizne01 | 2:3278e4fd8fc2 | 14 | char acc_dir; |
xlizne01 | 1:b23831b703fe | 15 | |
xlizne01 | 0:ae588eb31707 | 16 | DigitalOut prvni(PTE5); |
xlizne01 | 0:ae588eb31707 | 17 | DigitalOut druhy(PTE4); |
xlizne01 | 0:ae588eb31707 | 18 | DigitalOut treti(PTE3); |
xlizne01 | 0:ae588eb31707 | 19 | DigitalOut ctvrty(PTE2); |
xlizne01 | 0:ae588eb31707 | 20 | DigitalOut paty(PTB11); |
xlizne01 | 0:ae588eb31707 | 21 | DigitalOut sesty(PTB10); |
xlizne01 | 0:ae588eb31707 | 22 | DigitalOut sedmy(PTB9); |
xlizne01 | 0:ae588eb31707 | 23 | DigitalOut osmy(PTB8); |
xlizne01 | 0:ae588eb31707 | 24 | |
xlizne01 | 0:ae588eb31707 | 25 | DigitalOut Rled1(PTC9); |
xlizne01 | 0:ae588eb31707 | 26 | DigitalOut Rled2(PTC8); |
xlizne01 | 0:ae588eb31707 | 27 | DigitalOut Rled3(PTA5); |
xlizne01 | 0:ae588eb31707 | 28 | DigitalOut Rled4(PTA4); |
xlizne01 | 0:ae588eb31707 | 29 | DigitalOut Rled5(PTA12); |
xlizne01 | 0:ae588eb31707 | 30 | DigitalOut Rled6(PTD4); |
xlizne01 | 0:ae588eb31707 | 31 | DigitalOut Rled7(PTA1); |
xlizne01 | 0:ae588eb31707 | 32 | DigitalOut Rled8(PTA2); |
xlizne01 | 0:ae588eb31707 | 33 | |
xlizne01 | 0:ae588eb31707 | 34 | DigitalOut Gled1(PTA13); |
xlizne01 | 0:ae588eb31707 | 35 | DigitalOut Gled2(PTD5); |
xlizne01 | 0:ae588eb31707 | 36 | DigitalOut Gled3(PTD0); |
xlizne01 | 0:ae588eb31707 | 37 | DigitalOut Gled4(PTD2); |
xlizne01 | 0:ae588eb31707 | 38 | DigitalOut Gled5(PTD3); |
xlizne01 | 0:ae588eb31707 | 39 | DigitalOut Gled6(PTD1); |
xlizne01 | 0:ae588eb31707 | 40 | DigitalOut Gled7(PTE31); |
xlizne01 | 0:ae588eb31707 | 41 | DigitalOut Gled8(PTB0); |
xlizne01 | 0:ae588eb31707 | 42 | |
xlizne01 | 0:ae588eb31707 | 43 | DigitalOut Bled1(PTC1); |
xlizne01 | 0:ae588eb31707 | 44 | DigitalOut Bled2(PTE29); |
xlizne01 | 0:ae588eb31707 | 45 | DigitalOut Bled3(PTC2); |
xlizne01 | 0:ae588eb31707 | 46 | DigitalOut Bled4(PTB3); |
xlizne01 | 0:ae588eb31707 | 47 | DigitalOut Bled5(PTB2); |
xlizne01 | 0:ae588eb31707 | 48 | DigitalOut Bled6(PTE21); |
xlizne01 | 0:ae588eb31707 | 49 | DigitalOut Bled7(PTE20); |
xlizne01 | 0:ae588eb31707 | 50 | DigitalOut Bled8(PTB1); |
xlizne01 | 0:ae588eb31707 | 51 | |
xlizne01 | 0:ae588eb31707 | 52 | |
xlizne01 | 2:3278e4fd8fc2 | 53 | |
xlizne01 | 0:ae588eb31707 | 54 | |
xlizne01 | 0:ae588eb31707 | 55 | int main() |
xlizne01 | 0:ae588eb31707 | 56 | { |
xlizne01 | 2:3278e4fd8fc2 | 57 | |
xlizne01 | 0:ae588eb31707 | 58 | |
xlizne01 | 0:ae588eb31707 | 59 | while(1) |
xlizne01 | 0:ae588eb31707 | 60 | { |
xlizne01 | 2:3278e4fd8fc2 | 61 | |
xlizne01 | 2:3278e4fd8fc2 | 62 | accelerometer(); |
xlizne01 | 0:ae588eb31707 | 63 | |
xlizne01 | 0:ae588eb31707 | 64 | for(k=1;k<5;k++) |
xlizne01 | 0:ae588eb31707 | 65 | { |
xlizne01 | 0:ae588eb31707 | 66 | |
xlizne01 | 0:ae588eb31707 | 67 | if(k==1) |
xlizne01 | 0:ae588eb31707 | 68 | { |
xlizne01 | 0:ae588eb31707 | 69 | prvni=1; |
xlizne01 | 0:ae588eb31707 | 70 | druhy=1; |
xlizne01 | 0:ae588eb31707 | 71 | treti=0; |
xlizne01 | 0:ae588eb31707 | 72 | ctvrty=0; |
xlizne01 | 0:ae588eb31707 | 73 | paty=0; |
xlizne01 | 0:ae588eb31707 | 74 | sesty=0; |
xlizne01 | 0:ae588eb31707 | 75 | sedmy=0; |
xlizne01 | 0:ae588eb31707 | 76 | osmy=0; |
xlizne01 | 0:ae588eb31707 | 77 | } |
xlizne01 | 0:ae588eb31707 | 78 | |
xlizne01 | 0:ae588eb31707 | 79 | if(k==2) |
xlizne01 | 0:ae588eb31707 | 80 | { |
xlizne01 | 0:ae588eb31707 | 81 | prvni=0; |
xlizne01 | 0:ae588eb31707 | 82 | druhy=0; |
xlizne01 | 0:ae588eb31707 | 83 | treti=1; |
xlizne01 | 0:ae588eb31707 | 84 | ctvrty=1; |
xlizne01 | 0:ae588eb31707 | 85 | paty=0; |
xlizne01 | 0:ae588eb31707 | 86 | sesty=0; |
xlizne01 | 0:ae588eb31707 | 87 | sedmy=0; |
xlizne01 | 0:ae588eb31707 | 88 | osmy=0; |
xlizne01 | 0:ae588eb31707 | 89 | } |
xlizne01 | 0:ae588eb31707 | 90 | |
xlizne01 | 0:ae588eb31707 | 91 | if(k==3) |
xlizne01 | 0:ae588eb31707 | 92 | { |
xlizne01 | 0:ae588eb31707 | 93 | prvni=0; |
xlizne01 | 0:ae588eb31707 | 94 | druhy=0; |
xlizne01 | 0:ae588eb31707 | 95 | treti=0; |
xlizne01 | 0:ae588eb31707 | 96 | ctvrty=0; |
xlizne01 | 0:ae588eb31707 | 97 | paty=1; |
xlizne01 | 0:ae588eb31707 | 98 | sesty=1; |
xlizne01 | 0:ae588eb31707 | 99 | sedmy=0; |
xlizne01 | 0:ae588eb31707 | 100 | osmy=0; |
xlizne01 | 0:ae588eb31707 | 101 | } |
xlizne01 | 0:ae588eb31707 | 102 | |
xlizne01 | 0:ae588eb31707 | 103 | if(k==4) |
xlizne01 | 0:ae588eb31707 | 104 | { |
xlizne01 | 0:ae588eb31707 | 105 | prvni=0; |
xlizne01 | 0:ae588eb31707 | 106 | druhy=0; |
xlizne01 | 0:ae588eb31707 | 107 | treti=0; |
xlizne01 | 0:ae588eb31707 | 108 | ctvrty=0; |
xlizne01 | 0:ae588eb31707 | 109 | paty=0; |
xlizne01 | 0:ae588eb31707 | 110 | sesty=0; |
xlizne01 | 0:ae588eb31707 | 111 | sedmy=1; |
xlizne01 | 0:ae588eb31707 | 112 | osmy=1; |
xlizne01 | 0:ae588eb31707 | 113 | } |
xlizne01 | 0:ae588eb31707 | 114 | |
xlizne01 | 0:ae588eb31707 | 115 | for(j=1;j<5;j++) |
xlizne01 | 0:ae588eb31707 | 116 | { |
xlizne01 | 0:ae588eb31707 | 117 | |
xlizne01 | 2:3278e4fd8fc2 | 118 | // if(j==1) |
xlizne01 | 2:3278e4fd8fc2 | 119 | // { |
xlizne01 | 2:3278e4fd8fc2 | 120 | switch(acc_dir) |
xlizne01 | 2:3278e4fd8fc2 | 121 | { |
xlizne01 | 2:3278e4fd8fc2 | 122 | case 'D': |
xlizne01 | 2:3278e4fd8fc2 | 123 | RED=10; |
xlizne01 | 2:3278e4fd8fc2 | 124 | break; |
xlizne01 | 2:3278e4fd8fc2 | 125 | case 'R': |
xlizne01 | 2:3278e4fd8fc2 | 126 | BLUE=10; |
xlizne01 | 2:3278e4fd8fc2 | 127 | break; |
xlizne01 | 2:3278e4fd8fc2 | 128 | case 'U': |
xlizne01 | 2:3278e4fd8fc2 | 129 | GREEN=10; |
xlizne01 | 2:3278e4fd8fc2 | 130 | break; |
xlizne01 | 2:3278e4fd8fc2 | 131 | case 'L': |
xlizne01 | 2:3278e4fd8fc2 | 132 | RED=10; |
xlizne01 | 2:3278e4fd8fc2 | 133 | BLUE=10; |
xlizne01 | 2:3278e4fd8fc2 | 134 | break; |
xlizne01 | 2:3278e4fd8fc2 | 135 | default: |
xlizne01 | 2:3278e4fd8fc2 | 136 | break; |
xlizne01 | 2:3278e4fd8fc2 | 137 | } |
xlizne01 | 2:3278e4fd8fc2 | 138 | |
xlizne01 | 2:3278e4fd8fc2 | 139 | /* } |
xlizne01 | 0:ae588eb31707 | 140 | |
xlizne01 | 0:ae588eb31707 | 141 | if(j==2) |
xlizne01 | 0:ae588eb31707 | 142 | { |
xlizne01 | 2:3278e4fd8fc2 | 143 | |
xlizne01 | 0:ae588eb31707 | 144 | } |
xlizne01 | 0:ae588eb31707 | 145 | |
xlizne01 | 0:ae588eb31707 | 146 | if(j==3) |
xlizne01 | 0:ae588eb31707 | 147 | { |
xlizne01 | 2:3278e4fd8fc2 | 148 | |
xlizne01 | 0:ae588eb31707 | 149 | } |
xlizne01 | 0:ae588eb31707 | 150 | |
xlizne01 | 0:ae588eb31707 | 151 | if(j==4) |
xlizne01 | 0:ae588eb31707 | 152 | { |
xlizne01 | 2:3278e4fd8fc2 | 153 | |
xlizne01 | 0:ae588eb31707 | 154 | } |
xlizne01 | 2:3278e4fd8fc2 | 155 | */ |
xlizne01 | 0:ae588eb31707 | 156 | for(i=11;i>0;i--) |
xlizne01 | 0:ae588eb31707 | 157 | { |
xlizne01 | 0:ae588eb31707 | 158 | |
xlizne01 | 0:ae588eb31707 | 159 | if(RED>0) |
xlizne01 | 0:ae588eb31707 | 160 | { |
xlizne01 | 0:ae588eb31707 | 161 | RledA=1; |
xlizne01 | 0:ae588eb31707 | 162 | RledB=1; |
xlizne01 | 0:ae588eb31707 | 163 | } |
xlizne01 | 0:ae588eb31707 | 164 | else |
xlizne01 | 0:ae588eb31707 | 165 | { |
xlizne01 | 0:ae588eb31707 | 166 | RledA=0; |
xlizne01 | 0:ae588eb31707 | 167 | RledB=0; |
xlizne01 | 0:ae588eb31707 | 168 | } |
xlizne01 | 0:ae588eb31707 | 169 | |
xlizne01 | 0:ae588eb31707 | 170 | if(GREEN>0) |
xlizne01 | 0:ae588eb31707 | 171 | { |
xlizne01 | 0:ae588eb31707 | 172 | GledA=1; |
xlizne01 | 0:ae588eb31707 | 173 | GledB=1; |
xlizne01 | 0:ae588eb31707 | 174 | } |
xlizne01 | 0:ae588eb31707 | 175 | else |
xlizne01 | 0:ae588eb31707 | 176 | { |
xlizne01 | 0:ae588eb31707 | 177 | GledA=0; |
xlizne01 | 0:ae588eb31707 | 178 | GledB=0; |
xlizne01 | 0:ae588eb31707 | 179 | } |
xlizne01 | 0:ae588eb31707 | 180 | |
xlizne01 | 0:ae588eb31707 | 181 | if(BLUE>0) |
xlizne01 | 0:ae588eb31707 | 182 | { |
xlizne01 | 0:ae588eb31707 | 183 | BledA=1; |
xlizne01 | 0:ae588eb31707 | 184 | BledB=1; |
xlizne01 | 0:ae588eb31707 | 185 | } |
xlizne01 | 0:ae588eb31707 | 186 | else |
xlizne01 | 0:ae588eb31707 | 187 | { |
xlizne01 | 0:ae588eb31707 | 188 | BledA=0; |
xlizne01 | 0:ae588eb31707 | 189 | BledB=0; |
xlizne01 | 0:ae588eb31707 | 190 | } |
xlizne01 | 0:ae588eb31707 | 191 | |
xlizne01 | 0:ae588eb31707 | 192 | if(j==1) |
xlizne01 | 0:ae588eb31707 | 193 | { |
xlizne01 | 0:ae588eb31707 | 194 | Rled1=RledA; |
xlizne01 | 0:ae588eb31707 | 195 | Rled2=RledB; |
xlizne01 | 0:ae588eb31707 | 196 | Gled1=GledA; |
xlizne01 | 0:ae588eb31707 | 197 | Gled2=GledB; |
xlizne01 | 0:ae588eb31707 | 198 | Bled1=BledA; |
xlizne01 | 0:ae588eb31707 | 199 | Bled2=BledB; |
xlizne01 | 0:ae588eb31707 | 200 | } |
xlizne01 | 0:ae588eb31707 | 201 | |
xlizne01 | 0:ae588eb31707 | 202 | if(j==2) |
xlizne01 | 0:ae588eb31707 | 203 | { |
xlizne01 | 0:ae588eb31707 | 204 | Rled3=RledA; |
xlizne01 | 0:ae588eb31707 | 205 | Rled4=RledB; |
xlizne01 | 0:ae588eb31707 | 206 | Gled3=GledA; |
xlizne01 | 0:ae588eb31707 | 207 | Gled4=GledB; |
xlizne01 | 0:ae588eb31707 | 208 | Bled3=BledA; |
xlizne01 | 0:ae588eb31707 | 209 | Bled4=BledB; |
xlizne01 | 0:ae588eb31707 | 210 | } |
xlizne01 | 0:ae588eb31707 | 211 | |
xlizne01 | 0:ae588eb31707 | 212 | if(j==3) |
xlizne01 | 0:ae588eb31707 | 213 | { |
xlizne01 | 0:ae588eb31707 | 214 | Rled5=RledA; |
xlizne01 | 0:ae588eb31707 | 215 | Rled6=RledB; |
xlizne01 | 0:ae588eb31707 | 216 | Gled5=GledA; |
xlizne01 | 0:ae588eb31707 | 217 | Gled6=GledB; |
xlizne01 | 0:ae588eb31707 | 218 | Bled5=BledA; |
xlizne01 | 0:ae588eb31707 | 219 | Bled6=BledB; |
xlizne01 | 0:ae588eb31707 | 220 | } |
xlizne01 | 0:ae588eb31707 | 221 | |
xlizne01 | 0:ae588eb31707 | 222 | if(j==4) |
xlizne01 | 0:ae588eb31707 | 223 | { |
xlizne01 | 0:ae588eb31707 | 224 | Rled7=RledA; |
xlizne01 | 0:ae588eb31707 | 225 | Rled8=RledB; |
xlizne01 | 0:ae588eb31707 | 226 | Gled7=GledA; |
xlizne01 | 0:ae588eb31707 | 227 | Gled8=GledB; |
xlizne01 | 0:ae588eb31707 | 228 | Bled7=BledA; |
xlizne01 | 0:ae588eb31707 | 229 | Bled8=BledB; |
xlizne01 | 0:ae588eb31707 | 230 | } |
xlizne01 | 0:ae588eb31707 | 231 | |
xlizne01 | 2:3278e4fd8fc2 | 232 | wait(0.00001); |
xlizne01 | 0:ae588eb31707 | 233 | RED--; |
xlizne01 | 0:ae588eb31707 | 234 | BLUE--; |
xlizne01 | 0:ae588eb31707 | 235 | GREEN--; |
xlizne01 | 0:ae588eb31707 | 236 | |
xlizne01 | 0:ae588eb31707 | 237 | } |
xlizne01 | 0:ae588eb31707 | 238 | } |
xlizne01 | 0:ae588eb31707 | 239 | } |
xlizne01 | 0:ae588eb31707 | 240 | } |
xlizne01 | 0:ae588eb31707 | 241 | } |
xlizne01 | 2:3278e4fd8fc2 | 242 | |
xlizne01 | 2:3278e4fd8fc2 | 243 | void accelerometer() |
xlizne01 | 2:3278e4fd8fc2 | 244 | { |
xlizne01 | 2:3278e4fd8fc2 | 245 | |
xlizne01 | 2:3278e4fd8fc2 | 246 | float ax, ay, az; |
xlizne01 | 2:3278e4fd8fc2 | 247 | float xAngle, yAngle; |
xlizne01 | 2:3278e4fd8fc2 | 248 | |
xlizne01 | 2:3278e4fd8fc2 | 249 | ax = acc.getAccX(); |
xlizne01 | 2:3278e4fd8fc2 | 250 | ay = acc.getAccY(); |
xlizne01 | 2:3278e4fd8fc2 | 251 | az = acc.getAccZ(); |
xlizne01 | 2:3278e4fd8fc2 | 252 | |
xlizne01 | 2:3278e4fd8fc2 | 253 | xAngle = atan( ax / (sqrt((ay)*(ay) + (az)*(az)))) * 60; |
xlizne01 | 2:3278e4fd8fc2 | 254 | yAngle = atan( ay / (sqrt((ax)*(ax) + (az)*(az)))) * 60; |
xlizne01 | 2:3278e4fd8fc2 | 255 | |
xlizne01 | 2:3278e4fd8fc2 | 256 | |
xlizne01 | 2:3278e4fd8fc2 | 257 | if(abs(xAngle) >= abs(yAngle)) |
xlizne01 | 2:3278e4fd8fc2 | 258 | { |
xlizne01 | 2:3278e4fd8fc2 | 259 | if(xAngle >= MOVE_ANGLE) |
xlizne01 | 2:3278e4fd8fc2 | 260 | { |
xlizne01 | 2:3278e4fd8fc2 | 261 | acc_dir = 'U'; // +X |
xlizne01 | 2:3278e4fd8fc2 | 262 | } |
xlizne01 | 2:3278e4fd8fc2 | 263 | |
xlizne01 | 2:3278e4fd8fc2 | 264 | if(xAngle <= -MOVE_ANGLE) |
xlizne01 | 2:3278e4fd8fc2 | 265 | { |
xlizne01 | 2:3278e4fd8fc2 | 266 | acc_dir = 'D'; // -X |
xlizne01 | 2:3278e4fd8fc2 | 267 | } |
xlizne01 | 2:3278e4fd8fc2 | 268 | } |
xlizne01 | 2:3278e4fd8fc2 | 269 | |
xlizne01 | 2:3278e4fd8fc2 | 270 | else |
xlizne01 | 2:3278e4fd8fc2 | 271 | { |
xlizne01 | 2:3278e4fd8fc2 | 272 | if(yAngle >= MOVE_ANGLE) |
xlizne01 | 2:3278e4fd8fc2 | 273 | { |
xlizne01 | 2:3278e4fd8fc2 | 274 | acc_dir = 'L'; // +Y |
xlizne01 | 2:3278e4fd8fc2 | 275 | } |
xlizne01 | 2:3278e4fd8fc2 | 276 | |
xlizne01 | 2:3278e4fd8fc2 | 277 | if(yAngle <= -MOVE_ANGLE) |
xlizne01 | 2:3278e4fd8fc2 | 278 | { |
xlizne01 | 2:3278e4fd8fc2 | 279 | acc_dir = 'R'; // -Y |
xlizne01 | 2:3278e4fd8fc2 | 280 | } |
xlizne01 | 2:3278e4fd8fc2 | 281 | } |
xlizne01 | 2:3278e4fd8fc2 | 282 | |
xlizne01 | 2:3278e4fd8fc2 | 283 | } |
xlizne01 | 0:ae588eb31707 | 284 | |
xlizne01 | 0:ae588eb31707 | 285 | |
xlizne01 | 0:ae588eb31707 | 286 |