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