Advertisement
pleasedontcode

**Heart Monitor** rev_01

Jun 2nd, 2025
1,417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Heart Monitor**
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2025-06-02 22:52:41
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a heart rate and SpO2 monitoring system */
  21.     /* using MAX30100 and MAX30105 sensors.  Ensure */
  22.     /* accurate readings, efficient power management, and */
  23.     /* include a calibration routine for sensor accuracy. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <MAX30105.h>   //https://github.com/sparkfun/SparkFun_MAX3010x_Sensor_Library
  31. #include "spo2_algorithm.h" // Added for SPO2 calculations
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. /***** DEFINITION OF I2C PINS *****/
  38. const uint8_t Max30102_MAX30100_I2C_PIN_SDA_D2      = 2;
  39. const uint8_t Max30102_MAX30100_I2C_PIN_SCL_D1      = 1;
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. MAX30105 particleSensor; // Instance of the MAX30105 sensor
  43.  
  44. #define MAX_BRIGHTNESS 255
  45.  
  46. // Using uint32_t for buffer as ESP8266 has enough SRAM
  47. uint32_t irBuffer[100]; //infrared LED sensor data
  48. uint32_t redBuffer[100];  //red LED sensor data
  49.  
  50. int32_t bufferLength; //data length
  51. int32_t spo2; //SPO2 value
  52. int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
  53. int32_t heartRate; //heart rate value
  54. int8_t validHeartRate; //indicator to show if the heart rate calculation is valid
  55.  
  56. byte pulseLED = 11; //Must be on PWM pin
  57. byte readLED = 13; //Blinks with each data read
  58.  
  59. void setup(void)
  60. {
  61.   Serial.begin(115200); // initialize serial communication at 115200 bits per second:
  62.  
  63.   pinMode(pulseLED, OUTPUT);
  64.   pinMode(readLED, OUTPUT);
  65.  
  66.   // Initialize sensor
  67.   if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  68.   {
  69.     Serial.println(F("MAX30105 was not found. Please check wiring/power."));
  70.     while (1);
  71.   }
  72.  
  73.   Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
  74.   while (Serial.available() == 0) ; //wait until user presses a key
  75.   Serial.read();
  76.  
  77.   byte ledBrightness = 60; //Options: 0=Off to 255=50mA
  78.   byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
  79.   byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  80.   byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
  81.   int pulseWidth = 411; //Options: 69, 118, 215, 411
  82.   int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
  83.  
  84.   particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
  85.  
  86.   // Calibration routine for sensor accuracy
  87.   calibrateSensor();
  88. }
  89.  
  90. void loop(void)
  91. {
  92.   bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps
  93.  
  94.   //read the first 100 samples, and determine the signal range
  95.   for (byte i = 0 ; i < bufferLength ; i++)
  96.   {
  97.     while (particleSensor.available() == false) //do we have new data?
  98.       particleSensor.check(); //Check the sensor for new data
  99.  
  100.     redBuffer[i] = particleSensor.getRed();
  101.     irBuffer[i] = particleSensor.getIR();
  102.     particleSensor.nextSample(); //We're finished with this sample so move to next sample
  103.  
  104.     Serial.print(F("red="));
  105.     Serial.print(redBuffer[i], DEC);
  106.     Serial.print(F(", ir="));
  107.     Serial.println(irBuffer[i], DEC);
  108.   }
  109.  
  110.   //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
  111.   maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  112.  
  113.   //Continuously taking samples from MAX30102.  Heart rate and SpO2 are calculated every 1 second
  114.   while (1)
  115.   {
  116.     //dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
  117.     for (byte i = 25; i < 100; i++)
  118.     {
  119.       redBuffer[i - 25] = redBuffer[i];
  120.       irBuffer[i - 25] = irBuffer[i];
  121.     }
  122.  
  123.     //take 25 sets of samples before calculating the heart rate.
  124.     for (byte i = 75; i < 100; i++)
  125.     {
  126.       while (particleSensor.available() == false) //do we have new data?
  127.         particleSensor.check(); //Check the sensor for new data
  128.  
  129.       digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
  130.  
  131.       redBuffer[i] = particleSensor.getRed();
  132.       irBuffer[i] = particleSensor.getIR();
  133.       particleSensor.nextSample(); //We're finished with this sample so move to next sample
  134.  
  135.       //send samples and calculation result to terminal program through UART
  136.       Serial.print(F("red="));
  137.       Serial.print(redBuffer[i], DEC);
  138.       Serial.print(F(", ir="));
  139.       Serial.print(irBuffer[i], DEC);
  140.  
  141.       Serial.print(F(", HR="));
  142.       Serial.print(heartRate, DEC);
  143.  
  144.       Serial.print(F(", HRvalid="));
  145.       Serial.print(validHeartRate, DEC);
  146.  
  147.       Serial.print(F(", SPO2="));
  148.       Serial.print(spo2, DEC);
  149.  
  150.       Serial.print(F(", SPO2Valid="));
  151.       Serial.println(validSPO2, DEC);
  152.     }
  153.  
  154.     //After gathering 25 new samples recalculate HR and SP02
  155.     maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  156.   }
  157. }
  158.  
  159. // Function to calibrate the sensor for accuracy
  160. void calibrateSensor() {
  161.   // Implement calibration logic here
  162.   // This could involve taking several readings and averaging them
  163.   Serial.println(F("Calibrating sensor..."));
  164.   // Example calibration routine (this should be replaced with actual calibration logic)
  165.   delay(2000); // Simulate calibration time
  166.   Serial.println(F("Calibration complete."));
  167. }
  168.  
  169. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement