Advice Needed: Troubleshooting Arduino Code with MAX30102 and NeoPixelsAdvice Needed: Troubleshooting Arduino Code with MAX30105 and NeoPixels

Hello, community!

I'm working on a project that involves using the MAX30102 sensor to detect heart rate and display it through NeoPixel LEDs. However, I've run into a few issues with my code and could really use your collective wisdom to troubleshoot. Here's a summary of my setup and the problems I'm facing:

Arduino Setup: I'm using an Arduino with a MAX30102 sensor and a strip of NeoPixels. The idea is to read the heart rate from the MAX30102 and display different colors on the NeoPixels based on the BPM.

  1. Issues Encountered:
  • When compiling, I get an error that 'I2C_SPEED_FAST' was not declared
    . I used this constant to set the I2C clock speed for the MAX30105 sensor. Is there a standard value I should use instead?
  • There are also errors indicating that setup
    , setPulseAmplitudeRed
    , and setPulseAmplitudeGreen
    are not members of the MAX30105
    class. It seems like I'm either using the wrong library or these methods are named differently. Has anyone else encountered this?
  • The getIR
    method also seems to be unrecognized. Is there a different method to read the IR value from the MAX30105 sensor?
  • Additionally, I'm unsure if my approach to updating the NeoPixels based on the BPM readings is optimal. Any suggestions for improvement?
  1. Request for Help:
  • Could anyone provide guidance on the correct usage of the MAX30105 library?
  • Are there alternative methods or constants I should use for initializing and reading data from the MAX30105 sensor?
  • Any advice on efficiently updating the NeoPixel color based on heart rate readings?
type or paste code here
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"

#ifdef __AVR__
 #include <avr/power.h> 
#endif

// NeoPixel setup
#define PIN        6 
#define NUMPIXELS 16 
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// MAX30105 setup
MAX30105 particleSensor;
const byte RATE_SIZE = 4;
byte rates[RATE_SIZE];
byte rateSpot = 0;
long lastBeat = 0;
float beatsPerMinute;
int beatAvg;

#define DELAYVAL 500

void setup() {
  #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
    clock_prescale_set(clock_div_1);
  #endif

  // Initialize NeoPixels
  pixels.begin();

  // Serial communication setup
  Serial.begin(115200);
  Serial.println("Initializing...");

  // Initialize sensor
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) { //Use default I2C port, 400kHz speed
    Serial.println("MAX30105 was not found. Please check wiring/power. ");
    while (1);
  }
  Serial.println("Place your index finger on the sensor with steady pressure.");

  particleSensor.setup(); //Configure sensor with default settings
  particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
  particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}

void loop() {
  long irValue = particleSensor.getIR();

  if (checkForBeat(irValue) == true) {
    // Heartbeat detected, calculate BPM
    long delta = millis() - lastBeat;
    lastBeat = millis();
    beatsPerMinute = 60 / (delta / 1000.0);

    // Store and average the BPM values
    if (beatsPerMinute < 255 && beatsPerMinute > 20) {
      rates[rateSpot++] = (byte)beatsPerMinute;
      rateSpot %= RATE_SIZE;

      beatAvg = 0;
      for (byte x = 0; x < RATE_SIZE; x++) {
        beatAvg += rates[x];
      }
      beatAvg /= RATE_SIZE;
    }
  }

  // Print the IR value and BPM to the serial monitor
  Serial.print("IR=");
  Serial.print(irValue);
  Serial.print(", BPM=");
  Serial.print(beatsPerMinute);
  Serial.print(", Avg BPM=");
  Serial.print(beatAvg);

  // Check if a finger is placed on the sensor
  if (irValue < 50000) {
    Serial.print(" No finger?");
  }
  Serial.println();
}


void updateNeoPixels(float bpm) {
  pixels.clear();
  uint32_t color;
  if (bpm < 100 && bpm > 60) {
    color = pixels.Color(0, 150, 0); // Green
  } else if (bpm <= 60) {
    color = pixels.Color(150, 0, 0); // Red
  } else if (bpm > 100) {
    color = pixels.Color(0, 0, 150); // Blue
  }
  for (int i = 0; i < NUMPIXELS; i++) {
    pixels.setPixelColor(i, color);
  }
  pixels.show();
  delay(DELAYVAL);
}



This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.