Hi everyone I am currently facing challenges regarding combining the DHT11 code with the Pulse sensor ,they can not run at the same
time can you kindly assist me ,even when I add a Servo motor, have decide to comment out the Servo for it gives me an error please have a look on my code.
#include <SoftwareSerial.h>
//#include <Servo.h> // include library
#include <dht.h> // Include library
#include <LiquidCrystal_I2C.h> //Liquid Crystal Display with I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
//Servo myflag; // create servo object to control a servo
// twelve servo objects can be created on most boards
//-----------------------------------------------------------------------------------
//Alert reciever's phone number with country code
const String PHONE_1 = "+27737838775";
const String PHONE_2 = ""; //optional
const String PHONE_3 = ""; //optional
//-----------------------------------------------------------------------------------
PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"3
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
#define rxPin 2
#define txPin 3
SoftwareSerial sim800L(rxPin,txPin);
// Creates a DHT object
//-----------------------------------------------------------------------------------
dht DHT;
#define outPin 7 // Defines pin number to which the sensor is connected
//-----------------------------------------------------------------------------------
const int PIN_SPEAKER = 12; // speaker on pin2 makes a beep with heartbeat
const int OUTPUT_TYPE = SERIAL_PLOTTER;
const int PULSE_INPUT = A0;
const int PULSE_BLINK = 13; // Pin 13 is the on-board LED
const int PULSE_FADE = 5;
const int THRESHOLD = 550; // Adjust this number to avoid noise when idle
int RelayBuzzer = 8;
int pos = 0; // variable to store the servo position
//-----------------------------------------------------------------------------------
// Creates a DHT object
//-----------------------------------------------------------------------------------
//#define buzzer_pin 4
//-----------------------------------------------------------------------------------
void setup()
{
//-----------------------------------------------------------------------------------
//Begin serial communication: Arduino IDE (Serial Monitor)
Serial.begin(115200);
lcd.init(); // LCD screen
lcd.backlight();
// Configure the PulseSensor manager.
lcd.clear();
delay(1000);
lcd.setCursor(0,0);
lcd.print("PulseSensor");
//-----------------------------------------------------------------------------------
// Configure the PulseSensor object, by assigning our variables to it.
pulseSensor.analogInput(PULSE_INPUT);
pulseSensor.blinkOnPulse(PULSE_BLINK);
pulseSensor.fadeOnPulse(PULSE_FADE);
pulseSensor.setSerial(Serial);
pulseSensor.setOutputType(OUTPUT_TYPE);
pulseSensor.setThreshold(THRESHOLD);
//-----------------------------------------------------------------------------------
lcd.init(); // LCD screen
lcd.backlight();
//Begin serial communication: SIM800L
sim800L.begin(9600);
//-----------------------------------------------------------------------------------
pinMode(RelayBuzzer,OUTPUT);
//-----------------------------------------------------------------------------------
//myflag.attach(9); // attaches the servo on pin 9 to the servo object
//-----------------------------------------------------------------------------------
pinMode(RelayBuzzer,OUTPUT);
//pinMode(Buzzer,OUTPUT);
//----------------------------------------------------------------------------------
Serial.println("Initializing...");
//Once the handshake test is successful, it will back to OK
sim800L.println("AT");
delay(1000);
sim800L.println("AT+CMGF=1");
delay(1000);
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
if (!pulseSensor.begin())
{
for(;;) {
// Flash the led to show things didn't work.
digitalWrite(PULSE_BLINK, LOW);
delay(50);
digitalWrite(PULSE_BLINK, HIGH);
delay(50);
}
}
}
void loop()
{
while(sim800L.available())
{
Serial.println(sim800L.readString());
}
//-----------------------------------------------------------------------------------
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
delay(20);
// write the latest sample to Serial.
pulseSensor.outputSample();
if (pulseSensor.sawStartOfBeat())
{
pulseSensor.outputBeat();
tone(PIN_SPEAKER,900,300);// tone(pin,frequency)
digitalWrite(RelayBuzzer,LOW);
Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
Serial.print("BPM: "); // Print phrase "BPM: "
Serial.println(myBPM);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("HeartBeat");
delay(500);
lcd.setCursor(0,1);
lcd.print("BPM: ");
lcd.setCursor(4,1);
lcd.print(myBPM); // Print the value inside of myBPM.
delay(500);
}
//-----------------------------------------------------------------------------------
else if(pulseSensor.isInsideBeat() == false)
{
delay(500);
///digitalWrite(RelayBuzzer,HIGH);
noTone(PIN_SPEAKER);
if( myBPM >= 120 || myBPM <= 30 )
{
digitalWrite(RelayBuzzer,HIGH);
send_multi_sms();
make_multi_call();
//-----------------------------------------------------------------------------------
}
}
//-----------------------------------------------------------------------------------
//The fire is detected, trigger Alarm and send sms
//-----------------------------------------------------------------------------------
/*void DHT_11()
{
int readData = DHT.read11(outPin);
float t = DHT.temperature; // Read temperature
float h = DHT.humidity; // Read humidity
lcd.setCursor(4,1);
lcd.print("Temp= ");
lcd.print(t);
lcd.print("°C |");
delay(2000);
lcd.setCursor(4,1);
lcd.print("Hum = ");
lcd.print(h);
lcd.println("% ");
lcd.println("");
delay(2000); // wait two seconds
}*/
}
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
void send_multi_sms()
{
if(PHONE_1 != ""){
Serial.print("Phone 1: ");
send_sms("Emergency on the Patient ", PHONE_1);
}
if(PHONE_2 != ""){
Serial.print("Phone 2: ");
send_sms("Emergency on the Patient ", PHONE_2);
}
if(PHONE_3 != ""){
Serial.print("Phone 3: ");
send_sms("Emergency on the Patient ", PHONE_3);
}
}
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
void make_multi_call()
{
if(PHONE_1 != ""){
Serial.print("Phone 1: ");
make_call(PHONE_1);
}
if(PHONE_2 != ""){
Serial.print("Phone 2: ");
make_call(PHONE_2);
}
if(PHONE_3 != ""){
Serial.print("Phone 3: ");
make_call(PHONE_3);
}
}
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
void send_sms(String text, String phone)
{ // lcd.clear();
lcd.setCursor(0,0);
lcd.print("sending sms....");
delay(50);
sim800L.print("AT+CMGF=1\r");
delay(1000);
sim800L.print("AT+CMGS=\""+phone+"\"\r");
delay(1000);
sim800L.print(text);
delay(100);
sim800L.write(0x1A); //ascii code for ctrl-26 //Serial2.println((char)26); //ascii code for ctrl-26
delay(5000);
}
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
void make_call(String phone)
{ // lcd.clear();
lcd.setCursor(0,0);
lcd.print("calling....");
sim800L.println("ATD"+phone+";");
delay(20000); //20 sec delay
sim800L.println("ATH");
delay(1000); //1 sec delay
}