0

I'm making alarm with input from pulse sensor. When it detect high heart rate then it send a text message. Everything works fine when i am using potentiometer (as input) for simulating triggering SMS. I made several heart rate trigger options based on ages, so i'm using switch-case in my loop. I'm using PulseSensor Playground library and some sample skecth that came from with it, but when i put program that read beat per minute and put that value inside if statement it become doesn't work at all. Even if i put the BPM value to serial monitor it only display 0. What can i do to fix this problem? Thanks

Here is my program:

#define USE_ARDUINO_INTERRUPTS true     
#include <PulseSensorPlayground.h>     
#include <SoftwareSerial.h>              
#include <JC_Button.h>

const int OUTPUT_TYPE = SERIAL_PLOTTER;
const int PulseSensor = A0;  
const int THRESHOLD = 550;  

PulseSensorPlayground pulseSensor;
SoftwareSerial SIM800L(2, 3);  //(RX | TX)
const byte switchButton = 4;
int LED1 = 5;   //ages range 1
int LED2 = 6;   //ages range 2
int LED3 = 7;   //ages range 3
int greenLED  = 8;    //for normal heart rate
int yellowLED  = 9;   //for medium heart rate
int redLED = 10;      //for high heart rate

Button mybutton(switchButton);
boolean SMSsent = false;
int x = 0;

void setup(){
  mybutton.begin();
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(redLED, OUTPUT);

  Serial.begin(115200);
  pulseSensor.analogInput(PulseSensor);
  pulseSensor.setSerial(Serial);
  pulseSensor.setOutputType(OUTPUT_TYPE);
  pulseSensor.setThreshold(THRESHOLD);

  SIM800L.begin(115200);  
  Serial.println("SIM800L ready");
}

void led_for_ages_range_1(){
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
}
void led_for_ages_range_2(){
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, LOW);
}
void led_for_ages_range_3(){
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, HIGH);
}

void led_indicator_1(){
  digitalWrite(greenLED, HIGH);
  digitalWrite(yellowLED, LOW);
  digitalWrite(redLED, LOW);
}
void led_indicator_2(){
  digitalWrite(greenLED, LOW);
  digitalWrite(yellowLED, HIGH);
  digitalWrite(redLED, LOW);
}
void led_indicator_3(){
  digitalWrite(greenLED, LOW);
  digitalWrite(yellowLED, LOW);
  digitalWrite(redLED, HIGH);
}

void range_1_indicator(){
  int BPMval = pulseSensor.getBeatsPerMinute();

  if (BPMval > 60 && BPMval < 85){
    led_indicator_1();
    SMSsent = false;
  }
  else if (BPMval > 86 && BPMval < 100){
    led_indicator_2();
    if(SMSsent == false){
      text_message_1();
      SMSsent = true;
    }
  }
  else if (BPMval > 101){
    if(SMSsent == false){
      text_message_2();
      SMSsent = true;
    }
  } 
  else {
    SMSsent = false;
  }
}

void range_2_indicator(){
  int BPMval = pulseSensor.getBeatsPerMinute();

  if (BPMval > 60 && BPMval < 75){
    led_indicator_1();
    SMSsent = false;
  }
  else if (BPMval > 76 && BPMval < 90){
    led_indicator_2();
    if(SMSsent == false){
      text_message_1();
      SMSsent = true;
    }
  }
  else if (BPMval > 91){
    if(SMSsent == false){
      text_message_2();
      SMSsent = true;
    }
  } 
  else {
    SMSsent = false;
  }
}

void range_3_indicator(){
  int BPMval = pulseSensor.getBeatsPerMinute();

  if (BPMval > 60 && BPMval < 70){
    led_indicator_1();
    SMSsent = false;
  }
  else if (BPMval > 71 && BPMval < 80){
    led_indicator_2();
    if(SMSsent == false){
      text_message_1();
      SMSsent = true;
    }
  }
  else if (BPMval > 81){
    if(SMSsent == false){
      text_message_2();
      SMSsent = true;
    }
  } 
  else {
    SMSsent = false;
  }
}

