Advertisement
safwan092

Untitled

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