// bibliothèque de code
#include <Wire.h>
#include <MPU6050.h>
#include <MAX30100_PulseOximeter.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Initialisation des capteurs
MPU6050 mpu;
PulseOximeter pox; // Utiliser la classe PulseOximeter IMPORTANT
// Initialisation de l'écran OLED
#define SCREEN_WIDTH 128 // Largeur de l'écran OLED vérfier si bonne valeurs
#define SCREEN_HEIGHT 64 // Hauteur de l'écran OLED
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Adresse I2C par défaut sinon vérifier l'adresse 11C
const int buzzerPin = 9; // Broche du buzzer
bool alert = false;
// Seuils de détection constante de détections
const float accThreshold = 2.0; // Seuil d'accélération
const float hrThreshold = 30.0; // Seuil de changement de la fréquence cardiaque (bpm)
const float spO2Threshold = 5.0; // Seuil de changement de la SpO2 (%)
unsigned long lastMovementTime = 0; // Temps du dernier mouvement
unsigned long lastHeartRateTime = 0; // Temps de la dernière mesure de pouls
unsigned long lastSpO2Time = 0; // Temps de la dernière mesure de SpO2
const unsigned long movementInterval = 2000; // Intervalle de temps entre les mouvements
const unsigned long heartRateInterval = 2000; // Intervalle pour la mesure de pouls
const unsigned long spO2Interval = 2000; // Intervalle pour la mesure de SpO2
float lastHeartRate = 0;
float lastSpO2 = 0;
void activateAlarm(const char* message); // Déclaration de la fonction d'activation de l'alarme
void deactivateAlarm(); // Déclaration de la fonction de désactivation de l'alarme
// Initialisation du programme
void setup() {
Serial.begin(115200);
Wire.begin();
mpu.initialize();
pox.begin();
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
// Initialiser l'écran OLED Elegoo
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Adresse I2C de l'écran OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Initialisation..."); // message afficher lors du setup
display.display();
delay(2000);
display.clearDisplay(); // enlever informations avant void loop
}
// action continuellement effectuer loop
void loop() {
// Mettre à jour les données du capteur de pouls et de SpO2
pox.update();
float heartRate = pox.getHeartRate();
float spO2 = pox.getSpO2();
// Lire les données de l'accéléromètre
int16_t ax, ay, az; // trois axes de mouvement x,y,z
mpu.getAcceleration(&ax, &ay, &az); // Récupérer les valeurs d'accélération transférer nano
// Afficher les données sur l'écran OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print("HR:"); // données rhytme cardiaquee
display.print(heartRate);
display.print(" bpm");
display.setCursor(0, 10);
display.print("SpO2:"); // saturation oxygène dans le sang
display.print(spO2);
display.print("%");
display.display(); // Mettre à jour l'affichage
// Détection de mouvement brusque
if ((abs(ax) > accThreshold || abs(ay) > accThreshold || abs(az) > accThreshold) &&
(millis() - lastMovementTime > movementInterval)) {
lastMovementTime = millis();
alert = true;
activateAlarm("Mouvement brusque!"); // afficher sur l'écran oled
}
// Détection d'augmentation ou de diminution soudaine du pouls
if (heartRate > 0 && (abs(heartRate - lastHeartRate) > hrThreshold) &&
(millis() - lastHeartRateTime > heartRateInterval)) {
lastHeartRateTime = millis();
lastHeartRate = heartRate; // Mettre à jour la dernière fréquence cardiaque (environ une secconde)
alert = true;
activateAlarm("Changement bpm!");
}
// Détection d'augmentation ou de diminution soudaine de la SpO2
if (spO2 > 0 && (abs(spO2 - lastSpO2) > spO2Threshold) &&
(millis() - lastSpO2Time > spO2Interval)) {
lastSpO2Time = millis();
lastSpO2 = spO2; // Mettre à jour la dernière valeur de SpO2
alert = true;
activateAlarm("Changement Saturation O2!");
}
// Désactiver l'alarme si les conditions sont normales
if (!alert) {
deactivateAlarm();
}
delay(100);
}
// Fonction pour activer l'alarme
void activateAlarm(const char* message) {
digitalWrite(buzzerPin, HIGH); // Activer le buzzer
display.clearDisplay();
display.setCursor(0, 0);
display.print(message); // Afficher le message d'alarme
display.display(); // Mettre à jour l'affichage
alert = true; // Marquer l'état d'alerte
}
// Fonction pour désactiver l'alarme lorsque les conditions redeviennent normales
void deactivateAlarm() {
digitalWrite(buzzerPin, LOW); // Désactiver le buzzer
display.clearDisplay();
display.setCursor(0, 0);
display.print("Conditions OK");
display.display(); // Mettre à jour l'affichage
alert = false; // Réinitialiser l'état d'alerte
}
Which Arduino are you using? With the Adafruit_SSD1306 library you might be running out of memory.
What does the IDE report for the memory usage (dynamic memory)? You will need to add 1024 bytes to that and the result should basically stay below 75%.
So here the code for the test of the heart rate detector
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <MAX30100_PulseOximeter.h>
// Define the dimensions of the OLED screen
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Create an OLED display object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Create a PulseOximeter object for the MAX30100 sensor
PulseOximeter pox;
// Variables to store sensor readings
float heartRate;
float spO2;
void setup() {
// Initialize the serial port
Serial.begin(115200);
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Failed to initialize the OLED display"));
while (true); // Infinite loop in case of an error
}
// Clear the OLED display and set up initial text
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Initializing...");
display.display();
// Initialize the MAX30100 sensor
if (!pox.begin()) {
Serial.println("Error initializing the MAX30100 sensor");
display.clearDisplay();
display.setCursor(0, 0);
display.println("MAX30100 Error");
display.display();
while (true); // Infinite loop in case of an error
}
display.clearDisplay();
display.setCursor(0, 0);
display.println("Sensor Ready!");
display.display();
delay(2000); // Pause for 2 seconds
}
void loop() {
// Update the sensor readings
pox.update();
// Get heart rate and SpO2 data
heartRate = pox.getHeartRate();
spO2 = pox.getSpO2();
// Display the data on the OLED screen
display.clearDisplay();
display.setCursor(0, 0);
display.print("BPM: ");
display.print(heartRate); // Show heart rate
display.setCursor(0, 20);
display.print("SpO2: ");
display.print(spO2); // Show blood oxygen saturation level
display.print("%");
display.display();
// Also print the data to the serial monitor
Serial.print("Heart Rate: ");
Serial.print(heartRate);
Serial.print(" bpm | SpO2: ");
Serial.print(spO2);
Serial.println("%");
delay(1000); // Pause for 1 second between readings
}
What was supposed to happen is that the heart rate sensor detect it stuff and the accelerometer too and if it exceed a certain limit it make an alarm with the buzzer (that is working) and all the number are suposed to be displayed on the screen.
the problem was that the screen wasn't working and that the max30100 wasn't working in the big code for the detector but only in a small code so my theory is that it was connections that were wrong for the max30100 but for the screen I need help