user37269: 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;
    }
  }
}


user37622: 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.

37269: <p>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</p>

<p>Here is my program:</p>

<pre><code>#define USE_ARDUINO_INTERRUPTS true     
#include &lt;PulseSensorPlayground.h&gt;     
#include &lt;SoftwareSerial.h&gt;              
#include &lt;JC_Button.h&gt;

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 &gt; 60 &amp;&amp; BPMval &lt; 85){
    led_indicator_1();
    SMSsent = false;
  }
  else if (BPMval &gt; 86 &amp;&amp; BPMval &lt; 100){
    led_indicator_2();
    if(SMSsent == false){
      text_message_1();
      SMSsent = true;
    }
  }
  else if (BPMval &gt; 101){
    if(SMSsent == false){
      text_message_2();
      SMSsent = true;
    }
  } 
  else {
    SMSsent = false;
  }
}

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

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

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

  if (BPMval &gt; 60 &amp;&amp; BPMval &lt; 70){
    led_indicator_1();
    SMSsent = false;
  }
  else if (BPMval &gt; 71 &amp;&amp; BPMval &lt; 80){
    led_indicator_2();
    if(SMSsent == false){
      text_message_1();
      SMSsent = true;
    }
  }
  else if (BPMval &gt; 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;
    }
  }
}
</code></pre>
 37622: <p>You forgot <code>pulseSensor.begin()</code> and you don't use <code>pulseSensor.sawNewSample()</code>. See the examples of the library.</p>

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

<p>The value measured by interrupts is accessible with <code>getBeatsPerMinute()</code> and <code>sawNewSample()</code> function indicates that new value is available.</p>

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