Advertisement
pleasedontcode

"Oximeter Integration" rev_02

Jun 10th, 2024
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Oximeter Integration"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-10 17:58:03
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop a heart rate and SpO2 monitoring system */
  21.     /* using the MAX30100 sensor connected via I2C (SDA: */
  22.     /* A4, SCL: A5) and an interrupt pin (D2). Display */
  23.     /* real-time data on an OLED screen. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Wire.h>
  28. #include <Adafruit_GFX.h>
  29. #include <Adafruit_SSD1306.h>
  30. #include <MAX30100_PulseOximeter.h>  // https://github.com/gabriel-milan/Arduino-MAX30100
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void onBeatDetected(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t display_MAX30100_INT_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF I2C PINS *****/
  41. const uint8_t display_MAX30100_I2C_PIN_SDA_A4 = A4;
  42. const uint8_t display_MAX30100_I2C_PIN_SCL_A5 = A5;
  43.  
  44. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  45. PulseOximeter pox;  // Create an instance of the PulseOximeter class
  46. Adafruit_SSD1306 display(128, 64, &Wire);  // Create an instance of the OLED display class
  47.  
  48. /****** CALLBACK FUNCTION *****/
  49. // Callback (registered below) fired when a pulse is detected
  50. void onBeatDetected()
  51. {
  52.     Serial.println("Beat!");
  53. }
  54.  
  55. void setup(void)
  56. {
  57.     // Initialize serial communication
  58.     Serial.begin(115200);
  59.  
  60.     // Initialize the interrupt pin
  61.     pinMode(display_MAX30100_INT_PIN_D2, INPUT_PULLUP);
  62.  
  63.     Serial.print("Initializing pulse oximeter..");
  64.  
  65.     // Initialize the PulseOximeter instance
  66.     if (!pox.begin()) {
  67.         Serial.println("FAILED");
  68.         for(;;);
  69.     } else {
  70.         Serial.println("SUCCESS");
  71.     }
  72.  
  73.     // Register a callback for the beat detection
  74.     pox.setOnBeatDetectedCallback(onBeatDetected);
  75.  
  76.     // Initialize the OLED display
  77.     if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {  // Use the correct I2C address
  78.         Serial.println(F("SSD1306 allocation failed"));
  79.         for(;;);
  80.     }
  81.     display.display();
  82.     delay(2000);  // Pause for 2 seconds
  83.  
  84.     // Clear the buffer
  85.     display.clearDisplay();
  86.     display.setTextSize(1);      // Normal 1:1 pixel scale
  87.     display.setTextColor(SSD1306_WHITE); // Draw white text
  88.     display.setCursor(0, 0);     // Start at top-left corner
  89.     display.print(F("Initializing..."));
  90.     display.display();
  91.     delay(1000);
  92. }
  93.  
  94. void loop(void)
  95. {
  96.     // Make sure to call update as fast as possible
  97.     pox.update();
  98.  
  99.     // Asynchronously dump heart rate and oxidation levels to the serial
  100.     // For both, a value of 0 means "invalid"
  101.     static uint32_t tsLastReport = 0;
  102.     const uint32_t REPORTING_PERIOD_MS = 1000;
  103.     if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
  104.         float heartRate = pox.getHeartRate();
  105.         uint8_t spO2 = pox.getSpO2();
  106.  
  107.         Serial.print("Heart rate:");
  108.         Serial.print(heartRate);
  109.         Serial.print("bpm / SpO2:");
  110.         Serial.print(spO2);
  111.         Serial.println("%");
  112.  
  113.         // Clear the display buffer
  114.         display.clearDisplay();
  115.  
  116.         // Display heart rate
  117.         display.setCursor(0, 0);
  118.         display.print(F("Heart rate: "));
  119.         display.print(heartRate);
  120.         display.println(F(" bpm"));
  121.  
  122.         // Display SpO2
  123.         display.print(F("SpO2: "));
  124.         display.print(spO2);
  125.         display.println(F(" %"));
  126.  
  127.         // Update the display
  128.         display.display();
  129.  
  130.         tsLastReport = millis();
  131.     }
  132. }
  133.  
  134. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement