Hi everyone
I am doing a project where I need to get the location and bpm(beats per minute) of a person. I am using the GPS Neo 6m and a pulse sensor.
I am having some problems in the code. When I run it, everything starts working correctly. I call the GPS function and get the data, then I call the PulseSensor function and everything run perfect. The problem is when I try to call the GPS function again, it doesn't show me the data, when firstly it was working very well.
I believe the problem is in the pulseSensor.begin();. Because when the code pass through this line, it starts the "library" and the GPS stops working. I tried initializing the GPS again in the loop but it doesn't work.
Hope you guys can help me.
Sorry for my bad english, it is not my native language.
This is my code
#include <SoftwareSerial.h>//incluimos SoftwareSerial
#include <TinyGPS.h>//incluimos TinyGPS
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
TinyGPS gps;//Declaramos el objeto gps
SoftwareSerial serialgps(4,3);//Declaramos el pin 4 Tx y 3 Rx
//Declaramos la variables para la obtención de datos
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long chars;
unsigned short sentences, failed_checksum;
int x=0, y=0;
// Variables
const int PulseWire = 0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
int Threshold = 550; // Determine which Signal to "count as a beat" and which to ignore.
// Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
// Otherwise leave the default "550" value.
PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"
void setup()
{
/pinMode(7,OUTPUT);
Serial.begin(115200);//Iniciamos el puerto serie
serialgps.begin(9600);//Iniciamos el puerto serie del gps
Serial.println("Inicio de Programa\nIngresa un 1 para acceder al GPS\nIngresa un 2 para acceder al Sensor de pulso\nIngresa 3 para no realizar ninguna accion\n\n");
// Configure the PulseSensor object, by assigning our variables to it.
pulseSensor.analogInput(PulseWire);
pulseSensor.setThreshold(Threshold);
}
void loop()
{
x=Serial.read();
//Serial.println(y);
if(x==49)
{
y=x;
digitalWrite(7, LOW);
Serial.println(" GPS\n---Buscando senal--- ");
}
else if(x==50){
y=x;
digitalWrite(7, HIGH);
Serial.println("Sensor de Pulso\nObteniendo Datos\n");}
else if(x==48)
y=x;
switch (y)
{
case 49: localizar(); break;
case 50: pulso(); break;
}
}
void localizar()
{
serialgps.begin(9600);//Iniciamos el puerto serie del gps
while(serialgps.available())
{
int c = serialgps.read();
if(gps.encode(c))
{
float latitude, longitude;
gps.f_get_position(&latitude, &longitude);
Serial.print("Latitud/Longitud: ");
Serial.print(latitude,5);
Serial.print(", ");
Serial.println(longitude,5);
gps.crack_datetime(&year,&month,&day,&hour,&minute,&second,&hundredths);
Serial.print("Fecha: "); Serial.print(day, DEC); Serial.print("/");
Serial.print(month, DEC); Serial.print("/"); Serial.print(year);
Serial.print(" Hora: "); Serial.print(hour, DEC); Serial.print(":");
Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC);
Serial.print("."); Serial.println(hundredths, DEC);
Serial.print("Velocidad(kmph): "); Serial.println(gps.f_speed_kmph());
Serial.println();
gps.stats(&chars, &sentences, &failed_checksum);
}
}
}
void pulso()
{
pulseSensor.begin();
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened".
Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
Serial.print("BPM: "); // Print phrase "BPM: "
Serial.println(myBPM); // Print the value inside of myBPM.
}
delay(20); // considered best practice in a simple sketch.
}
