-
Notifications
You must be signed in to change notification settings - Fork 0
/
zxcounter.ino
1674 lines (1380 loc) · 38.2 KB
/
zxcounter.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ZX Counter sketch for DIYGeigerCounter Kit
#include <stdio.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>
#define VERSION 1.7 // version of this software
// display modes
#define MODE_AUTO 0 // show stats and bar with auto interval
#define MODE_CUSTOM_PERIOD_1 1 // show stats within custom period 1
#define MODE_CUSTOM_PERIOD_2 2 // show stats within custom period 2
#define MODE_ALL 3 // show stats within the all time
#define MODE_MAX 4 // show max dose
#define MODE_DOSE 5 // show accumulated dose
#define PERIOD_100MS 100
#define PERIOD_1S 1000
#define PERIOD_5S 5000
#define PERIOD_10S 10000
#define PERIOD_REFRESH 1000 // display refresh period
#define PERIOD_BAR_REFRESH 100 // analog bar refresh period
#define PERIOD_LOG 60000 // log to serial every 60 seconds
#define PERIOD_VCC_CHECK 10000 // check VCC every 10 seconds
#define PERIOD_BUTTON_WAIT 2000 // wait until button pressed
#define PERIOD_BUTTON_CHECK 100 // button check period
#define DELAY_BLINK 200 // blink delay
#define DELAY_DEBOUNCE 50 // button debounce delay
#define COUNTS_1S_LEN 10 // 1 sec stats array length (10 data points per 100 milliseconds)
#define COUNTS_5S_LEN 5 // 5 sec stats array length (5 data points per second)
#define COUNTS_10S_LEN 10 // 10 sec stats array length (10 data points per second)
#define COUNTS_30S_LEN 30 // 30 sec stats array length (30 data points per second)
#define COUNTS_1M_LEN 60 // 1 min stats array length (60 data points per second)
#define COUNTS_5M_LEN 60 // 5 min stats array length (60 data points per 5 seconds)
#define COUNTS_10M_LEN 60 // 10 min stats array length (60 data points per 10 seconds)
#define CPM_LIMIT_1S 7500 // min CPM to display 1 sec stats
#define CPM_LIMIT_5S 1500 // min CPM to display 5 sec stats
#define CPM_LIMIT_10S 300 // min CPM to display 10 sec stats
#define CPM_LIMIT_30S 60 // min CPM to display 30 sec stats
#define PIN_LCD_RS 3 // LCD register select (RS) pin
#define PIN_LCD_EN 4 // LCD enable pin
#define PIN_LCD_D4 5 // LCD D4 pin
#define PIN_LCD_D5 6 // LCD D5 pin
#define PIN_LCD_D6 7 // LCD D6 pin
#define PIN_LCD_D7 8 // LCD D7 pin
#define PIN_BUTTON_MODE 10 // button to toggle mode
#define PIN_BUTTON_ALT 11 // optional button to toggle mode backwards
#define PIN_ALARM 15 // outputs HIGH when alarm triggered
#define ADDR_SETTINGS 32 // settings addr in EEPROM
// dose units
#define UNIT_SV 0 // Sieverts
#define UNIT_R 1 // Roentgens
#define RATIO_SV_TO_R 100. // Sieverts to Roentgen ratio
// custom periods
#define CUSTOM_PERIOD_1S 0
#define CUSTOM_PERIOD_5S 1
#define CUSTOM_PERIOD_10S 2
#define CUSTOM_PERIOD_30S 3
#define CUSTOM_PERIOD_1M 4
#define CUSTOM_PERIOD_5M 5
#define CUSTOM_PERIOD_10M 6
#define DEFAULT_UNIT UNIT_SV // Sieverts by default
#define DEFAULT_ALARM 1. // 1 uSv/h by default
#define DEFAULT_BAR_SCALE 0.5 // 0.5 uSv/h by default
#define DEFAULT_RATIO 175. // default CPM to uSv/h ratio for SBM-20
#define DEFAULT_CUSTOM_PERIOD_1 CUSTOM_PERIOD_10S
#define DEFAULT_CUSTOM_PERIOD_2 CUSTOM_PERIOD_5M
#define MAX_TIME 8640000 // limit time to 100 days
#define MAX_ALARM 100 // uSv/h
#define MAX_BAR_SCALE 100 // uSv/h
#define MAX_RATIO 2000 // CPM to uSv/h
#define MIN_VCC 4200 // min VCC value
#define MIN_BAR_SCALE 0.5 // 0.1 uSv/h by default
// settings
struct Settings {
byte unit;
float alarm; // uSv/h only
float ratio; // CPM to uSv/h only
byte custom_period_1;
byte custom_period_2;
float bar_scale;
} settings = {
DEFAULT_UNIT,
DEFAULT_ALARM,
DEFAULT_RATIO,
DEFAULT_CUSTOM_PERIOD_1,
DEFAULT_CUSTOM_PERIOD_2,
DEFAULT_BAR_SCALE
};
// counts within 100ms, 1 sec, 5 sec and 10 sec
volatile unsigned long count_100ms;
volatile unsigned long count_1s;
volatile unsigned long count_5s;
volatile unsigned long count_10s;
// default startup mode
byte mode = MODE_AUTO;
// conversion factor depends on unit
float factor = 1.;
// alarm enabled flag
boolean alarm_enabled = false;
// low VCC flag
boolean low_vcc = false;
// total counts
unsigned long total;
// 1 sec, 5 sec, 10 sec, 30 sec, 1 min, 5 min, 10 min data arrays
unsigned long counts_1s[COUNTS_1S_LEN];
unsigned long counts_5s[COUNTS_5S_LEN];
unsigned long counts_10s[COUNTS_10S_LEN];
unsigned long counts_30s[COUNTS_30S_LEN];
unsigned long counts_1m[COUNTS_1M_LEN];
unsigned long counts_5m[COUNTS_5M_LEN];
unsigned long counts_10m[COUNTS_10M_LEN];
// current time
unsigned long time;
// max CPM count value
unsigned long max_cpm;
// max CPM count time
unsigned long max_time;
// current VCC in mV
unsigned long vcc;
// last 100 msec, 1 sec, 5 sec, 10 sec check times
unsigned long count_100ms_time;
unsigned long count_1s_time;
unsigned long count_5s_time;
unsigned long count_10s_time;
// last display refresh time
unsigned long last_refresh;
// last bar refresh time
unsigned long last_bar_refresh;
// last log time
unsigned long last_log;
// last button check time
unsigned long last_button_time;
// last VCC check time
unsigned long last_vcc_check;
// counts array pointers
byte counts_1s_index;
byte counts_5s_index;
byte counts_10s_index;
byte counts_30s_index;
byte counts_1m_index;
byte counts_5m_index;
byte counts_10m_index;
// data ready flags
boolean counts_1s_ready;
boolean counts_5s_ready;
boolean counts_10s_ready;
boolean counts_30s_ready;
boolean counts_1m_ready;
boolean counts_5m_ready;
boolean counts_10m_ready;
// equiv dose units
char *units[] = {"Sv", "R"};
// custom periods
char *custom_periods[] = {"1s", "5s", "10s", "30s", "1m", "5m", "10m"};
// custom characters used for analog bar
// blank
byte char_bar_0[8] = {
B00000,
B00000,
B00000,
B10101,
B00000,
B00000,
B00000,
B00000
};
// 1 bar
byte char_bar_1[8] = {
B00000,
B10000,
B10000,
B10101,
B10000,
B10000,
B00000,
B00000
};
// 2 bars
byte char_bar_2[8] = {
B00000,
B11000,
B11000,
B11101,
B11000,
B11000,
B00000,
B00000
};
// 3 bars
byte char_bar_3[8] = {
B00000,
B11100,
B11100,
B11101,
B11100,
B11100,
B00000,
B00000
};
// 4 bars
byte char_bar_4[8] = {
B00000,
B11110,
B11110,
B11111,
B11110,
B11110,
B00000,
B00000
};
// 5 bars
byte char_bar_5[8] = {
B00000,
B11111,
B11111,
B11111,
B11111,
B11111,
B00000,
B00000
};
// instantiate the library and pass pins for (RS, Enable, D4, D5, D6, D7)
// default layout for the Geiger board
LiquidCrystal lcd(PIN_LCD_RS, PIN_LCD_EN, PIN_LCD_D4, PIN_LCD_D5, PIN_LCD_D6, PIN_LCD_D7);
void setup() {
// init serial
Serial.begin(9600);
// geiger event on pin 2 triggers interrupt
attachInterrupt(0, click, FALLING);
// setup buttons
pinMode(PIN_BUTTON_MODE, INPUT);
pinMode(PIN_BUTTON_ALT, INPUT);
digitalWrite(PIN_BUTTON_MODE, HIGH);
digitalWrite(PIN_BUTTON_ALT, HIGH);
// setup alarm PIN
pinMode(PIN_ALARM, OUTPUT);
// init 16x2 display
lcd.begin(16, 2);
// load 6 custom chars
lcd.createChar(0, char_bar_0);
lcd.createChar(1, char_bar_1);
lcd.createChar(2, char_bar_2);
lcd.createChar(3, char_bar_3);
lcd.createChar(4, char_bar_4);
lcd.createChar(5, char_bar_5);
// read EEPROM settings
loadSettings();
// print software version
clearDisplay();
lcd.setCursor(3, 0);
lcd.print("ZX Counter");
lcd.setCursor(2, 1);
lcd.print("Version ");
lcd.print(VERSION);
// show debug info if mode button was pressed during startup
unsigned long time = millis();
while (millis() < time + PERIOD_BUTTON_WAIT) {
if (readButton(PIN_BUTTON_ALT) == LOW || readButton(PIN_BUTTON_MODE) == LOW) {
clearDisplay();
// print avail RAM
lcd.print("RAM: ");
lcd.print(getAvailRAM());
// print VCC
long vcc = readVCC();
lcd.setCursor(0, 1);
lcd.print("VCC: ");
lcd.print(vcc / 1000., 2);
lcd.print("V");
}
}
clearDisplay();
lcd.print(" Press MODE to");
lcd.setCursor(0, 1);
lcd.print(" enter SETUP");
// if mode button pressed during startup then enter settings
time = millis();
while (millis() < time + PERIOD_BUTTON_WAIT) {
if (readButton(PIN_BUTTON_ALT) == LOW || readButton(PIN_BUTTON_MODE) == LOW) {
delay(PERIOD_BUTTON_CHECK);
// unit setting
unitSetting();
// alarm setting
alarmSetting();
// bar scale setting
barScaleSetting();
// ratio setting
ratioSetting();
// custom period 1 setting
customPeriodSetting("Custom Period 1:", &settings.custom_period_1);
// custom period 2 setting
customPeriodSetting("Custom Period 2:", &settings.custom_period_2);
// reset to default setting
resetSetting();
}
}
// print scale
printScale();
// reset counts
resetCounts();
}
void loop() {
// collect stats data
collectData();
// check button state each PERIOD_BUTTON_CHECK milliseconds
if (millis() >= last_button_time + PERIOD_BUTTON_CHECK) {
// update last button check time
last_button_time = millis();
boolean pushed = false;
// cycle modes back
if (readButton(PIN_BUTTON_ALT) == LOW) {
pushed = true;
if (mode <= MODE_AUTO) {
mode = MODE_DOSE;
} else {
mode--;
}
}
// cycle modes forward
if (readButton(PIN_BUTTON_MODE) == LOW) {
pushed = true;
if (mode >= MODE_DOSE) {
mode = MODE_AUTO;
} else {
mode++;
}
}
if (pushed) {
// redraw display and scale
printScale();
// refresh display
refreshDisplay();
delay(500);
}
}
// refresh display each PERIOD_REFRESH milliseconds
if (millis() >= last_refresh + PERIOD_REFRESH) {
// update last refresh time
last_refresh = millis();
// check if alarm must be turned on
checkAlarm();
// refresh display
refreshDisplay();
}
// fast refresh bar each PERIOD_BAR_REFRESH milliseconds
if (mode == MODE_AUTO && !low_vcc && !alarm_enabled && millis() >= last_bar_refresh + PERIOD_BAR_REFRESH) {
// update last fast refresh time
last_bar_refresh = millis();
refreshBar();
}
// log to serial each PERIOD_LOG milliseconds
if (millis() >= last_log + PERIOD_LOG) {
// update last log time
last_log = millis();
// log to serial
logStats(get1mCPS());
}
// check VCC each PERIOD_VCC_CHECK milliseconds
if (millis() >= last_vcc_check + PERIOD_VCC_CHECK) {
// update current VCC
vcc = readVCC();
// update last VCC check time
last_vcc_check = millis();
// update low VCC flag
low_vcc = (vcc <= MIN_VCC);
}
}
void refreshDisplay() {
switch (mode) {
case MODE_AUTO:
displayAutoStats();
break;
case MODE_CUSTOM_PERIOD_1:
// display custom period 1 stats
displayCustomPeriodStats(settings.custom_period_1);
break;
case MODE_CUSTOM_PERIOD_2:
// display custom period 2 stats
displayCustomPeriodStats(settings.custom_period_2);
break;
case MODE_ALL:
// display stats within all time
displayAllStats();
break;
case MODE_MAX:
// display max CPM and dose
displayMax();
break;
case MODE_DOSE:
// display total count and accumulated dose
displayDose();
break;
}
}
// refresh analog bar
void refreshBar() {
lcd.setCursor(11, 0);
printBar(getCPS() * 60. / settings.ratio * factor, settings.bar_scale * factor, 5); // max 5 chars
}
// displays auto stats
void displayAutoStats() {
byte period;
boolean ready;
float cps_5s, cpm_5s, avg_cps, avg_cpm, avg_dose;
// calculate average CPS within last 5 sec
cps_5s = get5sCPS();
// calculate average CPM within last 5 sec
cpm_5s = cps_5s * 60.;
// auto scale
if (cpm_5s > CPM_LIMIT_1S) {
period = 1;
avg_cps = getCPS(); // current CPS
ready = true;
} else if (cpm_5s > CPM_LIMIT_5S) {
period = 5;
avg_cps = cps_5s; // average CPS within 5 sec
ready = counts_5s_ready;
} else if (cpm_5s > CPM_LIMIT_10S) {
period = 10;
avg_cps = get10sCPS(); // average CPS within 10 sec
ready = counts_10s_ready;
} else if (cpm_5s > CPM_LIMIT_30S) {
period = 30;
avg_cps = get30sCPS(); // average CPS within 30 sec
ready = counts_30s_ready;
} else {
period = 60;
avg_cps = get1mCPS(); // average CPS within 1 min
ready = counts_1m_ready;
}
// convert CPS to CPM
avg_cpm = avg_cps * 60.;
// convert CPM to dose
avg_dose = avg_cpm / settings.ratio * factor;
// update CPM
lcd.setCursor(4, 0);
lcd.print(" "); // erase 6 chars after "CPM "
lcd.setCursor(4, 0);
printCPM(cpm_5s); // max 6 chars
if (low_vcc || alarm_enabled) {
lcd.setCursor(11, 0);
lcd.print(" "); // erase 5 chars
lcd.setCursor(11, 0);
delay(DELAY_BLINK);
if (low_vcc) {
lcd.print("LOWBT");
} else if (alarm_enabled) {
lcd.print("ALARM");
}
}
// update dose unit
lcd.setCursor(0, 1);
printUnit(avg_dose); // 1 char
// update dose
lcd.setCursor(6, 1);
lcd.print(" "); // erase 6 chars after unit
lcd.setCursor(6, 1);
// blink while data is not ready
if (!ready) {
delay(DELAY_BLINK);
}
printDose(avg_dose, 2); // max 6 chars
// update period
lcd.setCursor(13, 1);
printPeriod(period, false); // 3 chars
}
// display custom period stats
void displayCustomPeriodStats(byte period) {
switch (period) {
case CUSTOM_PERIOD_1S:
// display 1 sec stats
displayStats(getCPS(), counts_1s_ready, 1, false);
break;
case CUSTOM_PERIOD_5S:
// display 5 sec stats
displayStats(get5sCPS(), counts_5s_ready, 5, false);
break;
case CUSTOM_PERIOD_10S:
// display 10 sec stats
displayStats(get10sCPS(), counts_10s_ready, 10, false);
break;
case CUSTOM_PERIOD_30S:
// display 30 sec stats
displayStats(get30sCPS(), counts_30s_ready, 30, false);
break;
case CUSTOM_PERIOD_1M:
// display 1 min stats
displayStats(get1mCPS(), counts_1m_ready, 1, true);
break;
case CUSTOM_PERIOD_5M:
// display 5 min stats
displayStats(get5mCPS(), counts_5m_ready, 5, true);
break;
case CUSTOM_PERIOD_10M:
// display 10 min stats
displayStats(get10mCPS(), counts_10m_ready, 10, true);
break;
}
}
// displays stats within period
void displayStats(float cps, boolean ready, byte period, boolean minutes) {
// convert CPS to CPM
float cpm = cps * 60.;
// convert CPM to equivalent dose;
float dose = cpm / settings.ratio * factor;
// update CPM
lcd.setCursor(4, 0);
lcd.print(" "); // erase 6 chars after "CPM "
lcd.setCursor(4, 0);
printCPM(cpm); // max 6 chars
// update time
lcd.setCursor(11, 0);
printTime(time); // 5 chars
// update dose unit
lcd.setCursor(0, 1);
printUnit(dose); // 1 char
// update dose
lcd.setCursor(6, 1);
lcd.print(" "); // erase 6 chars after unit
lcd.setCursor(6, 1);
// blink while data is not ready
if (!ready) {
delay(DELAY_BLINK);
}
printDose(dose, 2); // max 6 chars
lcd.setCursor(13, 1);
printPeriod(period, minutes); // 3 chars
}
// displays stats within all time
void displayAllStats() {
float cpm = 0, dose = 0;
if (time) {
// calculate CPM
cpm = total / (time / 60.);
// convert CPM to equivalent dose;
dose = cpm / settings.ratio * factor;
}
// update CPM
lcd.setCursor(4, 0);
lcd.print(" "); // erase 6 chars after "CPM "
lcd.setCursor(4, 0);
printCPM(cpm); // max 6 chars
// update time
lcd.setCursor(11, 0);
printTime(time); // 5 chars
// update dose unit
lcd.setCursor(0, 1);
printUnit(dose); // 1 char
// update dose
lcd.setCursor(6, 1);
lcd.print(" "); // erase 6 chars after unit
lcd.setCursor(6, 1);
printDose(dose, 2); // max 6 chars
}
// displays max count and max dose
void displayMax() {
// convert CPM to equivalent dose
float max_dose = max_cpm / settings.ratio * factor;
// update CPM
lcd.setCursor(4, 0);
lcd.print(" "); // erase 6 chars after "CPM "
lcd.setCursor(4, 0);
printCPM(max_cpm); // max 6 chars
// update time
lcd.setCursor(11, 0);
printTime(max_time); // 5 chars
// update dose unit
lcd.setCursor(0, 1);
printUnit(max_dose); // 1 char
// update dose
lcd.setCursor(6, 1);
lcd.print(" "); // erase 6 chars after unit
lcd.setCursor(6, 1);
printDose(max_dose, 2); // max 6 chars
}
// displays total count and accumulated dose
void displayDose() {
float avg_cpm, avg_dose, dose = 0;
if (time) {
// calculate average CPM
avg_cpm = total / (time / 60.);
// calculate average dose
avg_dose = avg_cpm / settings.ratio * factor;
// calculate accumulated dose
dose = avg_dose * (time / 3600.);
}
// update total count
lcd.setCursor(4, 0);
lcd.print(" "); // erase 6 chars after "CNT "
lcd.setCursor(4, 0);
printCPM(total); // max 6 chars
// update time
lcd.setCursor(11, 0);
printTime(time); // 5 chars
// update dose unit
lcd.setCursor(0, 1);
printUnit(dose); // 1 char
// update dose
lcd.setCursor(4, 1);
lcd.print(" "); // erase 7 chars after unit
lcd.setCursor(4, 1);
printDose(dose, 3); // max 7 chars
}
// prints auto scaled time
void printTime(unsigned long sec) {
char str[6];
word days = sec / 86400;
word hours = (sec % 86400) / 3600;
word minutes = ((sec % 86400) % 3600) / 60;
word seconds = ((sec % 86400) % 3600) % 60;
if (days) {
sprintf(str, "%02dd%02d", days, hours);
} else if (hours) {
sprintf(str, "%02dh%02d", hours, minutes);
} else {
sprintf(str, "%02d:%02d", minutes, seconds);
}
lcd.print(str);
}
// prints auto scaled CPM
void printCPM(unsigned long cpm) {
if (cpm >= 1000000) {
lcd.print(cpm / 1000000., 1);
lcd.print("M");
} else {
lcd.print(cpm);
}
}
// prints unit
void printUnit(float dose) {
if (dose >= 500000) {
lcd.print(" ");
} else if (dose >= 500) {
lcd.print("m");
} else {
lcd.print("u");
}
}
// prints auto scaled dose
void printDose(float dose, byte base) {
if (dose >= 500000) {
lcd.print(dose / 1000000., base);
} else if (dose >= 500) {
lcd.print(dose / 1000., base);
} else {
lcd.print(dose, base);
}
}
// prints auto scaled period
void printPeriod(byte period, boolean minutes) {
if (period < 10) {
lcd.print(" ");
}
if (minutes) {
lcd.print(period);
lcd.print("m");
} else {
lcd.print(period);
lcd.print("s");
}
}
// prints analog bar
void printBar(float value, float max, byte blocks) {
if (value > max) {
value = max;
}
float scaler = max / float(blocks * 5);
byte bar_value = value / scaler;
byte full_blocks = bar_value / 5;
byte prtl_blocks = bar_value % 5;
for (byte i = 0; i < full_blocks; i++) {
lcd.write(5);
}
if (prtl_blocks) {
lcd.write(prtl_blocks);
}
for (byte i = full_blocks + prtl_blocks; i < blocks; i++) {
lcd.write(byte(0));
}
}
// prints scale
void printScale() {
clearDisplay();
switch (mode) {
case MODE_AUTO:
case MODE_CUSTOM_PERIOD_1:
case MODE_CUSTOM_PERIOD_2:
lcd.setCursor(0, 0);
lcd.print("CPM ?");
lcd.setCursor(0, 1);
if (settings.unit == UNIT_SV) {
lcd.print("uSv/h ?");
} else if (settings.unit == UNIT_R) {
lcd.print("uR/h ?");
}
break;
case MODE_ALL:
lcd.setCursor(0, 0);
lcd.print("CPM ?");
lcd.setCursor(0, 1);
if (settings.unit == UNIT_SV) {
lcd.print("uSv/h ?");
} else if (settings.unit == UNIT_R) {
lcd.print("uR/h ?");
}
lcd.setCursor(13, 1);
lcd.print("ALL");
break;
case MODE_MAX:
lcd.setCursor(0, 0);
lcd.print("CPM ?");
lcd.setCursor(0, 1);
if (settings.unit == UNIT_SV) {
lcd.print("uSv/h ?");
} else if (settings.unit == UNIT_R) {
lcd.print("uR/h ?");
}
lcd.setCursor(13, 1);
lcd.print("MAX");
break;
case MODE_DOSE:
lcd.setCursor(0, 0);
lcd.print("CNT ?");
lcd.setCursor(0, 1);
if (settings.unit == UNIT_SV) {
lcd.print("uSv ?");
} else if (settings.unit == UNIT_R) {
lcd.print("uR ?");
}
lcd.setCursor(12, 1);
lcd.print("DOSE");
break;
}
}
// log stats to serial
void logStats(float cps) {
float cpm = cps * 60.;
// convert CPM to equivalent dose;
float dose = cpm / settings.ratio * factor;
Serial.print(cpm);
Serial.print(',');
Serial.print(dose, 2);
Serial.print(',');
Serial.println(vcc / 1000.);
}
// reset counts
void resetCounts() {
for (byte i = 0; i < COUNTS_1S_LEN; i++) {
counts_1s[i] = 0;
}
for (byte i = 0; i < COUNTS_5S_LEN; i++) {
counts_5s[i] = 0;
}
for (byte i = 0; i < COUNTS_10S_LEN; i++) {
counts_10s[i] = 0;
}
for (byte i = 0; i < COUNTS_30S_LEN; i++) {
counts_30s[i] = 0;
}
for (byte i = 0; i < COUNTS_1M_LEN; i++) {
counts_1m[i] = 0;
}
for (byte i = 0; i < COUNTS_5M_LEN; i++) {
counts_5m[i] = 0;
}
for (byte i = 0; i < COUNTS_10M_LEN; i++) {
counts_10m[i] = 0;
}
// reset data ready flags
counts_1s_ready = counts_5s_ready = counts_10s_ready = counts_30s_ready = counts_1m_ready = counts_10m_ready = false;
// reset timers
last_button_time = last_refresh = last_bar_refresh = count_100ms_time = count_1s_time = count_5s_time = count_10s_time = millis();
// reset counts
time = total = max_cpm = max_time = count_100ms = count_1s = count_5s = count_10s = 0;
}
// check if alarm dose reached
void checkAlarm() {
float usv;
if (settings.alarm) {
usv = get5sCPS() * 60. / settings.ratio; // using uSv/h value for alarm
if (usv >= settings.alarm) {
setAlarm(true);
} else if (alarm_enabled) {
setAlarm(false);
}
} else if (alarm_enabled) {
setAlarm(false);
}
}
// turn alarm on or off
void setAlarm(boolean enabled) {
if (enabled) {
// turn on alarm (set alarm pin to Vcc)
digitalWrite(PIN_ALARM, HIGH);
alarm_enabled = true;
} else {
// turn off alarm (set alarm pin to Gnd)
digitalWrite(PIN_ALARM, LOW);
alarm_enabled = false;
}
}
// unit setting
void unitSetting() {
clearDisplay();
lcd.print("Unit:");
lcd.setCursor(0, 1);
lcd.print(units[settings.unit]);
byte new_unit = settings.unit;
unsigned long time = millis();
while (millis() < time + PERIOD_BUTTON_WAIT) {
if (readButton(PIN_BUTTON_ALT) == LOW || readButton(PIN_BUTTON_MODE) == LOW) {
if (new_unit == UNIT_SV) {
new_unit = UNIT_R;
} else {
new_unit = UNIT_SV;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(units[new_unit]);
time = millis();
delay(100);
}
}