Last active
November 21, 2020 14:42
-
-
Save azaharafernandezguizan/53f63d2a69ce0c14cdb01ec9f1cee160 to your computer and use it in GitHub Desktop.
SPO2 sensor code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MAX30105.h" | |
#include "spo2_algorithm.h" | |
MAX30105 heartAndOxigenSensor; | |
uint32_t irBuffer[50]; //infrared LED sensor data | |
uint32_t redBuffer[50]; //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 | |
bool isFirstIteration; | |
String resultHearAndOxigenSensor = ""; | |
long bps = 9600; | |
int aplicationDelay = 500; | |
void setup() | |
{ | |
isFirstIteration = true; | |
bufferLength = 50; //buffer length of 100 stores 4 seconds of samples running at 25sps | |
if (!heartAndOxigenSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed | |
{ | |
while (1) | |
{ | |
} | |
} | |
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 = 50; //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 | |
heartAndOxigenSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); | |
} | |
void loop() | |
{ | |
HeartAndOxigenSensorControl(); | |
delay(500); | |
} | |
void HeartAndOxigenSensorControl() | |
{ | |
if (isFirstIteration) | |
{ | |
isFirstIteration = false; | |
//read the first bufferLength samples, and determine the signal range | |
for (byte i = 0; i < bufferLength; i++) | |
{ | |
while (heartAndOxigenSensor.available() == false) //do we have new data? | |
{ | |
heartAndOxigenSensor.check(); //Check the sensor for new data | |
} | |
redBuffer[i] = heartAndOxigenSensor.getRed(); | |
irBuffer[i] = heartAndOxigenSensor.getIR(); | |
heartAndOxigenSensor.nextSample(); //We're finished with this sample so move to next sample | |
} | |
//calculate heart rate and SpO2 after first 50 samples (first 4 seconds of samples) | |
maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate); | |
} | |
//dumping the first 12 sets of samples in the memory and shift the last 50 sets of samples to the top | |
for (byte i = 12; i < bufferLength; i++) | |
{ | |
redBuffer[i - 12] = redBuffer[i]; | |
irBuffer[i - 12] = irBuffer[i]; | |
} | |
//take 38 sets of samples before calculating the heart rate. | |
for (byte i = 38; i < bufferLength; i++) | |
{ | |
while (heartAndOxigenSensor.available() == false) //do we have new data? | |
{ | |
heartAndOxigenSensor.check(); //Check the sensor for new data | |
} | |
redBuffer[i] = heartAndOxigenSensor.getRed(); | |
irBuffer[i] = heartAndOxigenSensor.getIR(); | |
heartAndOxigenSensor.nextSample(); //We're finished with this sample so move to next sample | |
} | |
//After gathering 25 new samples recalculate HR and SP02 | |
maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate); | |
if (validSPO2 == 1) | |
{ | |
//Read the values and display them on LCD and leds | |
resultHearAndOxigenSensor = ""; | |
resultHearAndOxigenSensor.concat(spo2); | |
} | |
else | |
{ | |
//No data readed, show it on LCD | |
resultHearAndOxigenSensor = ""; | |
resultHearAndOxigenSensor.concat("-"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment