-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeiger_0.2.0.ino
More file actions
323 lines (262 loc) · 7.91 KB
/
Copy pathgeiger_0.2.0.ino
File metadata and controls
323 lines (262 loc) · 7.91 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
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
/*
* DP-5V Arduino Geiger Counter Firmware
*
* Copyright (C) 2025 Arduino-DP5V-Monitor
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
/*
============================
Hardware configuration
Аппаратная конфигурация
============================
*/
#define GEIGER_PIN 2 // Geiger counter input (interrupt)
#define BUZZER_PIN 7 // Buzzer (optional)
#define BUTTON_PIN 3 // Mode / reset button
LiquidCrystal_I2C lcd(0x27, 16, 2);
/*
============================
Measurement parameters
Параметры измерений
============================
*/
// CPS -> µSv/h conversion factor (background calibrated)
// Коэффициент CPS -> мкЗв/ч (калиброван по фону ДКГ-03Д)
const float CPS_TO_USVH = 0.34;
// B-8 control source conversion
// Пересчёт CPS в условные единицы Б-8
const float CPS_TO_B8 = 60.0;
// Sliding window duration (seconds)
// Длительность скользящего окна (сек)
#define WINDOW_SECONDS 30
// Software dead time / debounce (µs)
// Программное мёртвое время (мкс)
#define DEAD_TIME_US 20000
/*
============================
Button timing
Тайминги кнопки
============================
*/
#define DEBOUNCE_MS 200
#define LONG_PRESS_MS 1500
/*
============================
Global variables
Глобальные переменные
============================
*/
volatile unsigned long pulseCount = 0;
volatile unsigned long lastPulseTime = 0;
volatile bool beepFlag = false;
unsigned long lastSecondMillis = 0;
// Sliding window buffer
unsigned int cpsBuffer[WINDOW_SECONDS] = {0};
unsigned int bufferIndex = 0;
unsigned long lastPulseSnapshot = 0;
// Accumulated dose (µSv)
// Накопленная доза (мкЗв)
float accumulatedDose_uSv = 0.0;
// Accumulation time (seconds)
// Время накопления (секунды)
unsigned long accumulationTime_s = 0;
// Button handling
bool lastButtonState = HIGH;
unsigned long buttonPressTime = 0;
unsigned long lastButtonEvent = 0;
// Display mode
// Режим отображения
enum DisplayMode {
DISPLAY_MAIN,
DISPLAY_ACCUM
};
DisplayMode displayMode = DISPLAY_MAIN;
/*
============================
Interrupt Service Routine
Обработчик прерывания
============================
*/
void geigerISR() {
unsigned long now = micros();
// Software dead time (anti-bounce)
// Программное мёртвое время (антидребезг)
if (now - lastPulseTime > DEAD_TIME_US) {
pulseCount++;
lastPulseTime = now;
beepFlag = true;
}
}
/*
============================
Reset all measurements
Сброс всех измерений
============================
*/
void resetMeasurements() {
noInterrupts();
pulseCount = 0;
lastPulseSnapshot = 0;
interrupts();
for (unsigned int i = 0; i < WINDOW_SECONDS; i++) {
cpsBuffer[i] = 0;
}
bufferIndex = 0;
accumulatedDose_uSv = 0.0;
accumulationTime_s = 0;
}
/*
============================
Setup
============================
*/
void setup() {
pinMode(GEIGER_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(GEIGER_PIN), geigerISR, RISING);
lcd.init();
lcd.backlight();
lcd.clear();
}
/*
============================
Main loop
============================
*/
void loop() {
unsigned long nowMillis = millis();
/*
============================
Beep outside ISR
Пищалка вне прерывания
============================
*/
if (beepFlag) {
tone(BUZZER_PIN, 3000, 5);
beepFlag = false;
}
/*
============================
Button handling
Обработка кнопки
============================
*/
bool buttonState = digitalRead(BUTTON_PIN);
// Button press detected
// Обнаружено нажатие
if (buttonState == LOW && lastButtonState == HIGH &&
nowMillis - lastButtonEvent > DEBOUNCE_MS) {
buttonPressTime = nowMillis;
lastButtonEvent = nowMillis;
}
// Button release detected
// Обнаружено отпускание
if (buttonState == HIGH && lastButtonState == LOW) {
unsigned long pressDuration = nowMillis - buttonPressTime;
// Long press -> reset all measurements
// Долгое нажатие -> сброс всех измерений
if (pressDuration >= LONG_PRESS_MS) {
resetMeasurements();
}
// Short press -> switch display mode
// Короткое нажатие -> смена экрана
else {
displayMode = (displayMode == DISPLAY_MAIN) ? DISPLAY_ACCUM : DISPLAY_MAIN;
}
}
lastButtonState = buttonState;
/*
============================
Once per second processing
Обработка раз в секунду
============================
*/
if (nowMillis - lastSecondMillis >= 1000) {
lastSecondMillis += 1000;
// Safe pulse counter read
unsigned long currentCount;
noInterrupts();
currentCount = pulseCount;
interrupts();
// Pulses in last second
unsigned int pulsesThisSecond = currentCount - lastPulseSnapshot;
lastPulseSnapshot = currentCount;
// Update sliding window
cpsBuffer[bufferIndex] = pulsesThisSecond;
bufferIndex = (bufferIndex + 1) % WINDOW_SECONDS;
// Sum window pulses
unsigned long windowPulses = 0;
for (unsigned int i = 0; i < WINDOW_SECONDS; i++) {
windowPulses += cpsBuffer[i];
}
// CPS and dose rate
float cps = windowPulses / (float)WINDOW_SECONDS;
float doseRate_uSv = cps * CPS_TO_USVH;
// Accumulate dose ALWAYS
// Накопление дозы ВСЕГДА
accumulatedDose_uSv += doseRate_uSv / 3600.0;
// Accumulate time ALWAYS
// Время накопления ВСЕГДА
accumulationTime_s++;
// B-8 equivalent
unsigned long b8Value = (unsigned long)(cps * CPS_TO_B8);
// Poisson statistical error (%)
float errorPercent = 0.0;
if (windowPulses > 0) {
errorPercent = 100.0 / sqrt(windowPulses);
}
/*
============================
LCD output
Вывод на дисплей
============================
*/
if (displayMode == DISPLAY_MAIN) {
lcd.setCursor(0, 0);
lcd.print("CPS:");
lcd.print(cps, 1);
lcd.print(" B8:");
lcd.print(b8Value);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(doseRate_uSv, 2);
lcd.print("uSv ");
lcd.print(errorPercent, 1);
lcd.print("% ");
} else {
// Format accumulation time HH:MM:SS
// Форматирование времени накопления ЧЧ:ММ:СС
unsigned long hours = accumulationTime_s / 3600;
unsigned long minutes = (accumulationTime_s % 3600) / 60;
unsigned long seconds = accumulationTime_s % 60;
lcd.setCursor(0, 0);
if (hours < 10) lcd.print("0");
lcd.print(hours);
lcd.print(":");
if (minutes < 10) lcd.print("0");
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) lcd.print("0");
lcd.print(seconds);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(accumulatedDose_uSv, 4);
lcd.print(" uSv ");
}
}
}