Hello, Max30100 sensor works perfectly, but when i try to change Bluetooth state “b” sensor crashes and sends same measurement. And if state “a” is chosen, then program crashes and nothing works
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#define REPORTING_PERIOD_MS 10000
#define REPORTING_PERIOD_MS1 1000
PulseOximeter pox;
int readIndex=0;
int average_beat=0;
int average_SpO2=0;
const int numReadings=10;
float filterweight=0.5;
uint32_t tsLastReport = 0;
uint32_t last_beat=0;
bool calculation_complete=false;
bool calculating=false;
bool initialized=false;
byte beat=0;
String state = "";
// Callback (registered below) fired when a pulse is detected
void onBeatDetected()
{
Serial.println("Beat!");
}
void display_calculating(int j){
onBeatDetected();
Serial.println("Measuring");
for (int i=0;i<=j;i++) {
Serial.print(". ");
}
}
void setup()
{
Serial.begin(9600);
Serial.print("Initializing pulse oximeter..");
// Initialize the PulseOximeter instance
// Failures are generally due to an improper I2C wiring, missing power supply
// or wrong target chip
if (!pox.begin()) {
Serial.println("FAILED");
for(;;);
} else {
Serial.println("SUCCESS");
}
// The default current for the IR LED is 50mA and it could be changed
// by uncommenting the following line. Check MAX30100_Registers.h for all the
// available options.
//pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
// Register a callback for the beat detection
pox.setOnBeatDetectedCallback(onBeatDetected);
}
void calculate_average(int beat, int SpO2)
{
if (readIndex==numReadings) {
calculation_complete=true;
calculating=false;
initialized=false;
readIndex=0;
loop();
}
if (not calculation_complete and beat>30 and beat<220 and SpO2>50) {
average_beat = filterweight * (beat) + (1 - filterweight ) * average_beat;
average_SpO2 = filterweight * (SpO2) + (1 - filterweight ) * average_SpO2;
readIndex++;
display_calculating(readIndex);
}
}
void loop()
{
// Make sure to call update as fast as possible
pox.update();
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.readString(); // Reads the data from the serial port
pox.update();
}
//Serial.print(state);
// Asynchronously dump heart rate and oxidation levels to the serial
// For both, a value of 0 means "invalid"
if (state == "a"){
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
pox.update();
Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
Serial.print("bpm / SpO2:");
Serial.print(pox.getSpO2());
Serial.println("%");
Serial.print(state);
tsLastReport = millis();
pox.update();
}
}
while (state == "b"){
pox.update();
if (millis() - tsLastReport > REPORTING_PERIOD_MS1) {
pox.update();
Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
Serial.print("bpm / SpO2:");
Serial.print(pox.getSpO2());
Serial.println("%");
Serial.print(state);
pox.update();
tsLastReport = millis();
//state = "";
}
pox.update();
}
}