-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
06N-ONE-Nixie-Clock---Classic-Nixie-Clock-PWM-Fade-In-Out-Transition.ino
577 lines (482 loc) · 15.6 KB
/
06N-ONE-Nixie-Clock---Classic-Nixie-Clock-PWM-Fade-In-Out-Transition.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
568
569
570
571
572
573
574
575
576
577
// ONE Nixie Clock by Marcin Saj https://nixietester.com
// https://github.com/marcinsaj/ONE-Nixie-Clock
//
// Classic Nixie Clock with PWM fade in/out effect + multisegment tubes transition effect
// This example demonstrates how to set new time, display (time) digits or symbols
// fade in/out effect and fade in/out backlight color effect.
//
// Hardware:
// ONE Nixie Clock Arduino Shield - https://nixietester.com/project/one-nixie-clock
// Arduino Nano - https://store.arduino.cc/arduino-nano
//
// NOTE: For Arduino Nano use 5V power settings on the clock motherboard (VCC jumper)
//
// Nixie Tube Socket - https://bit.ly/nixie-socket & https://bit.ly/NixieSocket-Project
// Nixie Power Supply module and RTC DS3231 module
// Nixie Clock require 12V, 1.5A power supply
// Schematic ONE Nixie Clock - http://bit.ly/ONE-Nixie-Clock-Schematic
// Schematic Nixie Power Supply Module - http://bit.ly/ONE-Nixie-Clock-NPS-Module
// DS3231 RTC datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
#include <Adafruit_NeoPixel.h>
// https://github.com/adafruit/Adafruit_NeoPixel
// https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-use
#include <RTClib.h>
// https://github.com/adafruit/RTClib
// RTC library declaration
RTC_DS3231 rtc;
// Choose Time Format *******************************************************
#define hourFormat 12 // 12 Hour Clock or 24 Hour Clock
// **************************************************************************
// Set fade in/out effect delay *********************************************
#define fadeDelay 8 // Best effect in range 5 - 16 milliseconds
// **************************************************************************
// Cathode poisoning prevention settings*************************************
// How often to run the cathode poisoning prevention routine
#define howOftenRoutine 1 // 0 - none, 1 - everytime,
// 2 - every second time and so on
// **************************************************************************
// NeoPixels LEDs pin
#define LED_PIN A3
// Number of NeoPixels LEDs
#define LED_COUNT 4
// Declare our NeoPixel led object:
Adafruit_NeoPixel led(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels
// Argument 2 = Arduino pin number
// Argument 3 = Pixel type flags:
// NEO_KHZ800 800 KHz bitstream for WS2812 LEDs
// NEO_GRB Pixels are wired for GRB bitstream
// Backlight color settings *************************************************
// Hour backlight color
uint32_t hour_color = led.Color(0, 0, 255); // Blue
// Minute backlight color
uint32_t minute_color = led.Color(0, 255, 0); // Green
// **************************************************************************
// Shift registers control pins
#define DIN_PIN A0
#define EN_PIN A1
#define CLK_PIN A2
// Nixie Power Supply Module control pin
#define EN_NPS_PIN 13
// PWM pin for nixie tube fade effect
#define PWM_PIN 10
// The clock has a built-in detection mechanism
// for 15 segment nixie tubes (e.g. B-7971, B-8971)
#define DETECT_PIN A6
int loopCounter = 0;
int analogDetectInput = 0;
// Serial monitor state
boolean serialState = 0;
// Bit numbers
//
// 8
// ___________
// |\ | /|
// | \ |0 / |
// 9 | 1\ | /7 | 13
// | \ | / |
// |____\|/____|
// | 2 /|\ 6 |
// | / | \ |
// 10 | / |4 \ | 12
// | /3 | 5\ |
// |/ | \|
// ¯¯¯¯¯¯¯¯¯¯¯
// 11
// /¯¯¯¯¯¯¯¯¯\
// 14
// ______________________
// | SOCKET 20A, 24A, 26A |
// ¯¯¯¯¯|¯¯¯¯¯¯¯¯¯¯|¯¯¯¯¯
// Bit notation of 15-segment tube symbols
uint16_t symbol_nixie_tube[]={
0b0011111110001000, // 0
0b0000000000010001, // 1
0b0010100101001000, // 2
0b0001100111000000, // 3
0b0011001001000100, // 4
0b0000101100100100, // 5
0b0001111101000100, // 6
0b0000000110010000, // 7
0b0011111101000100, // 8
0b0011101101000100, // 9
0b0011011101000100, // A
0b0011100101010001, // B
0b0000111100000000, // C
0b0011100100010001, // D
0b0000111100000100, // E
0b0000011100000100, // F
0b0001111101000000, // G
0b0011011001000100, // H
0b0000100100010001, // I
0b0011110000000000, // J
0b0000011010100100, // K
0b0000111000000000, // L
0b0011011010000010, // M
0b0011011000100010, // N
0b0011111100000000, // O
0b0010011101000100, // P
0b0011111100100000, // Q
0b0010011101100100, // R
0b0001101101000100, // S
0b0000000100010001, // T
0b0011111000000000, // U
0b0000011010001000, // V
0b0011011000101000, // W
0b0000000010101010, // X
0b0000000010001010, // Y
0b0000100110001000 // Z
};
uint16_t animation[]={
0b0000000000000010,
0b0000000000000001,
0b0000000110000000,
0b0010000101000000,
0b0011000100100000,
0b0111000100010000,
0b0111100100001000,
0b0111110100000100,
0b0111111100000010,
0b0111111100000001,
0b0111111110000000,
0b0111111101000000,
0b0111111100100000,
0b0111111100010000,
0b0111111100001000,
0b0111111100000100,
0b0111111100000010,
0b0111111100000001,
0b0111111010000000,
0b0101111001000000,
0b0100111000100000,
0b0000111000010000,
0b0000011000001000,
0b0000001000000100,
0b0000000000000010,
0b0000000000000000
};
uint16_t transition[]={
0b0000000000000001,
0b0000000010000000,
0b0000000001000000,
0b0000000000100000,
0b0000000000010000,
0b0000000000001000,
0b0000000000000100,
0b0000000000000010
};
uint16_t transition_mask[]={
0b0000000000000000,
0b0000000100000001,
0b0010000010000000,
0b0001000001000000,
0b0000000000100000,
0b0000100000010000,
0b0000010000001000,
0b0000001000000100
};
// Nixie tube cathode no.14 (underscore symbol)
uint16_t hour_symbol = 0b0100000000000000;
// Underscore symbol flag, hour display distinguishing feature
// for multisegment tubes
// 0 - turn off, 1 - turn on
boolean hourUnderscore = 0;
// Bit notation of 10-segment tube digits
uint16_t digit_nixie_tube[]={
0b0000000000000001, // 0
0b0000000000000010, // 1
0b0000000000000100, // 2
0b0000000000001000, // 3
0b0000000000010000, // 4
0b0000000000100000, // 5
0b0000000001000000, // 6
0b0000000010000000, // 7
0b0000000100000000, // 8
0b0000001000000000 // 9
};
/*
// Bit notation of 7-segment tube digits
// Socket no.29, MG-17G nixie tube
uint16_t digit_nixie_tube[]={
0b0000000001110111, // 0
0b0000000000100100, // 1
0b0000000001011101, // 2
0b0000000001101101, // 3
0b0000000000101110, // 4
0b0000000001101011, // 5
0b0000000001111011, // 6
0b0000000000100101, // 7
0b0000000001111111, // 8
0b0000000001101111 // 9
};
*/
void setup()
{
Serial.begin(9600);
rtc.begin();
led.begin(); // Initialize NeoPixel led object
led.show(); // Turn OFF all pixels ASAP
led.setBrightness(255); // Set brightness 0-255
delay(5000);
TCCR1B = TCCR1B & B11111000 | B00000100; // Set PWM frequency on 122.55 Hz
pinMode(EN_NPS_PIN, OUTPUT);
digitalWrite(EN_NPS_PIN, HIGH); // Turn OFF nixie power supply module
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW);
pinMode(CLK_PIN, OUTPUT);
digitalWrite(CLK_PIN, LOW);
pinMode(DIN_PIN, OUTPUT);
digitalWrite(DIN_PIN, LOW);
pinMode(PWM_PIN, OUTPUT);
digitalWrite(PWM_PIN, LOW);
Serial.println("##############################################################");
Serial.println("------------ Test Example - Classic Nixie Clock --------------");
Serial.println("---------------- If you want to set new Time -----------------");
Serial.println("----------- press ENTER for Arduino IDE up to 1.8 ------------");
Serial.println("----------- press CTRL+ENTER for Arduino IDE 2.0 -------------");
// Millis time start
unsigned long millis_time_now = millis();
unsigned long millis_time_now_2 = millis();
// Wait 10 seconds
while((millis() < millis_time_now + 10000))
{
if (millis() - millis_time_now_2 > 160)
{
Serial.print("#");
millis_time_now_2 = millis();
}
// Set serialState flag if time settings have been selected
if(Serial.available() > 0)
{
serialState = 1;
break;
}
}
Serial.println('\n');
// Clear serial buffer
while(Serial.available())
Serial.read();
if(serialState == 0)
{
// Turn on the nixie power module if settings have not been selected
digitalWrite(EN_NPS_PIN, LOW);
}
}
void loop()
{
loopCounter++;
// Set a new time if settings have been selected
if(serialState == 1)
{
SetNewTime();
serialState = 0;
// Turn ON nixie power supply module
digitalWrite(EN_NPS_PIN, LOW);
}
// Get time from RTC and display on nixie tubes
DisplayTime();
// How often to run the cathode poisoning prevention routine
if(loopCounter == howOftenRoutine)
{
CathodePoisoningPrevention();
loopCounter = 0;
}
}
void SetNewTime()
{
Serial.println("--------------- Enter the TIME without spaces ----------------");
Serial.println("--------------- in the HHMM format e.g. 0923 -----------------");
Serial.println("------- and when you are ready to send data to the RTC -------");
Serial.println("------------ press ENTER for Arduino IDE up to 1.8 -----------");
Serial.println("------------ press CTRL+ENTER for Arduino IDE 2.0 ------------");
Serial.println('\n');
// Clear serial buffer
while(Serial.available())
Serial.read();
// Wait for the values
while (!Serial.available()) {}
// Read time as an integer value
int hhmm_time = Serial.parseInt();
// Extract minutes and hours
byte timeSecond = 0;
byte timeMinute = (hhmm_time / 1) % 100;
byte timeHour = (hhmm_time / 100) % 100;
rtc.adjust(DateTime(0, 0, 0, timeHour, timeMinute, 0));
}
void DisplayTime()
{
DateTime now = rtc.now();
byte timeHour = now.hour();
byte timeFormat = hourFormat;
// Check time format and adjust
if(timeFormat == 12 && timeHour > 12) timeHour = timeHour - 12;
if(timeFormat == 12 && timeHour == 0) timeHour = 12;
byte timeMinute = now.minute();
byte timeSecond = now.second();
Serial.print("Time: ");
if(timeHour < 10) Serial.print("0");
Serial.print(timeHour);
Serial.print(":");
if(timeMinute < 10) Serial.print("0");
Serial.print(timeMinute);
Serial.print(":");
if(timeSecond < 10) Serial.print("0");
Serial.println(timeSecond);
int digit_1;
int digit_2;
// Underscore symbol turn on for multisegment tubes
hourUnderscore = 1;
// Extract individual digits
digit_1 = (timeHour / 10) % 10;
digit_2 = (timeHour / 1) % 10;
NixieDisplay(digit_1, digit_2, hour_color);
delay(400);
// Underscore symbol turn off for multisegment tubes
hourUnderscore = 0;
digit_1 = (timeMinute / 10) % 10;
digit_2 = (timeMinute / 1) % 10;
NixieDisplay(digit_1, digit_2, minute_color);
delay(2000);
}
// If a high state appears on the analog input,
// it means that a multi-segment tube socket has been inserted
bool DetectNixieTube()
{
analogDetectInput = analogRead(DETECT_PIN);
// 0 - 1024, Detecting anything above 0 means true
// 950 is for sure
if(analogDetectInput >= 950) return(true);
else return(false);
}
void NixieDisplay(uint16_t digit_1, uint16_t digit_2, uint32_t backlight_color)
{
if(DetectNixieTube() == true) ShowSymbol(digit_1, digit_2, backlight_color);
else ShowDigit(digit_1, digit_2, backlight_color);
}
// PWM fade in/out effect
void ShowDigit(uint16_t digit_1, uint16_t digit_2, uint32_t backlight_color)
{
for(int digits = 0 ; digits < 2; digits++)
{
if(digits == 0) ShiftOutData(digit_nixie_tube[digit_1]);
else ShiftOutData(digit_nixie_tube[digit_2]);
// Fade-in from min to max
for (int i = 255 ; i >= 0; i = i - 5)
{
analogWrite(PWM_PIN, i);
led.setBrightness(255 - i); // Set brightness
led.fill(backlight_color); // Fill all LEDs with a color
led.show(); // Update LEDs
// wait to see the fade in effect
delay(fadeDelay);
}
delay(400);
// Fade-out from max to min
for (int i = 0 ; i <= 255; i = i + 5)
{
analogWrite(PWM_PIN, i);
led.setBrightness(255 - i); // Set brightness
led.fill(backlight_color); // Fill all LEDs with a color
led.show(); // Update LEDs
// wait to see the fade in effect
delay(fadeDelay);
}
ClearNixieTube();
}
}
void ShowSymbol(uint16_t digit_1, uint16_t digit_2, uint32_t backlight_color)
{
uint16_t currentDigit;
currentDigit = symbol_nixie_tube[digit_1];
if(hourUnderscore == 1) currentDigit = currentDigit | 0b0100000000000000;
ShiftOutData(currentDigit);
// fade in from min to max in decrements of 5 points
for (int i = 255 ; i >= 0; i = i - 5)
{
analogWrite(PWM_PIN, i);
led.setBrightness(255 - i); // Set brightness
led.fill(backlight_color); // Fill all LEDs with a color
led.show(); // Update LEDs
// wait to see the fade in effect
delay(fadeDelay);
}
delay(800);
for(int i = 0; i < 8; i++)
{
currentDigit = currentDigit | transition[i];
currentDigit = currentDigit & ~transition_mask[i];
if(hourUnderscore == 1) currentDigit = currentDigit | 0b0100000000000000;
ShiftOutData(currentDigit);
currentDigit = currentDigit & ~transition[i];
delay(80);
}
currentDigit = symbol_nixie_tube[digit_2];
if(hourUnderscore == 1) currentDigit = currentDigit | 0b0100000000000000;
uint16_t originalDigit = currentDigit;
uint16_t newDigit = 0;
for(int i = 0; i < 8; i++)
{
currentDigit = originalDigit & transition_mask[i];
currentDigit = newDigit | currentDigit | transition[i];
if(hourUnderscore == 1) currentDigit = currentDigit | 0b0100000000000000;
ShiftOutData(currentDigit);
newDigit = currentDigit & ~transition[i];
delay(80);
}
ShiftOutData(originalDigit);
delay(800);
// fade out from max to min in increments of 5 points
for (int i = 0 ; i <= 255; i = i + 5)
{
analogWrite(PWM_PIN, i);
led.setBrightness(255 - i); // Set brightness
led.fill(backlight_color); // Fill all LEDs with a color
led.show(); // Update LEDs
// wait to see the fade in effect
delay(fadeDelay);
}
ClearNixieTube();
}
// Turn off nixie tube
void ClearNixieTube()
{
ShiftOutData(0);
}
void CathodePoisoningPrevention()
{
delay(1000);
analogWrite(PWM_PIN, 0);
// 15 cathodes nixie tube
if(DetectNixieTube() == true)
{
for(int i = 0; i < 26; i++)
{
ShiftOutData(animation[i]);
delay(80);
}
}
else // 10 cathodes nixie tube
{
for(int i = 0; i <= 3; i++)
{
for(int j = 0; j < 10; j++)
{
ShiftOutData(digit_nixie_tube[j]);
delay(80);
}
}
}
ClearNixieTube();
delay(1000);
}
void ShiftOutData(uint16_t character)
{
uint8_t first_half = character >> 8;
uint8_t second_half = character;
digitalWrite(EN_PIN, LOW);
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, first_half);
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, second_half);
// Return the latch pin high to signal chip that it
// no longer needs to listen for information
digitalWrite(EN_PIN, HIGH);
}