school project

Dependencies:   MMA8451Q mbed

Committer:
xlizne01
Date:
Tue Jan 12 19:43:31 2016 +0000
Revision:
4:4930b1cb20bd
Parent:
3:94bfc4de4ab1
Child:
5:d6738e1238d8
funkcni vraceni do nulove pozice matice

Who changed what in which revision?

UserRevisionLine numberNew 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 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 2:3278e4fd8fc2 14 int i,j,k;
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 4:4930b1cb20bd 21 int pole[4][4]={{0,0,4,2}, {16,8,2,0}, {0,0,8,0}, {0,0,0,0}};
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 4:4930b1cb20bd 74 case 'D':
xlizne01 4:4930b1cb20bd 75 pole[3][3]=8;
xlizne01 4:4930b1cb20bd 76 break;
xlizne01 4:4930b1cb20bd 77 case 'R':
xlizne01 4:4930b1cb20bd 78 pole[3][3]=6;
xlizne01 4:4930b1cb20bd 79 break;
xlizne01 4:4930b1cb20bd 80 case 'U':
xlizne01 4:4930b1cb20bd 81 pole[3][3]=4;
xlizne01 4:4930b1cb20bd 82 break;
xlizne01 4:4930b1cb20bd 83 case 'L':
xlizne01 4:4930b1cb20bd 84 pole[3][3]=2;
xlizne01 4:4930b1cb20bd 85 break;
xlizne01 4:4930b1cb20bd 86 case '0':
xlizne01 4:4930b1cb20bd 87 pole[3][3]=0;
xlizne01 4:4930b1cb20bd 88 break;
xlizne01 4:4930b1cb20bd 89
xlizne01 4:4930b1cb20bd 90 default:
xlizne01 4:4930b1cb20bd 91 break;
xlizne01 4:4930b1cb20bd 92 }
xlizne01 4:4930b1cb20bd 93 }
xlizne01 4:4930b1cb20bd 94
xlizne01 3:94bfc4de4ab1 95
xlizne01 3:94bfc4de4ab1 96 void zobrazeni()
xlizne01 3:94bfc4de4ab1 97 {
xlizne01 3:94bfc4de4ab1 98 for(k=0;k<4;k++)
xlizne01 3:94bfc4de4ab1 99 {
xlizne01 3:94bfc4de4ab1 100 prvni=0;
xlizne01 3:94bfc4de4ab1 101 druhy=0;
xlizne01 3:94bfc4de4ab1 102 treti=0;
xlizne01 3:94bfc4de4ab1 103 ctvrty=0;
xlizne01 3:94bfc4de4ab1 104 paty=0;
xlizne01 3:94bfc4de4ab1 105 sesty=0;
xlizne01 3:94bfc4de4ab1 106 sedmy=0;
xlizne01 3:94bfc4de4ab1 107 osmy=0;
xlizne01 3:94bfc4de4ab1 108
xlizne01 3:94bfc4de4ab1 109 if(k==0)
xlizne01 0:ae588eb31707 110 {
xlizne01 3:94bfc4de4ab1 111 prvni=1;
xlizne01 3:94bfc4de4ab1 112 druhy=1;
xlizne01 0:ae588eb31707 113 }
xlizne01 3:94bfc4de4ab1 114
xlizne01 3:94bfc4de4ab1 115 if(k==1)
xlizne01 0:ae588eb31707 116 {
xlizne01 3:94bfc4de4ab1 117 treti=1;
xlizne01 3:94bfc4de4ab1 118 ctvrty=1;
xlizne01 0:ae588eb31707 119 }
xlizne01 3:94bfc4de4ab1 120
xlizne01 3:94bfc4de4ab1 121 if(k==2)
xlizne01 0:ae588eb31707 122 {
xlizne01 3:94bfc4de4ab1 123 paty=1;
xlizne01 3:94bfc4de4ab1 124 sesty=1;
xlizne01 0:ae588eb31707 125 }
xlizne01 3:94bfc4de4ab1 126
xlizne01 3:94bfc4de4ab1 127 if(k==3)
xlizne01 0:ae588eb31707 128 {
xlizne01 3:94bfc4de4ab1 129 sedmy=1;
xlizne01 3:94bfc4de4ab1 130 osmy=1;
xlizne01 0:ae588eb31707 131 }
xlizne01 0:ae588eb31707 132
xlizne01 3:94bfc4de4ab1 133 for(j=0;j<4;j++)
xlizne01 3:94bfc4de4ab1 134 {
xlizne01 3:94bfc4de4ab1 135
xlizne01 3:94bfc4de4ab1 136 barva(j,k);
xlizne01 3:94bfc4de4ab1 137
xlizne01 3:94bfc4de4ab1 138 for(i=11;i>0;i--)
xlizne01 3:94bfc4de4ab1 139 {
xlizne01 3:94bfc4de4ab1 140 if(RED>0)
xlizne01 3:94bfc4de4ab1 141 {
xlizne01 3:94bfc4de4ab1 142 RledA=1;
xlizne01 3:94bfc4de4ab1 143 RledB=1;
xlizne01 3:94bfc4de4ab1 144 }
xlizne01 3:94bfc4de4ab1 145 else
xlizne01 3:94bfc4de4ab1 146 {
xlizne01 3:94bfc4de4ab1 147 RledA=0;
xlizne01 3:94bfc4de4ab1 148 RledB=0;
xlizne01 3:94bfc4de4ab1 149 }
xlizne01 0:ae588eb31707 150
xlizne01 3:94bfc4de4ab1 151 if(GREEN>0)
xlizne01 3:94bfc4de4ab1 152 {
xlizne01 3:94bfc4de4ab1 153 GledA=1;
xlizne01 3:94bfc4de4ab1 154 GledB=1;
xlizne01 3:94bfc4de4ab1 155 }
xlizne01 3:94bfc4de4ab1 156 else
xlizne01 3:94bfc4de4ab1 157 {
xlizne01 3:94bfc4de4ab1 158 GledA=0;
xlizne01 3:94bfc4de4ab1 159 GledB=0;
xlizne01 3:94bfc4de4ab1 160 }
xlizne01 3:94bfc4de4ab1 161
xlizne01 3:94bfc4de4ab1 162 if(BLUE>0)
xlizne01 3:94bfc4de4ab1 163 {
xlizne01 3:94bfc4de4ab1 164 BledA=1;
xlizne01 3:94bfc4de4ab1 165 BledB=1;
xlizne01 3:94bfc4de4ab1 166 }
xlizne01 3:94bfc4de4ab1 167 else
xlizne01 3:94bfc4de4ab1 168 {
xlizne01 3:94bfc4de4ab1 169 BledA=0;
xlizne01 3:94bfc4de4ab1 170 BledB=0;
xlizne01 3:94bfc4de4ab1 171 }
xlizne01 0:ae588eb31707 172
xlizne01 3:94bfc4de4ab1 173 if(j==0)
xlizne01 3:94bfc4de4ab1 174 {
xlizne01 3:94bfc4de4ab1 175 Rled1=RledA;
xlizne01 3:94bfc4de4ab1 176 Rled2=RledB;
xlizne01 3:94bfc4de4ab1 177 Gled1=GledA;
xlizne01 3:94bfc4de4ab1 178 Gled2=GledB;
xlizne01 3:94bfc4de4ab1 179 Bled1=BledA;
xlizne01 3:94bfc4de4ab1 180 Bled2=BledB;
xlizne01 3:94bfc4de4ab1 181 }
xlizne01 3:94bfc4de4ab1 182
xlizne01 3:94bfc4de4ab1 183 if(j==1)
xlizne01 3:94bfc4de4ab1 184 {
xlizne01 3:94bfc4de4ab1 185 Rled3=RledA;
xlizne01 3:94bfc4de4ab1 186 Rled4=RledB;
xlizne01 3:94bfc4de4ab1 187 Gled3=GledA;
xlizne01 3:94bfc4de4ab1 188 Gled4=GledB;
xlizne01 3:94bfc4de4ab1 189 Bled3=BledA;
xlizne01 3:94bfc4de4ab1 190 Bled4=BledB;
xlizne01 3:94bfc4de4ab1 191 }
xlizne01 3:94bfc4de4ab1 192
xlizne01 3:94bfc4de4ab1 193 if(j==2)
xlizne01 3:94bfc4de4ab1 194 {
xlizne01 3:94bfc4de4ab1 195 Rled5=RledA;
xlizne01 3:94bfc4de4ab1 196 Rled6=RledB;
xlizne01 3:94bfc4de4ab1 197 Gled5=GledA;
xlizne01 3:94bfc4de4ab1 198 Gled6=GledB;
xlizne01 3:94bfc4de4ab1 199 Bled5=BledA;
xlizne01 3:94bfc4de4ab1 200 Bled6=BledB;
xlizne01 3:94bfc4de4ab1 201 }
xlizne01 3:94bfc4de4ab1 202
xlizne01 3:94bfc4de4ab1 203 if(j==3)
xlizne01 3:94bfc4de4ab1 204 {
xlizne01 3:94bfc4de4ab1 205 Rled7=RledA;
xlizne01 3:94bfc4de4ab1 206 Rled8=RledB;
xlizne01 3:94bfc4de4ab1 207 Gled7=GledA;
xlizne01 3:94bfc4de4ab1 208 Gled8=GledB;
xlizne01 3:94bfc4de4ab1 209 Bled7=BledA;
xlizne01 3:94bfc4de4ab1 210 Bled8=BledB;
xlizne01 3:94bfc4de4ab1 211 }
xlizne01 3:94bfc4de4ab1 212
xlizne01 3:94bfc4de4ab1 213 wait(0.00001);
xlizne01 3:94bfc4de4ab1 214 RED--;
xlizne01 3:94bfc4de4ab1 215 BLUE--;
xlizne01 3:94bfc4de4ab1 216 GREEN--;
xlizne01 3:94bfc4de4ab1 217 }
xlizne01 3:94bfc4de4ab1 218 }
xlizne01 3:94bfc4de4ab1 219 }
xlizne01 3:94bfc4de4ab1 220 }
xlizne01 3:94bfc4de4ab1 221
xlizne01 3:94bfc4de4ab1 222 void barva(int j , int k)
xlizne01 3:94bfc4de4ab1 223 {
xlizne01 3:94bfc4de4ab1 224 RED=pole[j][k];
xlizne01 3:94bfc4de4ab1 225 GREEN=pole[j][k];
xlizne01 3:94bfc4de4ab1 226 BLUE=pole[j][k];
xlizne01 0:ae588eb31707 227 }
xlizne01 3:94bfc4de4ab1 228
xlizne01 2:3278e4fd8fc2 229
xlizne01 2:3278e4fd8fc2 230 void accelerometer()
xlizne01 2:3278e4fd8fc2 231 {
xlizne01 2:3278e4fd8fc2 232 float ax, ay, az;
xlizne01 4:4930b1cb20bd 233
xlizne01 2:3278e4fd8fc2 234 ax = acc.getAccX();
xlizne01 2:3278e4fd8fc2 235 ay = acc.getAccY();
xlizne01 2:3278e4fd8fc2 236 az = acc.getAccZ();
xlizne01 2:3278e4fd8fc2 237
xlizne01 2:3278e4fd8fc2 238 xAngle = atan( ax / (sqrt((ay)*(ay) + (az)*(az)))) * 60;
xlizne01 2:3278e4fd8fc2 239 yAngle = atan( ay / (sqrt((ax)*(ax) + (az)*(az)))) * 60;
xlizne01 2:3278e4fd8fc2 240
xlizne01 2:3278e4fd8fc2 241
xlizne01 4:4930b1cb20bd 242 if((abs(xAngle)+abs(yAngle))<15)
xlizne01 4:4930b1cb20bd 243 {
xlizne01 4:4930b1cb20bd 244 acc_dir = '0'; // nula
xlizne01 4:4930b1cb20bd 245 }
xlizne01 4:4930b1cb20bd 246
xlizne01 4:4930b1cb20bd 247
xlizne01 2:3278e4fd8fc2 248 if(abs(xAngle) >= abs(yAngle))
xlizne01 2:3278e4fd8fc2 249 {
xlizne01 2:3278e4fd8fc2 250 if(xAngle >= MOVE_ANGLE)
xlizne01 2:3278e4fd8fc2 251 {
xlizne01 2:3278e4fd8fc2 252 acc_dir = 'U'; // +X
xlizne01 2:3278e4fd8fc2 253 }
xlizne01 2:3278e4fd8fc2 254
xlizne01 2:3278e4fd8fc2 255 if(xAngle <= -MOVE_ANGLE)
xlizne01 2:3278e4fd8fc2 256 {
xlizne01 2:3278e4fd8fc2 257 acc_dir = 'D'; // -X
xlizne01 2:3278e4fd8fc2 258 }
xlizne01 2:3278e4fd8fc2 259 }
xlizne01 2:3278e4fd8fc2 260
xlizne01 2:3278e4fd8fc2 261 else
xlizne01 2:3278e4fd8fc2 262 {
xlizne01 2:3278e4fd8fc2 263 if(yAngle >= MOVE_ANGLE)
xlizne01 2:3278e4fd8fc2 264 {
xlizne01 2:3278e4fd8fc2 265 acc_dir = 'L'; // +Y
xlizne01 2:3278e4fd8fc2 266 }
xlizne01 2:3278e4fd8fc2 267
xlizne01 2:3278e4fd8fc2 268 if(yAngle <= -MOVE_ANGLE)
xlizne01 2:3278e4fd8fc2 269 {
xlizne01 2:3278e4fd8fc2 270 acc_dir = 'R'; // -Y
xlizne01 2:3278e4fd8fc2 271 }
xlizne01 2:3278e4fd8fc2 272 }
xlizne01 2:3278e4fd8fc2 273
xlizne01 3:94bfc4de4ab1 274 }