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;
}
}
}
1 Answer 1
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 insideSerial.println
. Why i need to usepulseSensor.sawNewSample()
? Its only return true or false because it's a boolean.rofimu– rofimu2018年10月27日 19:40:05 +00:00Commented Oct 27, 2018 at 19:40 -
without
begin
the library doesn't read the sensor. getBeatsPerMinute is only a getter which reads a variable in library.2018年10月27日 19:44:53 +00:00Commented Oct 27, 2018 at 19:44 -
-
Oh i see. I will try that.rofimu– rofimu2018年10月29日 07:07:08 +00:00Commented Oct 29, 2018 at 7:07
-
I added
pulseSensor.begin()
and it works. But it getting error when i writepulseSensor.sawNewSample()
, some of the LEDs always fast blinking and not in full brightness. But overall it works by addingpulseSensor.begin()
. Thanksrofimu– rofimu2018年10月31日 02:08:45 +00:00Commented Oct 31, 2018 at 2:08
>=
and<=
.