CCCPaste

"Oximeter Integration" rev_02

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Oximeter Integration"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-06-10 17:58:03

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Develop a heart rate and SpO2 monitoring system */
	/* using the MAX30100 sensor connected via I2C (SDA: */
	/* A4, SCL: A5) and an interrupt pin (D2). Display */
	/* real-time data on an OLED screen. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 
#include   // https://github.com/gabriel-milan/Arduino-MAX30100

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onBeatDetected(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t display_MAX30100_INT_PIN_D2 = 2;

/***** DEFINITION OF I2C PINS *****/
const uint8_t display_MAX30100_I2C_PIN_SDA_A4 = A4;
const uint8_t display_MAX30100_I2C_PIN_SCL_A5 = A5;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
PulseOximeter pox;  // Create an instance of the PulseOximeter class
Adafruit_SSD1306 display(128, 64, &Wire);  // Create an instance of the OLED display class

/****** CALLBACK FUNCTION *****/
// Callback (registered below) fired when a pulse is detected
void onBeatDetected()
{
    Serial.println("Beat!");
}

void setup(void)
{
    // Initialize serial communication
    Serial.begin(115200);

    // Initialize the interrupt pin
    pinMode(display_MAX30100_INT_PIN_D2, INPUT_PULLUP);

    Serial.print("Initializing pulse oximeter..");

    // Initialize the PulseOximeter instance
    if (!pox.begin()) {
        Serial.println("FAILED");
        for(;;);
    } else {
        Serial.println("SUCCESS");
    }

    // Register a callback for the beat detection
    pox.setOnBeatDetectedCallback(onBeatDetected);

    // Initialize the OLED display
    if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {  // Use the correct I2C address
        Serial.println(F("SSD1306 allocation failed"));
        for(;;);
    }
    display.display();
    delay(2000);  // Pause for 2 seconds

    // Clear the buffer
    display.clearDisplay();
    display.setTextSize(1);      // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE); // Draw white text
    display.setCursor(0, 0);     // Start at top-left corner
    display.print(F("Initializing..."));
    display.display();
    delay(1000);
}

void loop(void)
{
    // Make sure to call update as fast as possible
    pox.update();

    // Asynchronously dump heart rate and oxidation levels to the serial
    // For both, a value of 0 means "invalid"
    static uint32_t tsLastReport = 0;
    const uint32_t REPORTING_PERIOD_MS = 1000;
    if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
        float heartRate = pox.getHeartRate();
        uint8_t spO2 = pox.getSpO2();

        Serial.print("Heart rate:");
        Serial.print(heartRate);
        Serial.print("bpm / SpO2:");
        Serial.print(spO2);
        Serial.println("%");

        // Clear the display buffer
        display.clearDisplay();

        // Display heart rate
        display.setCursor(0, 0);
        display.print(F("Heart rate: "));
        display.print(heartRate);
        display.println(F(" bpm"));

        // Display SpO2
        display.print(F("SpO2: "));
        display.print(spO2);
        display.println(F(" %"));

        // Update the display
        display.display();

        tsLastReport = millis();
    }
}

/* END CODE */