-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard_dealer.ino
More file actions
144 lines (118 loc) · 3.64 KB
/
card_dealer.ino
File metadata and controls
144 lines (118 loc) · 3.64 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
#include <Stepper.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// pins
const byte buttonPin = 2;
const byte potPin = A3;
// steppers
const int stepsPerRevolution = 2038;
const int stepsPerCard = -1700;
const int stepsResetCard = 300;
Stepper spinStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
Stepper cardStepper = Stepper(stepsPerRevolution, 4, 6, 5, 7);
// oled
const int displayAddress = 0x3C;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
// vars
int players;
const int playersMin = 1;
const int playersMax = 15;
int cardsPerPlayer;
const int cardsMin = 1;
const int cardsMax = 30;
const int potMin = 1;
const int potMax = 30;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(potPin, INPUT);
spinStepper.setSpeed(15);
cardStepper.setSpeed(15);
display.begin(SSD1306_SWITCHCAPVCC, displayAddress);
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
// select players count
while(digitalRead(buttonPin)) {
players = 1023-analogRead(potPin);
players = map(players, 0, 1023, potMin, potMax);
if(players > playersMax) {
players = playersMax;
}
if(players < playersMin) {
players = playersMin;
}
display.clearDisplay();
display.setCursor(0, 0);
display.print("Select\nplayers:");
display.setCursor(52, 40);
display.print(players);
display.drawRect(49, 37, 30, 20, SSD1306_WHITE);
display.fillTriangle(40, 37, 30, 47, 40, 57, SSD1306_WHITE);
display.fillTriangle(87, 37, 97, 47, 87, 57, SSD1306_WHITE);
display.display();
}
while(!digitalRead(buttonPin)) {} // wait for button release
delay(50);
// select cards per player count
while(digitalRead(buttonPin)) {
cardsPerPlayer = 1023-analogRead(potPin);
cardsPerPlayer = map(cardsPerPlayer, 0, 1023, potMin, potMax);
if(cardsPerPlayer > cardsMax) {
cardsPerPlayer = cardsMax;
}
if(cardsPerPlayer < cardsMin) {
cardsPerPlayer = cardsMin;
}
display.clearDisplay();
display.setCursor(0, 0);
display.print("Select\ncards:");
display.setCursor(52, 40);
display.print(cardsPerPlayer);
display.drawRect(49, 37, 30, 20, SSD1306_WHITE);
display.fillTriangle(40, 37, 30, 47, 40, 57, SSD1306_WHITE);
display.fillTriangle(87, 37, 97, 47, 87, 57, SSD1306_WHITE);
display.display();
}
unsigned long start = millis();
while(millis()-start < 3000) {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Starting!");
displayProgressBar(float(millis()-start)/3000, 2, 30, 124, 10);
display.display();
}
start = millis();
for(int p=0; p < players; p++) {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Dealing\ncards to\nplayer #");
display.print(p);
display.print(" / ");
display.print(players);
display.display();
for(int c=0;c<cardsPerPlayer;c++) {
cardStepper.step(stepsPerCard); // deal cards
cardStepper.step(stepsResetCard);
}
if(p<players-1) {
spinStepper.step(stepsPerRevolution*1.1 / players); // spin for next player
}
}
display.clearDisplay();
display.setCursor(0, 0);
display.print("Done!\n\nFinished\nin ");
display.print((millis()-start)/1000);
display.print(" s");
display.display();
while(digitalRead(buttonPin)) {} // wait for button press
while(!digitalRead(buttonPin)) {} // wait for button release
delay(50);
}
void displayProgressBar(float progress, int x, int y, int w, int h) {
display.drawRect(x, y, w, h, SSD1306_WHITE);
display.fillRect(x+3, y+3, ceil(w*progress)-6, h-6, SSD1306_WHITE);
}