void text_message_1(){
  int BPMval = pulseSensor.getBeatsPerMinute();
  SIM800L.write("AT+CMGF=1\r");
  delay(100);
  Serial.println("SIM800L set destination number.");
  SIM800L.write("AT+CMGS=\"0123456789\"\r");
  delay(100);
  Serial.println("Sending text message...");
  SIM800L.write("Your heart rate are: ");
  SIM800L.write(BPMval);
  SIM800L.write(" BPM");
  SIM800L.write("\r");
  Serial.println("Your heart rate are: ");
  Serial.print(BPMval);
  Serial.println(" BPM");
  SIM800L.write((char)26);
  delay(100);
  Serial.println("SMS sent.");
}

void text_message_2(){
  int BPMval = pulseSensor.getBeatsPerMinute();
  SIM800L.write("AT+CMGF=1\r");
  delay(100);
  Serial.println("SIM800L set destination number.");
  SIM800L.write("AT+CMGS=\"0123456789\"\r");
  delay(100);
  Serial.println("Sending text message...");
  SIM800L.write("Warning! Your heart rate are: ");
  SIM800L.write(BPMval);
  SIM800L.write(" BPM");
  SIM800L.write("\r");
  Serial.println("Warning! Your heart rate are: ");
  Serial.print(BPMval);
  Serial.println(" BPM");
  SIM800L.write((char)26);
  delay(100);
  Serial.println("SMS sent.");
}

void loop(){
  //int BPMval = pulseSensor.getBeatsPerMinute();
  static enum {OPTION_ONE, OPTION_TWO, OPTION_THREE} condition;
  while (x == 0){
    mybutton.read();
    switch (condition){
      case OPTION_ONE:
      led_for_ages_range_1();
      range_1_indicator();
      if (mybutton.wasReleased()){      
        condition = OPTION_TWO;
      }
      break;

      case OPTION_TWO:
      led_for_ages_range_2();
      range_2_indicator();
      if (mybutton.wasReleased()){      
        condition = OPTION_THREE;
      }
      break;

      case OPTION_THREE:
      led_for_ages_range_3();
      range_3_indicator();
      if (mybutton.wasReleased()){      
        condition = OPTION_ONE;
      }
      break;
    }
  }
}
  • examine you code carefully ... for example, you get no warning at 90bpm (there are more like that) – jsotola Oct 27 '18 at 5:12
  • That just an example value, i intend to use >= and <=. – rofimu Oct 27 '18 at 7:09
  • do you have a test sketch or some example sketch for the pulse sensor? does it work? or it doesn't work only in a sketch with SIM800 and SoftwareSerial – Juraj Oct 27 '18 at 9:04
  • @Juraj. Yes i have, and it works perfectly when i use example sketch from the PulseSensorPlayground library. Also it works with SIM800L and SoftwareSerial when it still in simple sketch, i'm not adding switch-case for switching for the ages range and LED status at that time, because i don't insert any ages options program. Is it because of my faulty switch-case logic? But that switch-case logic also works for changing LED status and 3 different range values when i use potentiometer for changing the analog input value that change the LED indicator. And the text message also works. – rofimu Oct 27 '18 at 10:29
  • add this to question – Juraj Oct 27 '18 at 11:05
0

You forgot pulseSensor.begin() and you don't use pulseSensor.sawNewSample(). See the examples of the library.

The library uses interrupts to measure the pulse. (It takes time, which would otherwise block the loop execution). The begin() function starts this interrupts.

The value measured by interrupts is accessible with getBeatsPerMinute() and sawNewSample() function indicates that new value is available.

The library can work without interrupts (PulseSensor_BPM_Alternative.ino), but even then the begin() function initializes the library and sawNewSample() is then the function which reads the sensor.

  • Did you see that in "Getting BPM_to_Monitor" sample sketch? In that sketch pulseSensor.begin() run only once and it's to print text inside Serial.println. Why i need to use pulseSensor.sawNewSample()? Its only return true or false because it's a boolean. – rofimu Oct 27 '18 at 19:40
  • without begin the library doesn't read the sensor. getBeatsPerMinute is only a getter which reads a variable in library. – Juraj Oct 27 '18 at 19:44
  • I enhanced the answer. – Juraj Oct 28 '18 at 6:14
  • Oh i see. I will try that. – rofimu Oct 29 '18 at 7:07
  • I added pulseSensor.begin() and it works. But it getting error when i write pulseSensor.sawNewSample(), some of the LEDs always fast blinking and not in full brightness. But overall it works by adding pulseSensor.begin(). Thanks – rofimu Oct 31 '18 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.