Andredx

Codice

Apr 17th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.83 KB | None | 0 0
  1. //FOR DPS 310 PRESSURE SENSOR
  2. #include <ifx_dps310.h>
  3. const float sea_press = 1013.25; // DECLARING SEA LEVEL PRSSURE AS CONSTANT
  4. int alttitude = 0; // the sensor value
  5. int minimumalttitude = 100000; // minimum sensor value (changes after calibration)
  6. int maximumalttitude = 0; // maximum sensor value(changes after calibration)
  7.  
  8. // FOR EMERGRNCY BUTTON
  9. int buttonState = 0; // FOR EMERGENCY BUTTON
  10. const int buttonPin = 4; //DIGITAL PIN 4 DECLARED AS EMERGENCY PI
  11.  
  12. // FOR HEARTBEAT SENSOR
  13. int pulsePin = 0; // Pulse Sensor purple wire connected to analog pin 0
  14. unsigned long previousMillis = 0; // will store last time pulse sensor sensed
  15. const long interval = 5000; // interval at which pulse should be sensed
  16. // Volatile Variables, used in the interrupt service routine!
  17. volatile int BPM; // int that holds raw Analog in 0. updated every 2mS
  18. volatile int Signal; // holds the incoming raw data
  19. volatile int IBI = 600; // int that holds the time interval between beats! Must be seeded!
  20. volatile boolean Pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat".
  21. volatile boolean QS = false; // becomes true when Arduoino finds a beat.
  22.  
  23. // FOR BLYNK APP
  24. #define BLYNK_USE_DIRECT_CONNECT
  25. #include <SoftwareSerial.h>
  26. SoftwareSerial DebugSerial(2, 3); // RX, TX FOR HC05 BLUETOOTH MODULES
  27. #define BLYNK_PRINT DebugSerial
  28. #include <BlynkSimpleSerialBLE.h>
  29. // You should get Auth Token in the Blynk App.
  30. // Go to the Project Settings (nut icon).
  31. char auth[] = "YourAuthToken"; // GIVE YOUR BLYNK TOKEN HERE
  32.  
  33. void setup(){
  34. pinMode(buttonPin, INPUT); //emergency pi declared as input
  35. Serial.begin(9600);
  36. interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
  37. // IF YOU ARE POWERING The Pulse Sensor AT VOLTAGE LESS THAN THE BOARD VOLTAGE,
  38. // UN-COMMENT THE NEXT LINE AND APPLY THAT VOLTAGE TO THE A-REF PIN
  39. analogReference(3.3); //the voltage given to heartbeat sensor
  40. while (!Serial);
  41. Wire.begin();
  42.  
  43.  
  44. //Call begin to initialize ifxDps310
  45. //The parameter 0x76 is the bus address. The default address is 0x77 and does not need to be given.
  46. ifxDps310.begin(Wire, 0x76);
  47. //Use the commented line below instead of the one above to use the default I2C address.
  48. //if you are using the Pressure 3 click Board, you need 0x76
  49. //ifxDps310.begin(&Wire);
  50.  
  51. // IMPORTANT NOTE
  52. //If you face the issue that the DPS310 indicates a temperature around 60 C although it should be around 20 C (room temperature), you might have got an IC with a fuse bit problem
  53. //Call the following function directly after begin() to resolve this issue (needs only be called once after startup)
  54. //ifxDps310.correctTemp();
  55.  
  56. Serial.println("Init complete!");
  57. calibration(); //calling calibration function beacause we cant fix a altitude because it varies every were
  58. //so we calibrate the maximum altitude and minimum altitude for 3 seconds
  59.  
  60. DebugSerial.begin(9600);
  61.  
  62. // Blynk will work through Serial
  63. // 9600 is for HC-06. For HC-05 default speed is 38400
  64. // Do not read or write this serial manually in your sketch
  65.  
  66. Blynk.begin(Serial, auth);
  67. }
  68.  
  69.  
  70. // Where the Magic Happens
  71. void loop(){
  72. Blynk.run();
  73.  
  74. long int temperature;
  75. long int pressure;
  76. int oversampling = 7;
  77. int ret;
  78. Serial.println();
  79.  
  80. //lets the Dps310 perform a Single temperature measurement with the last (or standard) configuration
  81. //The result will be written to the paramerter temperature
  82. //ret = ifxDps310.measureTempOnce(temperature);
  83. //the commented line below does exactly the same as the one above, but you can also config the precision
  84. //oversampling can be a value from 0 to 7
  85. //the Dps 310 will perform 2^oversampling internal temperature measurements and combine them to one result with higher precision
  86. //measurements with higher precision take more time, consult datasheet for more information
  87. ret = ifxDps310.measureTempOnce(temperature, oversampling);
  88.  
  89. if (ret != 0)
  90. {
  91. //Something went wrong.
  92. //Look at the library code for more information about return codes
  93. Serial.print("FAIL! ret = ");
  94. Serial.println(ret);
  95. }
  96. else
  97. {
  98. Serial.print("Temperature: ");
  99. Serial.print(temperature);
  100. Serial.println(" degrees of Celsius");
  101. }
  102. if(temperature > 40) // CHEcking body temperatrue
  103. { //you should degin the case as per the sensor touches the skin else there is no use for this checking
  104. Serial.println("temperature rise");
  105. Blynk.email("your_email@mail.com", "subject :temperature rise is noticed ", "your randma/grandpa 's temperature has increased beyond normal value"); // provide your email id
  106. }
  107.  
  108. //Pressure measurement behaves like temperature measurement
  109. //ret = ifxDps310.measurePressureOnce(pressure);
  110. ret = ifxDps310.measurePressureOnce(pressure, oversampling);
  111. if (ret != 0)
  112. {
  113. //Something went wrong.
  114. //Look at the library code for more information about return codes
  115. Serial.print("FAIL! ret = ");
  116. Serial.println(ret);
  117. }
  118. else
  119. {
  120. Serial.print("Pressure: ");
  121. Serial.print(pressure);
  122. Serial.println(" Pascal");
  123. }
  124.  
  125. float getaltitude=(((pow((sea_press/(pressure)), 1/5.257) - 1.0)*(temperature +273.15)))/0.0065; //hypsometric formula to covert presure and temperature of a region to altitude
  126.  
  127. Serial.print(getaltitude); //print the obtained altitude
  128.  
  129. if(alttitude < (maximumalttitude - .5)) //if calibrated MAXIMUM alttitude is 3M. IF a SUDDEN FALL OCCURS WHCIH IS LESS THAN 2.5M (maximumalttitude - .5) THEN WE CAN ASSUME A FALL OCCURS
  130. { // we take a diiference of .5 meter decrement as falling situation. you can change as per your need
  131. // if you need to send the BPM rate coreesponding to this time you can add it in the email option
  132. Serial.println("alttitude drop");
  133. Blynk.email("your_email@mail.com", "subject :alttitude drop occurs ", "your randma/grandpa 's mAY fall down and required assistance"); // provide your email id
  134. }
  135.  
  136. if(alttitude > (maximumalttitude + 1)) //if calibrated MAXIMUM alttitude is 3M IF a rise occurs more than 1 METER which means cmlimbing up
  137.  
  138. {
  139.  
  140. Serial.println("alttitude rise");
  141. Blynk.email("your_email@mail.com", "subject :alttitude rise occurs ", "your randma/grandpa 's climbing up a ladder or something "); // provide your email id
  142.  
  143. }
  144.  
  145.  
  146.  
  147. unsigned long currentMillis = millis();
  148. if (currentMillis - previousMillis >= interval) // sense heart beat at every 5 second intervel you can change as per your need
  149. {
  150.  
  151. while (millis() < (currentMillis + 3000)) // calibration time you can change it as per your need
  152. {
  153.  
  154. if (QS == true){ // A Heartbeat Was Found
  155. // BPM and IBI have been Determined
  156. // Quantified Self "QS" true when arduino finds a heartbeat
  157.  
  158.  
  159. QS = false; // reset the Quantified Self flag for next time
  160. }}
  161. Serial.println(" . ");
  162. Serial.println(BPM);
  163. Serial.println(" .");
  164. //delay(20); // take a break
  165. unsigned long currentMillis = millis();
  166. previousMillis = currentMillis;
  167.  
  168. }
  169. buttonState = digitalRead(buttonPin); //reading emergency button state
  170.  
  171. if (buttonState == HIGH) //CHECKING THE EMERGENCY BUTTON STATE
  172. {
  173.  
  174. Serial.println("emergency button pressed");
  175. //if pressed emergency email is sending
  176. Blynk.email("your_email@mail.com", "subject :emergency case ", "your grandma/grandpa has pressed emergency button he/she need assistance now immediate"); // provide your email id
  177. }
  178.  
  179. if (BPM<60 || BPM>80) // CHECKING BLOOD PRESSURE
  180. { Serial.println("drastic change in blood pressure");
  181. Blynk.email("your_email@mail.com", "subject :need immediate attenion", "drastic change in blood prssure has been sensed please provide immediate assistance"); // provide your email id
  182. //you can also include the BPM rate along with the mail if you need
  183. }
  184. }
  185.  
  186. void calibration() //we canot fix a height because its changes with location so we calibrate the HEIGHT for 3 seconds
  187. {
  188. while (millis() < 3000) // calibration time you can change it as per your need
  189. {
  190.  
  191. long int temperature;
  192. long int pressure;
  193. int oversampling = 7;
  194. int ret;
  195. ret = ifxDps310.measureTempOnce(temperature, oversampling);
  196. ret = ifxDps310.measurePressureOnce(pressure, oversampling);
  197.  
  198. float getaltitude=(((pow((sea_press/(pressure/10)), 1/5.257) - 1.0)*(temperature +273.15)))/0.0065; //hypsometric formula
  199. alttitude = getaltitude;
  200.  
  201. // record the maximum altitude value
  202. if (alttitude > maximumalttitude) {
  203. maximumalttitude = alttitude;
  204. }
  205.  
  206. // record the minimum altitue value
  207. if (alttitude < minimumalttitude) {
  208. minimumalttitude = alttitude;
  209. }
  210. } Serial.println("");
  211. Serial.print(maximumalttitude);
  212. Serial.print(" -- maximumalttitude");
  213. Serial.println("");
  214. Serial.print(minimumalttitude);
  215. Serial.print(" --minimumalttitude");
  216. Serial.println("");
  217.  
  218. }
  219.  
  220. //THIS ARE INTERPTS CODE FOR PULSE SENSOR
  221.  
  222. volatile int rate[10]; // array to hold last ten IBI values
  223. volatile unsigned long sampleCounter = 0; // used to determine pulse timing
  224. volatile unsigned long lastBeatTime = 0; // used to find IBI
  225. volatile int P =512; // used to find peak in pulse wave, seeded
  226. volatile int T = 512; // used to find trough in pulse wave, seeded
  227. volatile int thresh = 530; // used to find instant moment of heart beat, seeded
  228. volatile int amp = 0; // used to hold amplitude of pulse waveform, seeded
  229. volatile boolean firstBeat = true; // used to seed rate array so we startup with reasonable BPM
  230. volatile boolean secondBeat = false; // used to seed rate array so we startup with reasonable BPM
  231.  
  232.  
  233. void interruptSetup(){ // CHECK OUT THE Timer_Interrupt_Notes TAB FOR MORE ON INTERRUPTS
  234. // Initializes Timer2 to throw an interrupt every 2mS.
  235. TCCR2A = 0x02; // DISABLE PWM ON DIGITAL PINS 3 AND 11, AND GO INTO CTC MODE
  236. TCCR2B = 0x06; // DON'T FORCE COMPARE, 256 PRESCALER
  237. OCR2A = 0X7C; // SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE
  238. TIMSK2 = 0x02; // ENABLE INTERRUPT ON MATCH BETWEEN TIMER2 AND OCR2A
  239. sei(); // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
  240. }
  241.  
  242.  
  243. // THIS IS THE TIMER 2 INTERRUPT SERVICE ROUTINE.
  244. // Timer 2 makes sure that we take a reading every 2 miliseconds
  245. ISR(TIMER2_COMPA_vect){ // triggered when Timer2 counts to 124
  246. cli(); // disable interrupts while we do this
  247. Signal = analogRead(pulsePin); // read the Pulse Sensor
  248. sampleCounter += 2; // keep track of the time in mS with this variable
  249. int N = sampleCounter - lastBeatTime; // monitor the time since the last beat to avoid noise
  250.  
  251. // find the peak and trough of the pulse wave
  252. if(Signal < thresh && N > (IBI/5)*3){ // avoid dichrotic noise by waiting 3/5 of last IBI
  253. if (Signal < T){ // T is the trough
  254. T = Signal; // keep track of lowest point in pulse wave
  255. }
  256. }
  257.  
  258. if(Signal > thresh && Signal > P){ // thresh condition helps avoid noise
  259. P = Signal; // P is the peak
  260. } // keep track of highest point in pulse wave
  261.  
  262. // NOW IT'S TIME TO LOOK FOR THE HEART BEAT
  263. // signal surges up in value every time there is a pulse
  264. if (N > 250){ // avoid high frequency noise
  265. if ( (Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3) ){
  266. Pulse = true; // set the Pulse flag when we think there is a pulse
  267.  
  268. IBI = sampleCounter - lastBeatTime; // measure time between beats in mS
  269. lastBeatTime = sampleCounter; // keep track of time for next pulse
  270.  
  271. if(secondBeat){ // if this is the second beat, if secondBeat == TRUE
  272. secondBeat = false; // clear secondBeat flag
  273. for(int i=0; i<=9; i++){ // seed the running total to get a realisitic BPM at startup
  274. rate[i] = IBI;
  275. }
  276. }
  277.  
  278. if(firstBeat){ // if it's the first time we found a beat, if firstBeat == TRUE
  279. firstBeat = false; // clear firstBeat flag
  280. secondBeat = true; // set the second beat flag
  281. sei(); // enable interrupts again
  282. return; // IBI value is unreliable so discard it
  283. }
  284.  
  285.  
  286. // keep a running total of the last 10 IBI values
  287. word runningTotal = 0; // clear the runningTotal variable
  288.  
  289. for(int i=0; i<=8; i++){ // shift data in the rate array
  290. rate[i] = rate[i+1]; // and drop the oldest IBI value
  291. runningTotal += rate[i]; // add up the 9 oldest IBI values
  292. }
  293.  
  294. rate[9] = IBI; // add the latest IBI to the rate array
  295. runningTotal += rate[9]; // add the latest IBI to runningTotal
  296. runningTotal /= 10; // average the last 10 IBI values
  297. BPM = 60000/runningTotal; // how many beats can fit into a minute? that's BPM!
  298. QS = true; // set Quantified Self flag
  299. // QS FLAG IS NOT CLEARED INSIDE THIS ISR
  300. }
  301. }
  302.  
  303. if (Signal < thresh && Pulse == true){ // when the values are going down, the beat is over
  304.  
  305. Pulse = false; // reset the Pulse flag so we can do it again
  306. amp = P - T; // get amplitude of the pulse wave
  307. thresh = amp/2 + T; // set thresh at 50% of the amplitude
  308. P = thresh; // reset these for next time
  309. T = thresh;
  310. }
  311.  
  312. if (N > 2500){ // if 2.5 seconds go by without a beat
  313. thresh = 530; // set thresh default
  314. P = 512; // set P default
  315. T = 512; // set T default
  316. lastBeatTime = sampleCounter; // bring the lastBeatTime up to date
  317. firstBeat = true; // set these to avoid noise
  318. secondBeat = false; // when we get the heartbeat back
  319. }
  320.  
  321. sei(); // enable interrupts when youre done!
  322. }// end isr
Add Comment
Please, Sign In to add comment