-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfranzininho-dice.ino
95 lines (75 loc) · 2.65 KB
/
franzininho-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
/*
Dado eletrônico com Franzininho DIY e um módulo matriz de LED 8x8 com MAX7219
by Anderson Costa with ❤ for the Wokwi community
Visit https://wokwi.com to learn about the Wokwi Simulator
Visit https://franzininho.com.br to learn about the Franzininho
*/
#include <LedControl.h>
#include <TinyDebug.h>
#define CLK_PIN PB0
#define CS_PIN PB1
#define DIN_PIN PB2
#define BTN_PIN PB3
#define RND_PIN A0
#define MAX_SEG 1
// Define os padrões binários para cada número do dado (1 a 6) e nenhum
uint8_t dice[7][8] = {
{ B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 }, // Zero
{ B00000000, B00000000, B00000000, B00011000, B00011000, B00000000, B00000000, B00000000 }, // Um
{ B00000000, B01100000, B01100000, B00000000, B00000000, B00000110, B00000110, B00000000 }, // Dois
{ B11000000, B11000000, B00000000, B00011000, B00011000, B00000000, B00000011, B00000011 }, // Três
{ B00000000, B01100110, B01100110, B00000000, B00000000, B01100110, B01100110, B00000000 }, // Quatro
{ B11000011, B11000011, B00000000, B00011000, B00011000, B00000000, B11000011, B11000011 }, // Cinco
{ B00000000, B11011011, B11011011, B00000000, B00000000, B11011011, B11011011, B00000000 } // Seis
};
// Define os pinos da matriz de LEDs
LedControl led = LedControl(DIN_PIN, CLK_PIN, CS_PIN, MAX_SEG); // MAX7219
bool waitDice = false;
uint8_t numberDice = 0;
void setup() {
Debug.begin();
delay(100);
Debug.println("Clique no botão para jogar o dado...");
pinMode(BTN_PIN, INPUT_PULLUP);
led.shutdown(0, false);
// Para o random não repetir a sequência
randomSeed(analogRead(RND_PIN));
// Ajusta o brilho da matriz de LEDs para intensidade média
led.setIntensity(0, 7);
// Limpa o display
led.clearDisplay(0);
}
void loop() {
// Verifica o pressionamento do botão
if (!digitalRead(BTN_PIN) && !waitDice) {
waitDice = true; // Trava o botão até o final da rolagem
rollsDice(); // Rola o dado
}
}
void showDice(uint8_t number) {
// Percorre a matriz e seta o número
for (uint8_t i = 0; i <= 7; i++) {
led.setRow(0, i, dice[number][i]);
}
}
void rollsDice() {
uint8_t rollingTime = random(10, 15);
for (uint8_t i = 0; i < rollingTime; i++) {
// A variável number vai assumir um valor entre 1 e 6
numberDice = random(1, 7);
showDice(numberDice);
Debug.print(numberDice);
if (i < rollingTime - 1)
Debug.print(", ");
else
Debug.print(" ");
delay(100 + i * 10);
}
showDice(0);
delay(500);
showDice(numberDice);
delay(250);
Debug.print("=> ");
Debug.println(numberDice);
waitDice = false; // Libera o botão para jogar novamente
}