-
Notifications
You must be signed in to change notification settings - Fork 2
/
RadiationWatch.cpp
244 lines (198 loc) · 4.82 KB
/
RadiationWatch.cpp
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
//////////////////////////////////////////////////
// Radiation-Watch.org
// URL http://www.radiation-watch.org/
//////////////////////////////////////////////////
#include "Arduino.h"
#include "RadiationWatch.h"
RadiationWatch::RadiationWatch(int signPin, int noisePin) : _signPin(signPin), _noisePin(noisePin)
{
_prevTime = 0;
index = 0;
signCount = 0;
noiseCount = 0;
sON = 0;
nON = 0;
_cpm = 0;
cpmIndex = 0;
cpmIndexPrev = 0;
totalSec = 0;
totalHour = 0;
cpmTimeMSec = 0;
cpmTimeSec = 0;
}
void RadiationWatch::setup()
{
//PIN setting for Radiation Pulse
pinMode(_signPin,INPUT);
digitalWrite(_signPin,HIGH);
//PIN setting for Noise Pulse
pinMode(_noisePin,INPUT);
digitalWrite(_noisePin,HIGH);
//Initialize cpmHistory[]
for(int i = 0; i < kHistoryCount;i++ )
{
_cpmHistory[i] = 0;
}
_prevTime = millis();
}
int RadiationWatch::signPin()
{
return digitalRead(_signPin);
}
int RadiationWatch::noisePin()
{
return digitalRead(_noisePin);
}
void RadiationWatch::loop()
{
// Raw data of Radiation Pulse: Not-detected -> High, Detected -> Low
int sign = signPin();
// Raw data of Noise Pulse: Not-detected -> Low, Detected -> High
int noise = noisePin();
//Radiation Pulse normally keeps low for about 100[usec]
if(sign==0 && sON==0)
{//Deactivate Radiation Pulse counting for a while
sON = 1;
signCount++;
}else if(sign==1 && sON==1){
sON = 0;
}
//Noise Pulse normally keeps high for about 100[usec]
if(noise==1 && nON==0)
{//Deactivate Noise Pulse counting for a while
nON = 1;
noiseCount++;
}else if(noise==0 && nON==1){
nON = 0;
}
//Output readings to serial port, after 10000 loops
if(index==10000) //About 160-170 msec in Arduino Nano(ATmega328)
{
//Get current time
int currTime = millis();
//No noise detected in 10000 loops
if(noiseCount == 0)
{
//Shift an array for counting log for each 6 sec.
if( totalSec % 6 == 0 && cpmIndexPrev != totalSec)
{
cpmIndexPrev = totalSec;
cpmIndex++;
if(cpmIndex >= kHistoryCount)
{
cpmIndex = 0;
}
if(_cpmHistory[cpmIndex] > 0)
{
_cpm -= _cpmHistory[cpmIndex];
}
_cpmHistory[cpmIndex] = 0;
}
//Store count log
_cpmHistory[cpmIndex] += signCount;
//Add number of counts
_cpm += signCount;
//Get ready time for 10000 loops
cpmTimeMSec += abs(currTime - _prevTime);
//Transform from msec. to sec. (to prevent overflow)
if(cpmTimeMSec >= 1000)
{
cpmTimeMSec -= 1000;
//Add measurement time to calcurate cpm readings (max=20min.)
if( cpmTimeSec >= 20*60 )
{
cpmTimeSec = 20*60;
}else{
cpmTimeSec++;
}
//Total measurement time
totalSec++;
//Transform from sec. to hour. (to prevent overflow)
const int kSecondsInHour = 60 * 60;
if(totalSec >= kSecondsInHour)
{
totalSec -= kSecondsInHour;
totalHour++;
}
}
printStatus();
index=0;
}
//Initialization for next 10000 loops
_prevTime = currTime;
signCount = 0;
noiseCount = 0;
}
index++;
}
void RadiationWatch::printKey()
{
}
void RadiationWatch::printStatus()
{
}
boolean RadiationWatch::isAvailable()
{
return cpmTime() != 0;
}
double RadiationWatch::cpmTime()
{
return cpmTimeSec / 60.0;
}
double RadiationWatch::cpm()
{
double min = cpmTime();
if (min != 0) {
return _cpm / min;
}
else {
return 0;
}
}
static const double kAlpha = 53.032; // cpm = uSv x alpha
double RadiationWatch::uSvh()
{
return cpm() / kAlpha;
}
double RadiationWatch::uSvhError()
{
double min = cpmTime();
if (min != 0) {
return sqrt(_cpm) / min / kAlpha;
}
else {
return 0;
}
}
RadiationWatchPrinter::RadiationWatchPrinter(int signPin, int noisePin) : RadiationWatch(signPin, noisePin)
{
}
void RadiationWatchPrinter::printKey()
{
//CSV-formatting for serial output (substitute , for _)
Serial.println("hour[h]_sec[s]_count_cpm_uSv/h_uSv/hError");
}
void RadiationWatchPrinter::printStatus()
{
char msg[256]; //Message buffer for serial output
//String buffers of float values for serial output
char cpmBuff[20];
char uSvBuff[20];
char uSvdBuff[20];
//Elapsed time of measurement (max=20min.)
double min = cpmTime();
dtostrf(cpm(), -1, 3, cpmBuff);
dtostrf(uSvh(), -1, 3, uSvBuff); // uSv/h
dtostrf(uSvhError(), -1, 3, uSvdBuff); // error of uSv/h
//Create message for serial port
sprintf(msg, "%d,%d.%03d,%d,%s,%s,%s",
totalHour,totalSec,
cpmTimeMSec,
signCount,
cpmBuff,
uSvBuff,
uSvdBuff
);
//Send message to serial port
Serial.println(msg);
}