I am using platformio platform with esp32 and max30102 to develop pulse oximetry and heart rate sensor. But I am facing problem with the code. I am unable to display both sp02 and bpm simentaneously on the oled display. Please help me with the code.
Thankyou.
#include <Arduino.h>
#include <Adafruit_GFX.h> //OLED libraries
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include "MAX30105.h" //MAX3010x library
#include "heartRate.h"
#include "spo2_algorithm.h"
MAX30105 particleSensor;
#define MAX_BRIGHTNESS 255
uint32_t irBuffer[100]; //infrared LED sensor data
uint32_t redBuffer[100]; //red LED sensor data
uint32_t abhi[100],spo2_avg1;
const byte RATE_SIZE = 25; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
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 = 12; //Must be on PWM pin
byte readLED = 2; //Blinks with each data read
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
void setup()
{
Serial.begin(115200); // initialize serial communication at 115200 bits per second:
pinMode(pulseLED, OUTPUT);
pinMode(readLED, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
display.display();
delay(3000);
// Initialize sensor
particleSensor.begin(Wire, I2C_SPEED_FAST); //Use default I2C port, 400kHz speed
particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
// 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
}
void loop()
{
bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps
//long irValue = particleSensor.getIR();
//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 = particleSensor.getRed();
irBuffer = particleSensor.getIR();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
Serial.print(F("red="));
Serial.print(redBuffer, DEC);
Serial.print(F(", ir="));
Serial.println(irBuffer, 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 = redBuffer;
irBuffer = irBuffer;
}
//take 25 sets of samples before calculating the heart rate.
//send samples and calculation result to terminal program through UART
// Serial.print(F("red="));
//Serial.print(redBuffer, DEC);
//Serial.print(F(", ir="));
//Serial.print(irBuffer, DEC);
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
abhi[i]=spo2;
Serial.print(spo2);
Serial.print("sample_spo2");
}
//int32_t y = particleSensor.getIR();
for (byte i=0;i<100;i++)
{
int32_t y=irBuffer[i];
if(y >7000)
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("BPM");
display.setCursor(50,0);
display.println(beatAvg);
//display.display();
display.setCursor(0,20);
display.println("Spo2");
display.setCursor(50,20);
display.println(spo2_avg1);
display.display();
Serial.print("1234567890");
delay(5000);
if (checkForBeat(y) == true) //If a heart beat is detected
{
display.clearDisplay(); //Clear the display
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("BPM");
display.setCursor(50,0);
display.println(beatAvg);
//display.display();
display.setCursor(0,20);
display.println("Spo2");
display.setCursor(50,20);
display.println(spo2_avg1);
display.display();
Serial.print("0987654321");
//And tone the buzzer for a 100ms you can reduce it it will be better
//delay(100);
//Deactivate the buzzer to have the effect of a "bip"
//We sensed a beat!
long delta = millis() - lastBeat; //Measure duration between two beats
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
Serial.print(beatsPerMinute);
Serial.print("beats");
//Calculating the BPM
if (beatsPerMinute < 255 && beatsPerMinute > 20) //To calculate the average we strore some values (4) then do some math to calculate the average
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++){
beatAvg += rates[x];}
beatAvg /= RATE_SIZE;
}
spo2_avg1 = 0;
for (byte i = 75; i < 100; i++){
spo2_avg1 += abhi[i];
}
spo2_avg1 /= 25;}
}
Serial.print(" BPM=");
Serial.print(beatAvg, DEC);
Serial.print(", SPO2=");
Serial.print(spo2_avg1, DEC);
Serial.print(", 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);
}
}
}
Problem with the Code. Please help me
Problem with the Code. Please help me
- Attachments
-
- main.cpp
- (7.88 KiB) Downloaded 303 times
Who is online
Users browsing this forum: Baidu [Spider] and 76 guests