rautanand03

Untitled

Mar 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. // Temperature Library
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4.  
  5. // Data wire is conntec to the Arduino digital pin 2
  6. #define ONE_WIRE_BUS 2
  7.  
  8. // Setup a oneWire instance to communicate with any OneWire devices
  9. OneWire oneWire(ONE_WIRE_BUS);
  10.  
  11. // Pass our oneWire reference to Dallas Temperature sensor
  12. DallasTemperature sensors(&oneWire);
  13.  
  14. //Variables
  15. int pulsePin = A0; // Pulse Sensor purple wire connected to analog pin A0
  16. int blinkPin = 13; // pin to blink led at each beat
  17.  
  18. // Volatile Variables, used in the interrupt service routine!
  19. volatile int BPM; // int that holds raw Analog in 0. updated every 2mS
  20. volatile int Signal; // holds the incoming raw data
  21. volatile int IBI = 600; // int that holds the time interval between beats! Must be seeded!
  22. volatile boolean Pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat".
  23. volatile boolean QS = false; // becomes true when Arduoino finds a beat.
  24.  
  25. static boolean serialVisual = true; // Set to 'false' by Default. Re-set to 'true' to see Arduino Serial Monitor ASCII Visual Pulse
  26.  
  27. volatile int rate[10]; // array to hold last ten IBI values
  28. volatile unsigned long sampleCounter = 0; // used to determine pulse timing
  29. volatile unsigned long lastBeatTime = 0; // used to find IBI
  30. volatile int P = 512; // used to find peak in pulse wave, seeded
  31. volatile int T = 512; // used to find trough in pulse wave, seeded
  32. volatile int thresh = 525; // used to find instant moment of heart beat, seeded
  33. volatile int amp = 100; // used to hold amplitude of pulse waveform, seeded
  34. volatile boolean firstBeat = true; // used to seed rate array so we startup with reasonable BPM
  35. volatile boolean secondBeat = false; // used to seed rate array so we startup with reasonable BPM
  36.  
  37. void setup()
  38.  
  39. {
  40. Serial.begin(115200); //Begin serial communication ss.begin(9600);
  41. Serial.println("Patient Monitoring System "); //Print a message
  42. sensors.begin();
  43. interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
  44. // IF YOU ARE POWERING The Pulse Sensor AT VOLTAGE LESS THAN THE BOARD VOLTAGE,
  45. // UN-COMMENT THE NEXT LINE AND APPLY THAT VOLTAGE TO THE A-REF PIN
  46. // analogReference(EXTERNAL);
  47.  
  48. pinMode(10, INPUT); // Setup for leads off detection LO +
  49. pinMode(11, INPUT); // Setup for leads off detection LO -
  50. }
  51.  
  52. void loop()
  53. {
  54. int BodyTemperature, MyBPM , MyECG;
  55.  
  56. //Calling Functions
  57. BodyTemperature = temperature();
  58. Serial.println("Body Temperature ");
  59. Serial.println(BodyTemperature);
  60. MyBPM = HeartRate();
  61. Serial.println("Pulse Rate (BPM) ");
  62. Serial.println(MyBPM);
  63. MyECG = ECG();
  64. Serial.println("ECG");
  65. Serial.println(MyECG);
  66. }
  67.  
  68. int temperature ()
  69. {
  70. // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  71. sensors.requestTemperatures();
  72.  
  73. Serial.print("Celsius temperature: ");
  74. // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
  75. int temp = sensors.getTempCByIndex(0);
  76. Serial.print(temp);
  77.  
  78. return temp;
  79. }
  80.  
  81. int HeartRate()
  82. {
  83. serialOutput();
  84.  
  85. if (QS == true) // A Heartbeat Was Found
  86. {
  87. // BPM and IBI have been Determined
  88. // Quantified Self "QS" true when arduino finds a heartbeat
  89. serialOutputWhenBeatHappens(); // A Beat Happened, Output that to serial.
  90. QS = false; // reset the Quantified Self flag for next time
  91. }
  92.  
  93. delay(20);
  94.  
  95. void interruptSetup()
  96. {
  97. // Initializes Timer2 to throw an interrupt every 2mS.
  98. TCCR2A = 0x02; // DISABLE PWM ON DIGITAL PINS 3 AND 11, AND GO INTO CTC MODE
  99. TCCR2B = 0x06; // DON'T FORCE COMPARE, 256 PRESCALER
  100. OCR2A = 0X7C; // SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE
  101. TIMSK2 = 0x02; // ENABLE INTERRUPT ON MATCH BETWEEN TIMER2 AND OCR2A
  102. sei(); // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
  103. }
  104.  
  105. void serialOutput()
  106. { // Decide How To Output Serial.
  107. if (serialVisual == true)
  108. {
  109. arduinoSerialMonitorVisual('-', Signal); // goes to function that makes Serial Monitor Visualizer
  110. }
  111. else
  112. {
  113. sendDataToSerial('S', Signal); // goes to sendDataToSerial function
  114. }
  115. }
  116.  
  117. void serialOutputWhenBeatHappens()
  118. {
  119. if (serialVisual == true) // Code to Make the Serial Monitor Visualizer Work
  120. {
  121. Serial.print(" Heart-Beat Found "); //ASCII Art Madness
  122. Serial.print("BPM: ");
  123. Serial.println(BPM);
  124. }
  125. else
  126. {
  127. sendDataToSerial('B',BPM); // send heart rate with a 'B' prefix
  128. sendDataToSerial('Q',IBI); // send time between beats with a 'Q' prefix
  129. }
  130. }
  131.  
  132. void arduinoSerialMonitorVisual(char symbol, int data )
  133. {
  134. const int sensorMin = 0; // sensor minimum, discovered through experiment
  135. const int sensorMax = 1024; // sensor maximum, discovered through experiment
  136. int sensorReading = data; // map the sensor range to a range of 12 options:
  137. int range = map(sensorReading, sensorMin, sensorMax, 0, 11);
  138. // do something different depending on the
  139. // range value:
  140. }
  141. void sendDataToSerial(char symbol, int data )
  142. {
  143. Serial.print(symbol);
  144. Serial.println(data);
  145. }
  146.  
  147. ISR(TIMER2_COMPA_vect) //triggered when Timer2 counts to 124
  148. {
  149. cli(); // disable interrupts while we do this
  150. Signal = analogRead(pulsePin); // read the Pulse Sensor
  151. sampleCounter += 2; // keep track of the time in mS with this variable
  152. int N = sampleCounter - lastBeatTime; // monitor the time since the last beat to avoid noise
  153. // find the peak and trough of the pulse wave
  154. if(Signal < thresh && N > (IBI/5)*3) // avoid dichrotic noise by waiting 3/5 of last IBI
  155. {
  156. if (Signal < T) // T is the trough
  157. {
  158. T = Signal; // keep track of lowest point in pulse wave
  159. }
  160. }
  161.  
  162. if(Signal > thresh && Signal > P)
  163. { // thresh condition helps avoid noise
  164. P = Signal; // P is the peak
  165. } // keep track of highest point in pulse wave
  166.  
  167. // NOW IT'S TIME TO LOOK FOR THE HEART BEAT
  168. // signal surges up in value every time there is a pulse
  169. if (N > 250)
  170. { // avoid high frequency noise
  171. if ( (Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3) )
  172. {
  173. Pulse = true; // set the Pulse flag when we think there is a pulse
  174. digitalWrite(blinkPin,HIGH); // turn on pin 13 LED
  175. IBI = sampleCounter - lastBeatTime; // measure time between beats in mS
  176. lastBeatTime = sampleCounter; // keep track of time for next pulse
  177.  
  178. if(secondBeat)
  179. { // if this is the second beat, if secondBeat == TRUE
  180. secondBeat = false; // clear secondBeat flag
  181. for(int i=0; i<=9; i++) // seed the running total to get a realisitic BPM at startup
  182. {
  183. rate[i] = IBI;
  184. }
  185. }
  186.  
  187. if(firstBeat) // if it's the first time we found a beat, if firstBeat == TRUE
  188. {
  189. firstBeat = false; // clear firstBeat flag
  190. secondBeat = true; // set the second beat flag
  191. sei(); // enable interrupts again
  192. return; // IBI value is unreliable so discard it
  193. }
  194. // keep a running total of the last 10 IBI values
  195. word runningTotal = 0; // clear the runningTotal variable
  196.  
  197. for(int i=0; i<=8; i++)
  198. { // shift data in the rate array
  199. rate[i] = rate[i+1]; // and drop the oldest IBI value
  200. runningTotal += rate[i]; // add up the 9 oldest IBI values
  201. }
  202.  
  203. rate[9] = IBI; // add the latest IBI to the rate array
  204. runningTotal += rate[9]; // add the latest IBI to runningTotal
  205. runningTotal /= 10; // average the last 10 IBI values
  206. BPM = 60000/runningTotal; // how many beats can fit into a minute? that's BPM!
  207. QS = true; // set Quantified Self flag
  208. // QS FLAG IS NOT CLEARED INSIDE THIS ISR
  209. }
  210. }
  211.  
  212. if (Signal < thresh && Pulse == true)
  213. { // when the values are going down, the beat is over
  214. digitalWrite(blinkPin,LOW); // turn off pin 13 LED
  215. Pulse = false; // reset the Pulse flag so we can do it again
  216. amp = P - T; // get amplitude of the pulse wave
  217. thresh = amp/2 + T; // set thresh at 50% of the amplitude
  218. P = thresh; // reset these for next time
  219. T = thresh;
  220. }
  221.  
  222. if (N > 2500)
  223. { // if 2.5 seconds go by without a beat
  224. thresh = 512; // set thresh default
  225. P = 512; // set P default
  226. T = 512; // set T default
  227. lastBeatTime = sampleCounter; // bring the lastBeatTime up to date
  228. firstBeat = true; // set these to avoid noise
  229. secondBeat = false; // when we get the heartbeat back
  230. }
  231.  
  232. sei(); // enable interrupts when youre done!
  233. }// end isr
  234.  
  235. return BPM;
  236. }
  237.  
  238. float ECG()
  239. {
  240. if((digitalRead(10) == 1)||(digitalRead(11) == 1))
  241. {
  242. Serial.println('!');
  243. }
  244. else
  245. {
  246. // send the value of analog input 0:
  247. ecg = analogRead(A0);
  248. Serial.println(ecg);
  249. }
  250. //Wait for a bit to keep serial data from saturating
  251. delay(1);
  252.  
  253. return ecg;
  254. }
Add Comment
Please, Sign In to add comment