How to programming buzzer on NodeMCU ESP8266 with MAX30100 and DS18B20 sensor

Hello friends, this project is my final project to design a health monitoring system. I have a problem here in combining the program to turn on the buzzer to sound when the temperature is below 36 C and above 37 C and SpO2 is below 90%. The program I use is as below :

#include <DallasTemperature.h>
#include <OneWire.h>
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"

#define ONE_WIRE_BUS 0 //D3 pin of nodemcu
#define REPORTING_PERIOD_MS 1000
#define minTemp 36
#define minSpO2 90

OneWire oneWire (ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
PulseOximeter pox;
uint32_t tsLastReport = 0;

void onBeatDetected() {
  Serial.println("Beat Detected!");
}
void setup() {
  Serial.begin(9600);
  Serial.print("Initializing the sensors...");
  sensors.begin(); //initialize sensor ds18b20
  sensors.setWaitForConversion(false);
  //Initialize sensor max30100
  if (!pox.begin()) {
    Serial.println("FAILED");
    for(;;);
  }else {
    Serial.println("SUCCESS");
  }
  //Configure sensor to use 7.6mA for LED drive
  pox.setIRLedCurrent (MAX30100_LED_CURR_7_6MA);
  //Register a callback routine
  pox.setOnBeatDetectedCallback(onBeatDetected);
  }
void loop() {
  //Read from the sensor
  pox.update();
  if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
    Serial.print("Your SpO2: ");
    Serial.print(pox.getSpO2());
    Serial.println("%");
    sensors.requestTemperatures();
    Serial.print("Your temperature: ");
    Serial.println(sensors.getTempCByIndex(0));
    tsLastReport = millis();
  }
}
void alerting() {
  pox.update();
  sensors.requestTemperatures();
  if (sensors.getTempCByIndex(0) < minTemp || pox.getSpO2() < minSpO2) { 
    tone(13, 1000); 
    delay(1000);
    noTone(13);
    delay(1000);
  }
}

Please for his guidance in doing this programming, I don't understand why my buzzer can't sound. can anyone understand about this problem?

@ijeeenk

Your similar question on the end of another topic deleted.

Please do not hijack other people's topics, doing so could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

you don't appear to call your void alerting() function?
should you call it in loop()?

void loop() {
   alerting() ;
........

... and also pox.update() doesn't like seeing delays() in your main loop

use millis instead

it can beep, but the problem appears SpO2 can't read

i just tried it by removing the delay, it worked. but how to stop the buzzer from running continuously? does it use millis in void alerting too? I don't understand how to use it

I assume you wish to sound the alarm once, e.g.

void alerting() {
 static int alarmOFF=1;
  pox.update();
  sensors.requestTemperatures();
  if (alramOFF && (sensors.getTempCByIndex(0) < minTemp || pox.getSpO2() < minSpO2)) { 
alarmOFF=0;
    tone(13, 1000); 
    delay(1000);
    noTone(13);
    delay(1000);
  }
else alarmOFF=1;
}

initally assume the alarm is off
when you get an alarm it should sound once
the variable alarmOFF is static so it retains updated values from call to call of function otherwise it would be reinitialised on each call

okay , I have done it for you, but you need to go through a millis() tutorial I linked you earlier

 void alerting() {

  pox.update();
  sensors.requestTemperatures();
  
  static bool alarmIsOn = false;
  static uint32_t prevMills1 = 0, prevMills2 = 0;
  
  if ( sensors.getTempCByIndex(0) < minTemp || pox.getSpO2() < minSpO2 ) { 
    if ( alarmIsOn == false ) {
    tone(13, 1000); 
	prevMills1 = millis();
	alarmIsOn = true;
	}
  }  // end if
  
  
  if ( prevMills1 > 0 && ( millis() - prevMills1 >= 1000 ) ) {
    noTone(13);
	prevMills1 = 0;
	prevMills2 = millis();
  } // end if
  
  if ( prevMills2 > 0 && ( millis() - prevMills2 >= 1000 ) ) {
	prevMills2 = 0;
	alarmIsOn = false;
  } // end if

}  // end function

thank you friend, you have helped me the way you did it, god bless you. btw I've opened the link you linked earlier, thanks friends

thank you friend, you have helped me in this way, god bless you

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