Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Heart Monitor**
- - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
- - Source Code created on: 2025-06-02 22:52:41
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Implement a heart rate and SpO2 monitoring system */
- /* using MAX30100 and MAX30105 sensors. Ensure */
- /* accurate readings, efficient power management, and */
- /* include a calibration routine for sensor accuracy. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <MAX30105.h> //https://github.com/sparkfun/SparkFun_MAX3010x_Sensor_Library
- #include "spo2_algorithm.h" // Added for SPO2 calculations
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t Max30102_MAX30100_I2C_PIN_SDA_D2 = 2;
- const uint8_t Max30102_MAX30100_I2C_PIN_SCL_D1 = 1;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- MAX30105 particleSensor; // Instance of the MAX30105 sensor
- #define MAX_BRIGHTNESS 255
- // Using uint32_t for buffer as ESP8266 has enough SRAM
- uint32_t irBuffer[100]; //infrared LED sensor data
- uint32_t redBuffer[100]; //red LED sensor data
- int32_t bufferLength; //data length
- int32_t spo2; //SPO2 value
- int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
- int32_t heartRate; //heart rate value
- int8_t validHeartRate; //indicator to show if the heart rate calculation is valid
- byte pulseLED = 11; //Must be on PWM pin
- byte readLED = 13; //Blinks with each data read
- void setup(void)
- {
- Serial.begin(115200); // initialize serial communication at 115200 bits per second:
- pinMode(pulseLED, OUTPUT);
- pinMode(readLED, OUTPUT);
- // Initialize sensor
- if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
- {
- Serial.println(F("MAX30105 was not found. Please check wiring/power."));
- while (1);
- }
- Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
- while (Serial.available() == 0) ; //wait until user presses a key
- Serial.read();
- byte ledBrightness = 60; //Options: 0=Off to 255=50mA
- byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
- byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
- byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
- int pulseWidth = 411; //Options: 69, 118, 215, 411
- int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
- particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
- // Calibration routine for sensor accuracy
- calibrateSensor();
- }
- void loop(void)
- {
- bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps
- //read the first 100 samples, and determine the signal range
- for (byte i = 0 ; i < bufferLength ; i++)
- {
- while (particleSensor.available() == false) //do we have new data?
- particleSensor.check(); //Check the sensor for new data
- redBuffer[i] = particleSensor.getRed();
- irBuffer[i] = particleSensor.getIR();
- particleSensor.nextSample(); //We're finished with this sample so move to next sample
- Serial.print(F("red="));
- Serial.print(redBuffer[i], DEC);
- Serial.print(F(", ir="));
- Serial.println(irBuffer[i], DEC);
- }
- //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
- maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
- //Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second
- while (1)
- {
- //dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
- for (byte i = 25; i < 100; i++)
- {
- redBuffer[i - 25] = redBuffer[i];
- irBuffer[i - 25] = irBuffer[i];
- }
- //take 25 sets of samples before calculating the heart rate.
- for (byte i = 75; i < 100; i++)
- {
- while (particleSensor.available() == false) //do we have new data?
- particleSensor.check(); //Check the sensor for new data
- digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
- redBuffer[i] = particleSensor.getRed();
- irBuffer[i] = particleSensor.getIR();
- particleSensor.nextSample(); //We're finished with this sample so move to next sample
- //send samples and calculation result to terminal program through UART
- Serial.print(F("red="));
- Serial.print(redBuffer[i], DEC);
- Serial.print(F(", ir="));
- Serial.print(irBuffer[i], DEC);
- Serial.print(F(", HR="));
- Serial.print(heartRate, DEC);
- Serial.print(F(", HRvalid="));
- Serial.print(validHeartRate, DEC);
- Serial.print(F(", SPO2="));
- Serial.print(spo2, DEC);
- Serial.print(F(", SPO2Valid="));
- Serial.println(validSPO2, DEC);
- }
- //After gathering 25 new samples recalculate HR and SP02
- maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
- }
- }
- // Function to calibrate the sensor for accuracy
- void calibrateSensor() {
- // Implement calibration logic here
- // This could involve taking several readings and averaging them
- Serial.println(F("Calibrating sensor..."));
- // Example calibration routine (this should be replaced with actual calibration logic)
- delay(2000); // Simulate calibration time
- Serial.println(F("Calibration complete."));
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement