forked from Pruthvish-E/soldier-monitoring-system-arduino
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch_mar28a.ino
284 lines (146 loc) · 5.3 KB
/
sketch_mar28a.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
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver;
int sensor_pin = 1;
int led_pin = 13;
float temp;
int tempPin = 2;
volatile int heart_rate;
volatile int analog_data;
volatile int time_between_beats = 600;
volatile boolean pulse_signal = false;
volatile int beat[10]; //heartbeat values will be sotred in this array
volatile int peak_value = 512;
volatile int trough_value = 512;
volatile int thresh = 525;
volatile int amplitude = 100;
volatile boolean first_heartpulse = true;
volatile boolean second_heartpulse = false;
volatile unsigned long samplecounter = 0; //This counter will tell us the pulse timing
volatile unsigned long lastBeatTime = 0;
volatile int code[3];
volatile int length=0;
unsigned long previousMillies=0;
int fsrPin = 0; // the FSR and 10K pulldown are connected to a0
int fsrReading;
volatile int sum;
void setup()
{
pinMode(led_pin,OUTPUT);
Serial.begin(9600);
interruptSetup();
if (!driver.init())
Serial.println("RF init failed");
}
void loop()
{
if(millis()-previousMillies>=350){
previousMillies=millis();
temp = analogRead(tempPin);
temp = temp * 0.48828125;
fsrReading = analogRead(fsrPin);
if(fsrReading >0 && fsrReading <200){
code[length++]=0;
}
else if(fsrReading >200){
code[length++]=1;
}
if(length==3){
length=0;
int num=4;
sum=0;
for(int x=0;x<3;x++){
sum=sum+(num*code[x]);
num= num/2;
}
}
}
String space=" ";
String msg1=String(temp)+"*c "+space+heart_rate+space+fsrReading+space+12.936+space+77.533+space+sum;
int length1= msg1.length()+1;
char msg[length1];
msg1.toCharArray(msg,length1);
Serial.println(String(temp)+"*c "+space+heart_rate+space+fsrReading+space+12.936+space+77.533+space+sum);
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
}
void interruptSetup()
{
TCCR2A = 0x02; // This will disable the PWM on pin 3 and 11
OCR2A = 0X7C; // This will set the top of count to 124 for the 500Hz sample rate
TCCR2B = 0x06; // DON'T FORCE COMPARE, 256 PRESCALER
TIMSK2 = 0x02; // This will enable interrupt on match between OCR2A and Timer
sei(); // This will make sure that the global interrupts are enable
}
ISR(TIMER2_COMPA_vect)
{
cli();
analog_data = analogRead(sensor_pin);
samplecounter += 2;
int N = samplecounter - lastBeatTime;
if(analog_data < thresh && N > (time_between_beats/5)*3)
{
if (analog_data < trough_value)
{
trough_value = analog_data;
}
}
if(analog_data > thresh && analog_data > peak_value)
{
peak_value = analog_data;
}
if (N > 250)
{
if ( (analog_data > thresh) && (pulse_signal == false) && (N > (time_between_beats/5)*3) )
{
pulse_signal = true;
digitalWrite(led_pin,HIGH);
time_between_beats = samplecounter - lastBeatTime;
lastBeatTime = samplecounter;
if(second_heartpulse)
{
second_heartpulse = false;
for(int i=0; i<=9; i++)
{
beat[i] = time_between_beats; //Filling the array with the heart beat values
}
}
if(first_heartpulse)
{
first_heartpulse = false;
second_heartpulse = true;
sei();
return;
}
word runningTotal = 0;
for(int i=0; i<=8; i++)
{
beat[i] = beat[i+1];
runningTotal += beat[i];
}
beat[9] = time_between_beats;
runningTotal += beat[9];
runningTotal /= 10;
heart_rate = 60000/runningTotal;
}
}
if (analog_data < thresh && pulse_signal == true)
{
digitalWrite(led_pin,LOW);
pulse_signal = false;
amplitude = peak_value - trough_value;
thresh = amplitude/2 + trough_value;
peak_value = thresh;
trough_value = thresh;
}
if (N > 2500)
{
thresh = 512;
peak_value = 512;
trough_value = 512;
lastBeatTime = samplecounter;
first_heartpulse = true;
second_heartpulse = false;
}
sei();
}