WongWC1998

every_ard

Apr 30th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "MAX30105.h"
  3.  
  4.  
  5.  
  6. class FilterBuBp2
  7. {
  8. public:
  9. FilterBuBp2()
  10. {
  11. for(int i=0; i <= 4; i++)
  12. v[i]=0.0;
  13. }
  14. private:
  15. float v[5];
  16. public:
  17. float step(float x) //class II
  18. {
  19. v[0] = v[1];
  20. v[1] = v[2];
  21. v[2] = v[3];
  22. v[3] = v[4];
  23. v[4] = (5.586996620231363987e-3 * x)
  24. + (-0.80080264666570755150 * v[0])
  25. + (3.37021291056280425380 * v[1])
  26. + (-5.33686791231501622690 * v[2])
  27. + (3.76742616976629829395 * v[3]);
  28. return
  29. (v[0] + v[4])
  30. - 2 * v[2];
  31. }
  32. };
  33.  
  34. FilterBuBp2 c_filter;
  35. long present, seconds, past, green, past2, present2, pastacc, presentacc, full_sum, sum;
  36. int hr_threshold=5;
  37. bool beatG;
  38. int flag;
  39.  
  40. long hrv_arr[70];
  41. int hrv_max=0;
  42. int hrv_index=0;
  43. int gyro_count=0;
  44.  
  45. MAX30105 particleSensor;
  46. const int MPU = 0x68;
  47. float GyroX, GyroY, GyroZ, GyroX2, GyroY2, GyroZ2, voltage;
  48. int movement=0;
  49. int move_arr[10];
  50.  
  51. bool smaller=false;
  52. long past_g=0;
  53.  
  54. void setup()
  55. {
  56. Serial1.begin(9600);
  57.  
  58. // Initialize sensor
  59. if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  60. {
  61. Serial1.println("MAX30105 was not found.");
  62. while (1);
  63. }
  64.  
  65. //Setup to sense a nice looking saw tooth on the plotter
  66. byte ledBrightness = 0x3F; //Options: 0=Off to 255=50mA
  67. byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
  68. byte ledMode = 3; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  69. int sampleRate = 400; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
  70. int pulseWidth = 411; //Options: 69, 118, 215, 411
  71. int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
  72.  
  73. particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
  74.  
  75. particleSensor.setPulseAmplitudeRed(0x00);
  76. particleSensor.setPulseAmplitudeIR(0x00);
  77. particleSensor.setPulseAmplitudeGreen(0xFF);
  78.  
  79. past = millis();
  80. past2 = millis();//send data every 0.5s
  81. pastacc = millis();
  82.  
  83. Wire.endTransmission(true);
  84.  
  85. Wire.beginTransmission(MPU); // Start communication with MPU6050 // MPU=0x68
  86. Wire.write(0x6B); // Talk to the register 6B
  87. Wire.write(0x00); // Make reset - place a 0 into the 6B register
  88. Wire.endTransmission(true); //end the transmission
  89.  
  90.  
  91. Wire.beginTransmission(MPU);
  92. Wire.write(0x1B);
  93. Wire.write(0x18);
  94. Wire.endTransmission(true);
  95.  
  96.  
  97. }
  98.  
  99. void loop()
  100. {
  101.  
  102.  
  103. green=c_filter.step(particleSensor.getGreen());
  104.  
  105.  
  106.  
  107. if (past_g<green) {
  108. flag++;
  109. }
  110. else {
  111. flag=0;
  112. }
  113.  
  114.  
  115.  
  116. if (flag>hr_threshold) {
  117. present=millis();
  118. if ((present-past) > 450 && (present-past) < 1000) {
  119. seconds=60000/(present-past);
  120. hrv_arr[hrv_index]=present-past;
  121. hrv_index++;
  122. /*if (hrv_max<10) {
  123. hrv_max++;
  124. }*/
  125. }
  126. past=millis();
  127.  
  128.  
  129.  
  130. if (hrv_index==10) {
  131. hrv_index=0;
  132. }
  133. }
  134.  
  135.  
  136.  
  137.  
  138. presentacc=millis();
  139. Wire.beginTransmission(MPU);
  140. Wire.write(0x43);
  141. Wire.endTransmission(false);
  142. Wire.requestFrom(MPU, 6, true); // Read 4 registers total, each axis value is stored in 2 registers
  143. GyroX = (Wire.read() << 8 | Wire.read()) / 131.0;
  144. GyroY = (Wire.read() << 8 | Wire.read()) / 131.0;
  145. GyroZ = (Wire.read() << 8 | Wire.read()) / 131.0;
  146.  
  147.  
  148. GyroX2+=GyroX*(presentacc-pastacc)/1000;
  149. GyroY2+=GyroY*(presentacc-pastacc)/1000;
  150. GyroZ2+=GyroZ*(presentacc-pastacc)/1000;
  151. pastacc=millis();
  152.  
  153. voltage = analogRead(PA3);
  154. voltage = voltage * 3.3 / 4096;
  155.  
  156. present2=millis();
  157.  
  158. if (present2-past2 > 480) {
  159.  
  160. for (int i = 0; i < 9; i++) {
  161. full_sum+=pow(hrv_arr[i]-hrv_arr[i+1],2);
  162. }
  163. sum=full_sum/9;
  164. sum=sqrt(sum);
  165.  
  166. Serial1.print("hr=");
  167. Serial1.print(seconds);
  168. Serial1.print(",sv=");
  169. Serial1.print(voltage);
  170.  
  171. move_arr[gyro_count]=(int)(GyroX2+GyroY2);
  172.  
  173. movement=0;
  174.  
  175. for (int i = 0; i < 10; i++) {
  176. movement+=move_arr[i];
  177. }
  178.  
  179. gyro_count++;
  180. if (gyro_count==10) {
  181. gyro_count=0;
  182. }
  183. GyroX2=0;
  184. GyroY2=0;
  185. GyroZ2=0;
  186. // default dry skin is around 0.5-0.6, wet skin around 0.9-1.1, threshold should be 0.7-0.8
  187.  
  188. Serial1.print(",mm=");
  189.  
  190. Serial1.print(movement);
  191.  
  192.  
  193. Serial1.print(",var=");
  194. Serial1.print(sum);
  195. Serial1.print(",");
  196. full_sum=0;
  197. sum=0;
  198.  
  199. past2=millis();
  200.  
  201.  
  202. }
  203.  
  204.  
  205.  
  206. past_g=green;
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment