-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDekatronClock.ino
567 lines (468 loc) · 11.4 KB
/
DekatronClock.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#define USE_RTC
#include <Arduino.h>
#ifdef USE_RTC
#include "RTCLib.h"
#endif
#include "EEPROM.h"
#define LED_PIN 13 // Test LED
#define GUIDE_1_PIN 5 // Guide 1 - G1 pin of 2-guide Dekatron
#define GUIDE_2_PIN 6 // Guide 2 - G2 pin of 2-guide Dekatron
#define INDEX_PIN 7 // INDEX_PIN - NDX input pin. High when glow at K0
#define pinButton 10
#define encoderA 8
#define encoderB 9
struct Button {
byte buttonPin;
int lastButtonValue = 0;
unsigned long changeTime = 0;
Button(byte pin) : buttonPin(pin) {
}
boolean state() {
int buttonValue = digitalRead(buttonPin);
unsigned long now = millis();
// Has to stay at that value for 50ms
if (buttonValue != lastButtonValue) {
if (now - changeTime > 50)
lastButtonValue = buttonValue;
changeTime = now;
}
return lastButtonValue;
}
boolean wasPressed = false;
boolean clicked() {
boolean nowPressed = state();
if (!wasPressed && nowPressed) {
wasPressed = true;
return true;
}
wasPressed = nowPressed;
return false;
}
};
struct RotaryEncoder {
byte pinA;
byte pinB;
int lastModeA;
int lastModeB;
int curModeA;
int curModeB;
int curPos;
int lastPos;
RotaryEncoder(byte aPin, byte bPin) :
pinA(aPin),
pinB(bPin),
lastModeA(LOW),
lastModeB(LOW),
curModeA(LOW),
curModeB(LOW),
curPos(0),
lastPos(0)
{
}
int getRotation() {
// read the current state of the current encoder's pins
curModeA = digitalRead(pinA);
curModeB = digitalRead(pinB);
if ((lastModeA == LOW) && (curModeA == HIGH)) {
if (curModeB == LOW) {
curPos--;
} else {
curPos++;
}
}
lastModeA = curModeA;
int rotation = curPos - lastPos;
lastPos = curPos;
return rotation;
}
};
int BinarySearch (byte a[], int low, int high, int key)
{
int mid;
if (low == high)
return low;
mid = low + ((high - low) / 2);
if (key > a[mid])
return BinarySearch (a, mid + 1, high, key);
else if (key < a[mid])
return BinarySearch (a, low, mid, key);
return mid;
}
void BinaryInsertionSort (byte a[], unsigned int b[], int n)
{
int ins, i;
byte tmpA;
unsigned int tmpB;
for (i = 1; i < n; i++) {
ins = BinarySearch (a, 0, i, a[i]);
if (ins < i) {
tmpB = b[i];
memmove (b + ins + 1, b + ins, sizeof (unsigned int) * (i - ins));
b[ins] = tmpB;
tmpA = a[i];
memmove (a + ins + 1, a + ins, sizeof (byte) * (i - ins));
a[ins] = tmpA;
}
}
}
/**
* A class that moves the lit pin forwards or backwards one step
*/
struct Stepper {
int cathode;
Stepper() {
cathode = 0;
}
void stepToCathode() // Dekatron Stepper
{
cli();
if (cathode == 0) // Main cathode
{
PORTD &= B10011111;
}
if (cathode == 1) // Guide 1 cathode
{
PORTD |= B00100000;
PORTD &= B10111111;
}
if (cathode == 2) // Guide 2 cathode
{
PORTD &= B11011111;
PORTD |= B01000000;
}
sei();
}
void forward() {
cathode = (cathode + 1) % 3;
stepToCathode();
}
void back() {
cathode = (cathode + 5) % 3;
stepToCathode();
}
};
/**
* Lights up the given pins. Pins are specified in an array. So for example
* you can give an array with three elements, each of which contains the number
* of a pin to light up. You can change the pins at any time, but not the number
* of pins.
*
* We use it to display a clock, but it could be used to move any (small) number of
* pins around in any way.
*/
struct PinSet {
byte *pins;
unsigned int *lingers;
int numPins;
int maxPins;
int pinIdx;
byte currPin;
byte moveTo;
boolean set;
Stepper &stepper;
unsigned long lastTick;
unsigned long lastMove;
PinSet(int maxPins, Stepper &stepper) :
pins(new byte[maxPins]),
lingers(new unsigned int[maxPins]),
numPins(0),
maxPins(maxPins),
pinIdx(0),
currPin(0),
moveTo(0),
set(false),
stepper(stepper),
lastTick(0),
lastMove(0)
{
}
/**
* Move the pin to zero - all pins will be relative to this.
*/
void zeroSet() {
if (!digitalRead(INDEX_PIN)) {
stepper.forward();
} else {
set = true;
}
}
/**
* Call periodically with the current number of microseconds.
*/
void periodic(unsigned long now) {
if (now - lastTick >= 80) { // Do this once per 80 us. Any faster and the dekatron gets confused.
lastTick = now;
if (!set) {
// Set to zero pin
zeroSet();
} else if (currPin != moveTo) {
#define CLOCKWISE
#ifdef SHORTEST
int delta = moveTo - currPin;
char step = delta > 0 ? 1 : -1;
if (abs(delta) > 15) {
step = -step;
}
if (step < 0) {
stepper.back();
}
if (step > 0) {
stepper.forward();
}
currPin = (currPin + step + 30) % 30;
#endif
#ifdef CLOCKWISE
// Always go clockwise
stepper.forward();
currPin = (currPin + 1) % 30;
#endif
}
}
// Switch between 'hands' every 7000us - seems like the slowest time with no flicker
if (now - lastMove >= lingers[pinIdx]) {
lastMove = now;
pinIdx = (pinIdx + 1) % numPins;
moveTo = pins[pinIdx];
}
}
/**
* Set the pins that should be lit and how bright they should be
*/
void setData(int numPins, byte *pins, unsigned int *lingers) {
this->numPins = numPins;
memcpy(this->lingers, lingers, sizeof (unsigned int) * numPins);
memcpy(this->pins, pins, sizeof (byte) * numPins);
BinaryInsertionSort(this->pins, this->lingers, numPins);
}
/**
* Set how long each pin will be lit
*/
void setLingers(unsigned int *lingers) {
for (int i=0; i<numPins; i++) {
this->lingers[i] = lingers[i];
}
}
};
/**
* Uses SetPins to implement a clock, with animations
*/
struct AnimatedClock {
Stepper stepper;
PinSet pinSet;
enum Mode {
plain,
trainBoth,
trainClockwise,
trainAnticlockwise,
pulseBoth,
pulseClockwise,
pulseAnticlockwise,
noSeconds,
SIZE
};
unsigned long lastSec;
unsigned long seconds;
unsigned long lastPulse;
byte inc;
Mode mode;
unsigned int lingers[9] = {
14000,
3500,
3500,
1500,
1500,
1500,
1500,
1500,
1500
};
AnimatedClock() :
pinSet(3 + 6, stepper),
lastSec(0),
seconds(0),
lastPulse(0),
inc(0),
mode(plain)
{
pinSet.setLingers(lingers);
}
/**
* Set the time in seconds. It should be the number of seconds since midnight,
* or it can be a real number of seconds if you have a RTC
*/
void set(unsigned long secs) {
seconds = secs;
lastSec = secs;
}
void incrementMinutes() {
seconds += 60;
lastSec = seconds;
}
void decrementMinutes() {
seconds -= 60;
lastSec = seconds;
}
void incrementMode() {
mode = (Mode)((((int)mode)+1) % Mode::SIZE);
}
void setMode(byte mode) {
if (mode < Mode::SIZE) {
this->mode = (Mode) mode;
}
}
byte getMode() {
return (byte)mode;
}
/**
* Should be called periodically with the current number of microseconds
*/
void periodic(unsigned long now) {
pinSet.periodic(now);
if (now - lastPulse >= 100000) {
int numPins = 2;
lastPulse = now;
if (now - lastSec >= 1000000) {
lastSec = now;
seconds++;
}
byte pins[3 + 6];
pins[0] = ((seconds / 60 / 12 + 20) % 60) / 2; // Location of hours hand
pins[1] = ((seconds / 60 + 20) % 60) / 2; // Location of minutes hand
if (mode != noSeconds) {
numPins = 3;
pins[2] = ((seconds + 20) % 60) / 2; // Location of seconds hand
if (mode != plain) {
if (mode < pulseBoth) {
byte origin = pins[1];
byte destination = pins[0];
char clockwiseDistance = destination - origin;
if (clockwiseDistance < 0) {
clockwiseDistance = 30 + clockwiseDistance;
}
for (int i=0; i<6; i++) {
byte distance = i * 5 + 1 + inc;
if (distance <= clockwiseDistance) {
if (mode == trainBoth || mode == trainClockwise) {
pins[numPins++] = (origin + distance) % 30;
}
} else {
if (mode == trainBoth || mode == trainAnticlockwise) {
pins[numPins++] = (30 + origin - (5 - i) * 5 - 1 - inc) % 30;
}
}
}
inc = (inc + 1) % 5;
} else {
byte origin = pins[1];
byte destination = pins[0];
char clockwiseDistance = destination - origin;
if (clockwiseDistance < 0) {
clockwiseDistance = 30 + clockwiseDistance;
}
char antiClockwiseDistance = origin - destination;
if (antiClockwiseDistance < 0) {
antiClockwiseDistance = 30 + antiClockwiseDistance;
}
byte distance = inc + 1;
if (distance <= clockwiseDistance) {
if (mode == pulseBoth || mode == pulseClockwise) {
pins[numPins++] = (origin + distance) % 30;
}
}
if (distance <= antiClockwiseDistance){
if (mode == pulseBoth || mode == pulseAnticlockwise) {
pins[numPins++] = (30 + origin - distance) % 30;
}
}
inc = (inc + 1) % 30;
}
}
}
pinSet.setData(numPins, pins, lingers);
}
}
};
AnimatedClock clock;
Button button(pinButton);
RotaryEncoder encoder(encoderA, encoderB);
#ifdef USE_RTC
RTC_DS3231 rtc;
#endif
bool rtcPresent = false;
unsigned long lastSetTime;
const unsigned long SET_PERIOD = 5L * 60L * 1000L; // Every 5 minutes
void setFromRTC() {
if (rtcPresent) {
#ifdef USE_RTC
DateTime time = rtc.now();
Serial.print(time.hour());
Serial.print(":");
Serial.print(time.minute());
Serial.print(":");
Serial.println(time.second());
clock.set((time.hour() % 12) * 60L * 60L + time.minute() * 60L + time.second());
#endif
}
}
void setRTC() {
if (rtcPresent) {
#ifdef USE_RTC
int hour = (clock.seconds / 3600) % 24;
int min = (clock.seconds / 60) % 60;
int sec = clock.seconds % 60;
DateTime time(2000, 1, 1, hour, min, sec);
Serial.print(time.hour());
Serial.print(":");
Serial.print(time.minute());
Serial.print(":");
Serial.println(time.second());
rtc.adjust(time);
#endif
}
}
// setup() runs once, at reset, to initialize system
void setup() {
Serial.begin(9600);
pinMode(GUIDE_1_PIN, OUTPUT);
pinMode(GUIDE_2_PIN, OUTPUT);
pinMode(INDEX_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(pinButton, INPUT_PULLUP);
pinMode(encoderA, INPUT_PULLUP);
pinMode(encoderB, INPUT_PULLUP);
byte mode = EEPROM.read(0);
if (mode >= AnimatedClock::Mode::SIZE) {
EEPROM.write(0, clock.getMode());
}
clock.setMode(EEPROM.read(0));
// Set to 08:20. Because why not?
clock.set(8L * 60L * 60L + 20L * 60L);
// If we have an RTC, use that instead
#ifdef USE_RTC
rtcPresent = rtc.begin();
#endif
Serial.print("RTC Present: ");
Serial.println(rtcPresent);
setFromRTC();
}
void loop() {
if (millis() - lastSetTime >= SET_PERIOD) {
lastSetTime = millis();
setFromRTC();
}
unsigned long now = micros();
clock.periodic(now);
if (button.clicked()) {
clock.incrementMode();
EEPROM.write(0, clock.getMode());
}
int rotation = encoder.getRotation();
if (rotation > 0) {
clock.incrementMinutes();
setRTC();
}
if (rotation < 0) {
clock.decrementMinutes();
setRTC();
}
}