-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDice.ino
More file actions
331 lines (287 loc) · 6.95 KB
/
Dice.ino
File metadata and controls
331 lines (287 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//Coordinates of the Dice points in the LED matrix
int DicePic[8][6][2] =
{
{ //empty matrix and start position:
{9,9}, //1. Point
{9,8}, //2. Point
{9,7}, //3. Point
{9,6}, //4. Point
{9,5}, //5. Point
{9,4} //6. Point
},
{ //1:
{4,4}, //1. Point
{-1,-1}, //2. Point
{-1,-1}, //3. Point
{-1,-1}, //4. Point
{-1,-1}, //5. Point
{-1,-1} //6. Point
},
{ //2:
{2,2}, //1. Point
{6,6}, //2. Point
{-1,-1}, //3. Point
{-1,-1}, //4. Point
{-1,-1}, //5. Point
{-1,-1} //6. Point
},
{ //3:
{2,6}, //1. Point
{6,2}, //2. Point
{4,4}, //3. Point
{-1,-1}, //4. Point
{-1,-1}, //5. Point
{-1,-1} //6. Point
},
{ //4:
{2,2}, //1. Point
{2,6}, //2. Point
{6,2}, //3. Point
{6,6}, //4. Point
{-1,-1}, //5. Point
{-1,-1} //6. Point
},
{ //5:
{2,2}, //1. Point
{2,6}, //2. Point
{6,2}, //3. Point
{6,6}, //4. Point
{4,4}, //5. Point
{-1,-1} //6. Point
},
{ //6:
{2,1}, //1. Point
{2,4}, //2. Point
{2,7}, //3. Point
{6,1}, //4. Point
{6,4}, //5. Point
{6,7} //6. Point
},
{ //Start:
{-1,-1}, //1. Point
{-1,-1}, //2. Point
{-1,-1}, //3. Point
{-1,-1}, //4. Point
{-1,-1}, //5. Point
{-1,-1} //6. Point
}
};
//Variables of the dice: position, direction, speed for X and Y
float DiceXpos[6];
float DiceXdir[6];
volatile byte DiceXspeed[6];
float DiceYpos[6];
float DiceYdir[6];
volatile byte DiceYspeed[6];
int DiceValue;
unsigned long timestamp;
byte Mode;
int volatile shakes;
int ShakesPerSecond;
int step;
void InterruptChecks() {
//Count Shakes
shakes=shakes+1;
Serial.println(millis());
timestamp=millis();
}
void SetSpeedX() {
if (Mode==0) {
//Speed-up dice in X
for (int i = 0; i < 6; i++) {
if (DiceXspeed[i]<255) {DiceXspeed[i]=DiceXspeed[i]+5;}
}
}
InterruptChecks();
}
void SetSpeedY() {
if (Mode==0) {
//Speed-up dice in Y
for (int i = 0; i < 6; i++) {
if (DiceYspeed[i]<255) {DiceYspeed[i]=DiceYspeed[i]+5;}
}
}
InterruptChecks();
}
void ShowLed(int x, int y, bool onoff) {
//show only, when x/y in matrix
if ((x<8) and (y<8) and (x>=0) and (y>=0)) {
if (onoff) {
matrix.drawPixel(y+12, x+4, matrix.Color333(7, 7, 7));
} else {
matrix.drawPixel(y+12, x+4, matrix.Color333(0, 0, 0));
}
}
}
void ShowDot(int x, int y, bool onoff) {
//Show or hide dice point
ShowLed(x-1, y-1, onoff);
ShowLed(x, y-1, onoff);
ShowLed(x-1, y, onoff);
ShowLed(x, y, onoff);
}
void ShowDicePic(int value) {
//Show dice
boolean done;
//move all points from current position to destination of DiceValue
for (int i = 0; i < 6; i++) {
DiceXspeed[i]=100;
DiceYspeed[i]=100;
//Calc x values
DiceXdir[i]=0;
if (int(DiceXpos[i])>DicePic[value][i][0]) {DiceXdir[i]=-1;}
else if (int(DiceXpos[i])<DicePic[value][i][0]) {DiceXdir[i]=1;}
DiceYdir[i]=0;
if (int(DiceYpos[i])>DicePic[value][i][1]) {DiceYdir[i]=-1;}
else if (int(DiceYpos[i])<DicePic[value][i][1]) {DiceYdir[i]=1;}
}
// Serial.println(value);
// Serial.println("Start moving");
//Move Points
do {
// Serial.println("Moving");
for (int i = 0; i < 6; i++) {
if (int(DiceXpos[i])!=DicePic[value][i][0]) {
DoStep(DiceXpos[i],DiceXdir[i],DiceXspeed[i],false);
}
if (int(DiceYpos[i])!=DicePic[value][i][1]) {
DoStep(DiceYpos[i],DiceYdir[i],DiceYspeed[i],false);
}
}
// lc.clearDisplay(1);
// lc.clearDisplay(2);
matrix.fillRect(0, 0, 32, 16, matrix.Color333(0, 0, 0));
for (int i = 0; i < 6; i++) {
ShowDot(int(DiceXpos[i]), int(DiceYpos[i]), true);
}
delay(30);
//Dice points are on destition position
done=true;
for (int i = 0; i < 6; i++) {
if (int(DiceXpos[i])!=DicePic[value][i][0]) {done=false;}
if (int(DiceYpos[i])!=DicePic[value][i][1]) {done=false;}
}
} while (done==false);
//Serial.println("End moving");
// lc.clearDisplay(1);
// lc.clearDisplay(2);
matrix.fillRect(0, 0, 32, 16, matrix.Color333(0, 0, 0));
for (int i = 0; i < 6; i++) {
ShowDot(DicePic[value][i][0],DicePic[value][i][1], true);
}
//Should we reset?
ShakesPerSecond = 0;
}
void DoStep(float &pos, float &dir, volatile byte &sp, bool check) {
pos=pos+float(sp)/255*dir;
if (check==true) {
if (pos>7) {
pos=7;
dir=dir*(-1);
}
if (pos<1) {
pos=1;
dir=dir*(-1);
}
}
// Velocity decreases every step
if (sp>0) {sp=sp-1;}
}
void MoveDots() {
//move dice points one step further
for (int i = 0; i < 6; i++) {
//calc new coordinates
DoStep(DiceXpos[i],DiceXdir[i],DiceXspeed[i],true);
DoStep(DiceYpos[i],DiceYdir[i],DiceYspeed[i],true);
}
//show dice points
// lc.clearDisplay(1);
// lc.clearDisplay(2);
matrix.fillRect(0, 0, 32, 16, matrix.Color333(0, 0, 0));
for (int i = 0; i < 6; i++) {
ShowDot(int(DiceXpos[i]), int(DiceYpos[i]), true);
}
}
void setupDice() {
DiceValue=0;
for (int i = 0; i < 6; i++) {
DiceXpos[i]=DicePic[7][i][0];
DiceYpos[i]=DicePic[7][i][1];
DiceXdir[i]=random(3)-1;
DiceYdir[i]=random(3)-1;
DiceXspeed[i]=random(126)+120;
DiceYspeed[i]=random(126)+120;
}
clearScreen();
timestamp=millis();
Mode=1;
ShowDicePic(6);
delay(1000);
clearScreen();
Mode=0;
Serial.begin(9600);
step=0;
shakes=0;
timerStart = millis();
}
int oldXAngle = 0;
int oldYAngle = 0;
//Loop
void playDice() {
mpu6050.update();
// Serial.print("accX : ");Serial.print(mpu6050.getAccX());
// Serial.print("\taccY : ");Serial.print(mpu6050.getAccY());
// Serial.print("\taccZ : ");Serial.println(mpu6050.getAccZ());
// int yAngle = mpu6050.getAngleY();
// int xAngle = mpu6050.getAngleX();
//
// if (abs(xAngle - oldXAngle) > 4) {
// Serial.print("angleX : ");
// Serial.print(xAngle);
// Serial.print("\tangleY : ");
// Serial.println(yAngle);
// SetSpeedX();
// }
// if (abs(yAngle - oldYAngle) > 4) {
// Serial.print("angleX : ");
// Serial.print(xAngle);
// Serial.print("\tangleY : ");
// Serial.println(yAngle);
// SetSpeedY();
// }
int xAcc = mpu6050.getAccX()*100;
int yAcc = mpu6050.getAccY()*100;
int zAcc = mpu6050.getAccZ()*100;
// Serial.print("accX : ");Serial.print(xAcc);
// Serial.print("\taccY : ");Serial.print(yAcc);
// Serial.print("\taccZ : ");Serial.println(zAcc);
if (abs(xAcc) > 5) {
SetSpeedX();
}
if (abs(yAcc) > 5) {
SetSpeedY();
}
// if ((abs(zAcc) > 140) || (abs(zAcc) < 50)) {
// game = -1;
// return;
// }
step=step+1;
if (step>20) {
//1 sec is over
step=0;
ShakesPerSecond=shakes;
shakes=0;
}
if (Mode==0) {
MoveDots();
if (millis()-timestamp>2000) {
//there is no shaking since 2 sec
Mode=1;
DiceValue=random(6)+1;
ShowDicePic(DiceValue);
}
}
if (ShakesPerSecond>5) {
Mode=0; //shaking again
}
}