-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdice.ino
124 lines (110 loc) · 3.25 KB
/
dice.ino
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
// boilerplate code taken from: https://create.arduino.cc/projecthub/SAnwandter1/programming-8x8-led-matrix-23475a
// another clutch website: https://xantorohara.github.io/led-matrix-editor/#
#define ROW_1 2
#define ROW_2 3
#define ROW_3 4
#define ROW_4 5
#define ROW_5 6
#define ROW_6 7
#define ROW_7 8
#define ROW_8 9
#define COL_1 10
#define COL_2 11
#define COL_3 12
#define COL_4 13
#define COL_5 A0
#define COL_6 A1
#define COL_7 A2
#define COL_8 A3
const byte rows[] = {
ROW_1, ROW_2, ROW_3, ROW_4, ROW_5, ROW_6, ROW_7, ROW_8
};
const byte col[] = {
COL_1,COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8
};
byte oneFace[] = {B00000000,B00000000,B00000000,B00011000,B00011000,B00000000,B00000000,B00000000};
byte twoFace[] = {B00000000,B00000110,B00000110,B00000000,B00000000,B01100000,B01100000,B00000000};
byte threeFace[] = {B00000000,B00000110,B00000110,B00011000,B00011000,B01100000,B01100000,B00000000};
byte fourFace[] = {B00000000,B01100110,B01100110,B00000000,B00000000,B01100110,B01100110,B00000000};
byte fiveFace[] = {B00000000,B01100110,B01100110,B00011000,B00011000,B01100110,B01100110,B00000000};
byte sixFace[] = {B01100110,B01100110,B00000000,B01100110,B01100110,B00000000,B01100110,B01100110};
int buttonState = 0;
void setup()
{
// Open serial port
Serial.begin(9600);
// Set all used pins to OUTPUT
// This is very important! If the pins are set to input
// the display will be very dim.
for (byte i = 2; i <= 13; i++)
pinMode(i, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, INPUT);
}
int randomN(int last) {
int randomNum = last;
while (randomNum == last) {
randomNum = random(1,7);
}
return randomNum;
}
byte * getPattern(int number) {
switch (number) {
case 1:
return oneFace;
case 2:
return twoFace;
case 3:
return threeFace;
case 4:
return fourFace;
case 5:
return fiveFace;
case 6:
return sixFace;
}
}
void rollDie() {
delay(5);
int randomNum = 0;
for (int i = 0; i < 6; i++) {
randomNum = randomN(randomNum);
byte* pattern = getPattern(randomNum);
for (int j = 0; j < 210; j++) {
drawScreen(pattern);
}
}
byte* truePattern = getPattern(randomN(randomNum));
for (int i = 0; i < 3000; i++) {
drawScreen(truePattern);
}
}
void loop() {
buttonState = digitalRead(A4);
if (buttonState) {
rollDie();
}
}
void drawScreen(byte buffer2[])
{
// Turn on each row in series
for (byte i = 0; i < 8; i++) // count next row
{
digitalWrite(rows[i], HIGH); //initiate whole row
for (byte a = 0; a < 8; a++) // count next row
{
// if You set (~buffer2[i] >> a) then You will have positive
digitalWrite(col[a], (~buffer2[i] >> a) & 0x01); // initiate whole column
delayMicroseconds(10); // uncoment deley for diferent speed of display
//delayMicroseconds(1000);
//delay(10);
//delay(100);
digitalWrite(col[a], 1); // reset whole column
}
digitalWrite(rows[i], LOW); // reset whole row
// otherwise last row will intersect with next row
}
}