This repository has been archived by the owner on Mar 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleDice.ino
98 lines (83 loc) · 2.61 KB
/
SingleDice.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
#include <stdlib.h>
#include "./BoardLed.h"
#include "./LD5161B.h"
#define GREEN 4
#define YELLOW_LEFT 3
#define YELLOW_MIDDLE 2
#define YELLOW_RIGHT 1
#define RED 0
#define BUTTON 8
BoardLed *leds;
LD5161B redDisplay;
bool isActive;
bool isDone;
unsigned long beginTime;
unsigned short redNumber;
void setup() {
leds = new BoardLed[5];
redDisplay = LD5161B(0x20);
// Leds are connected with pins 9 - 13.
for (int i = 0; i < 5; ++i) {
leds[i] = BoardLed(i + 9);
}
pinMode(BUTTON, INPUT_PULLUP);
randomSeed(analogRead(0));
}
void loop() {
// Waits for a new process.
if (!isActive && digitalRead(BUTTON) == LOW) {
redDisplay.off();
redNumber = 0;
isActive = true;
isDone = false;
beginTime = millis();
}
// Numbers are generated. Just display them.
if (isDone) {
redDisplay.show(redNumber);
}
// The process has been began.
if (isActive) {
unsigned long timeDifferent = millis() - beginTime;
// After 3 secs. until the began of process, display generated numbers.
if (timeDifferent > 3000) {
// And switch off loader.
for (int i = 0; i < 5; ++i) {
leds[i].off();
}
isDone = true;
isActive = false;
}
// LEDs loader...
if (timeDifferent < 3000) {
unsigned short secondModulo = timeDifferent % 1000;
// Generate the red number.
if (!redNumber && timeDifferent > 1000 && timeDifferent < 1500) {
redNumber = random(0, 6) + 1;
}
if (secondModulo >= 0 && secondModulo < 100) { // [X][ ][ ][ ][ ]
leds[GREEN].on();
} else if (secondModulo >= 100 && secondModulo < 200) { // [X][X][ ][ ][ ]
leds[YELLOW_LEFT].on();
} else if (secondModulo >= 200 && secondModulo < 300) { // [X][X][X][ ][ ]
leds[YELLOW_MIDDLE].on();
} else if (secondModulo >= 300 && secondModulo < 400) { // [X][X][X][X][ ]
leds[YELLOW_RIGHT].on();
} else if (secondModulo >= 400 && secondModulo < 500) { // [X][X][X][X][X]
leds[RED].on();
} else if (secondModulo >= 500 && secondModulo < 600) { // [X][X][X][X][ ]
leds[RED].off();
} else if (secondModulo >= 600 && secondModulo < 700) { // [X][X][X][ ][ ]
leds[YELLOW_RIGHT].off();
} else if (secondModulo >= 700 && secondModulo < 800) { // [X][X][ ][ ][ ]
leds[YELLOW_MIDDLE].off();
} else if (secondModulo >= 800 && secondModulo < 900) { // [X][ ][ ][ ][ ]
leds[YELLOW_LEFT].off();
} else { // [ ][ ][ ][ ][ ]
leds[GREEN].off();
}
}
}
// Delay all the actions 75ms.
delay(75);
}