Advertisement
safwan092

Untitled

Feb 14th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "MAX30105.h"
  3. #include "spo2_algorithm.h"
  4. MAX30105 particleSensor;
  5. #define MAX_BRIGHTNESS 255
  6. uint32_t irBuffer[100];
  7. uint32_t redBuffer[100];
  8. int32_t bufferLength;
  9. int32_t spo2;
  10. int8_t validSPO2;
  11. int32_t heartRate;
  12. int8_t validHeartRate;
  13.  
  14.  
  15. void setup() {
  16. Serial.begin(115200);
  17. initO2Sensor();
  18. }
  19.  
  20. void loop() {
  21. readO2Sensor();
  22. }
  23.  
  24.  
  25.  
  26.  
  27. void initO2Sensor() {
  28. if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
  29. Serial.println(F("MAX30105 was not found. Please check wiring/power."));
  30. while (1);
  31. }
  32. Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
  33. //while (Serial.available() == 0); //wait until user presses a key
  34. //Serial.read();
  35. byte ledBrightness = 60; //Options: 0=Off to 255=50mA
  36. byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
  37. byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  38. byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
  39. int pulseWidth = 411; //Options: 69, 118, 215, 411
  40. int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
  41. particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
  42. }
  43.  
  44.  
  45. void readO2Sensor() {
  46. bufferLength = 100;
  47. for (byte i = 0 ; i < bufferLength ; i++) {
  48. while (particleSensor.available() == false) //do we have new data?
  49. particleSensor.check(); //Check the sensor for new data
  50. redBuffer[i] = particleSensor.getRed();
  51. irBuffer[i] = particleSensor.getIR();
  52. particleSensor.nextSample(); //We're finished with this sample so move to next sample
  53. Serial.print(F("red="));
  54. Serial.print(redBuffer[i], DEC);
  55. Serial.print(F(", ir="));
  56. Serial.println(irBuffer[i], DEC);
  57. }
  58.  
  59. //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
  60. maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  61.  
  62. //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second
  63. while (1)
  64. {
  65. //dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
  66. for (byte i = 25; i < 100; i++)
  67. {
  68. redBuffer[i - 25] = redBuffer[i];
  69. irBuffer[i - 25] = irBuffer[i];
  70. }
  71.  
  72. //take 25 sets of samples before calculating the heart rate.
  73. for (byte i = 75; i < 100; i++)
  74. {
  75. while (particleSensor.available() == false) //do we have new data?
  76. particleSensor.check(); //Check the sensor for new data
  77. redBuffer[i] = particleSensor.getRed();
  78. irBuffer[i] = particleSensor.getIR();
  79. particleSensor.nextSample(); //We're finished with this sample so move to next sample
  80. //send samples and calculation result to terminal program through UART
  81. Serial.print(F("red="));
  82. Serial.print(redBuffer[i], DEC);
  83. Serial.print(F(", ir="));
  84. Serial.print(irBuffer[i], DEC);
  85. Serial.print(F(", HR="));
  86. Serial.print(heartRate, DEC);
  87. Serial.print(F(", HRvalid="));
  88. Serial.print(validHeartRate, DEC);
  89. Serial.print(F(", SPO2="));
  90. Serial.print(spo2, DEC);
  91. Serial.print(F(", SPO2Valid="));
  92. Serial.println(validSPO2, DEC);
  93. }
  94.  
  95. //After gathering 25 new samples recalculate HR and SP02
  96. maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  97. }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement