Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jenschr
Created November 2, 2018 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenschr/9312b904aaf403a280b10aaadad63913 to your computer and use it in GitHub Desktop.
Save jenschr/9312b904aaf403a280b10aaadad63913 to your computer and use it in GitHub Desktop.
Simple (incomplete) example of how to detect beats from data given by a MAX30102 sensor
unsigned long sensorValue = particleSensor.getIR();
if ( sensorValue > 50000)
{
if ( usePlotter )
{
Serial.println(sensorValue); //Send raw data to plotter
} else {
int diff = lastSensorValue - sensorValue;
if ( diff > 80 ) // we may have a beat!
{
unsigned long now = millis();
if (lastCount != 0) // ignore the first
{
int possibleValue = now - lastCount;
if ( possibleValue > 400 && possibleValue < 1500 )
{
heartbeats[ index ] = possibleValue;
//Serial.print("Saving: ");
//Serial.println(heartbeats[ index ]);
index++;
}
}
lastCount = now;
if ( index > BEATS ) {
index = 0;
// calc and output the average
int i, total;
for (i = 0; i < BEATS; i++)
{
total += heartbeats[ i ];
}
float average = (float)total / BEATS;
float bpm = 60000 / average;
Serial.print("Average ms between heartbeats: ");
Serial.print(average);
Serial.print(" BPM: ");
Serial.println((int)bpm);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(40, 40);
display.println((int)bpm);
display.display();
}
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
digitalWrite(LED_BUILTIN, LOW);
}
}
}
lastSensorValue = sensorValue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment