So i have a project with max30100,mcp9808 and ad8232 and st7789 tft screen,when i upload the code only temprature and ecg update but max30100 doesnt update correctly on blynk as well as on screen,what is wrong?
this is my code
#define BLYNK_TEMPLATE_ID "----------"
#define BLYNK_TEMPLATE_NAME "--------"
#define BLYNK_AUTH_TOKEN "---------"
#include <Wire.h>
#include <BlynkSimpleEsp32.h>
#include "MAX30100_PulseOximeter.h"
#include <Adafruit_GFX.h>
#include <Adafruit_MCP9808.h>
#include <Adafruit_ST7789.h>
char auth[] = "-------";
char ssid[] = "-------";
char pass[] = "--------";
// ECG Pin
const int analogPin = 34;
// TFT Pins
#define TFT_CS 5
#define TFT_DC 2
#define TFT_RST 4
// ECG Graph Config
#define WAVEFORM_HEIGHT 200
#define WAVEFORM_START_Y (320 - WAVEFORM_HEIGHT)
#define BUFFER_SIZE 240
int waveformBuffer[BUFFER_SIZE];
int bufferIndex = 0;
unsigned long lastSampleTime = 0;
const long sampleInterval = 10;
// Sensors
PulseOximeter pox;
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
float heartRate = 0, spO2 = 0;
#define RESET_TIMEOUT_MS 10000 // Reset sensor if no update for 10s
uint32_t lastValidUpdate = 0;
// Blynk Timer
BlynkTimer timer;
void setup() {
Serial.begin(115200);
Wire.setClock(400000); // Increase I2C speed
Blynk.begin(auth, ssid, pass); // Start Blynk
tft.init(240, 320);
tft.setRotation(4);
tft.fillScreen(ST77XX_BLACK);
tft.setTextSize(2);
tft.setTextColor(ST77XX_WHITE);
tft.setCursor(10, 10);
tft.println("Initializing...");
if (!pox.begin()) {
Serial.println("Oximeter FAILED! Retrying...");
delay(1000);
if (!pox.begin()) { // Retry once
Serial.println("Oximeter permanently failed!");
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(10, 10);
tft.println("Oximeter FAILED!");
while (1);
}
} else {
Serial.println("Oximeter SUCCESS!");
}
if (!tempsensor.begin()) {
Serial.println("MCP9808 Sensor NOT Found!");
while (1);
}
lastValidUpdate = millis();
// **Blynk Timers**
timer.setInterval(5000L, updateBlynk); // Update Blynk every 5s
}
void loop() {
pox.update(); // Must run frequently!
unsigned long currentMillis = millis();
int ecgValue = analogRead(analogPin);
if (currentMillis - lastSampleTime >= sampleInterval) {
lastSampleTime = currentMillis;
updateWaveformBuffer(ecgValue);
displayECGWaveform();
Blynk.virtualWrite(V4, 4095 - ecgValue); // Fix Blynk graph inversion
}
// Force update every cycle to avoid freezing
heartRate = pox.getHeartRate();
spO2 = pox.getSpO2();
if (millis() - lastValidUpdate > RESET_TIMEOUT_MS) {
Serial.println("MAX30100 not responding, restarting...");
pox.begin();
lastValidUpdate = millis();
}
Blynk.run();
timer.run();
}
// **Blynk Update Function**
void updateBlynk() {
heartRate = pox.getHeartRate();
spO2 = pox.getSpO2();
float temperatureC = tempsensor.readTempC();
// **Check if Finger is Removed**
if (heartRate > 0 && spO2 > 0) {
lastValidUpdate = millis(); // Reset timeout if valid data received
} else {
heartRate = 0;
spO2 = 0;
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(10, 10);
tft.setTextSize(2);
tft.println("Place Finger!");
}
Blynk.virtualWrite(V1, heartRate);
Blynk.virtualWrite(V2, spO2);
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(10, 10);
tft.setTextSize(2);
tft.print("HR: ");
tft.print(heartRate);
tft.print(" bpm");
tft.setCursor(10, 50);
tft.setTextSize(2);
tft.print("SpO2: ");
tft.print(spO2);
tft.print("%");
tft.setCursor(10, 90);
tft.setTextSize(2);
tft.print("TEMP: ");
tft.print(temperatureC, 1);
tft.print((char)247);
tft.print("C");
Serial.print("HR: "); Serial.print(heartRate);
Serial.print("\tSpO2: "); Serial.print(spO2);
Serial.print("\tTemp: "); Serial.print(temperatureC);
Serial.print("\tECG: "); Serial.println(analogRead(analogPin));
Blynk.virtualWrite(V3, temperatureC);
}
// **Update ECG waveform buffer**
void updateWaveformBuffer(int reading) {
waveformBuffer[bufferIndex] = reading;
bufferIndex = (bufferIndex + 1) % BUFFER_SIZE;
}
// **Display ECG waveform on TFT**
void displayECGWaveform() {
tft.fillRect(0, WAVEFORM_START_Y, 240, WAVEFORM_HEIGHT, ST77XX_BLACK);
for (int i = 1; i < BUFFER_SIZE; i++) {
int previousIndex = (bufferIndex + i - 1) % BUFFER_SIZE;
int currentIndex = (bufferIndex + i) % BUFFER_SIZE;
int y1 = map(waveformBuffer[previousIndex], 0, 4095, WAVEFORM_START_Y + WAVEFORM_HEIGHT, WAVEFORM_START_Y);
int y2 = map(waveformBuffer[currentIndex], 0, 4095, WAVEFORM_START_Y + WAVEFORM_HEIGHT, WAVEFORM_START_Y);
tft.drawLine(i - 1, y1, i, y2, ST77XX_WHITE);
}
}