GPS Neo 6m and Pulse sensor not working together

5. Maybe you should put your code in code tags so we can read it.

Modify your post. Select "More" in the lower right corner of your post and select "Modify...". Then insert code tags before and after your code, like this:

```
**[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
  ...
[/code]**
```

When you press Preview, it shows you the white code block. It will look like this:

#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
  ...

Press "Save" to update your post.

Always put your sketch in between those two tags,
** **[code]** **
and
** **[/code]** **
. This preserves the original code when we see it and makes it easier for us to copy it. This is described in How to use the forum. Read it.

6. Make sure the code you post will compile.

This will not compile:

void setup()
{
/pinMode(7,OUTPUT);

If you are asking about a compile error, show us the error messages. Copy the error messages from the IDE window and copy them into your post. USE CODE TAGS around the error messages. This is described in How to use the forum.

I also suggest that you indent your code. It will make your code easier to read and debug. Just press control-T in the IDE editor, and it will format your code for you.

7. The main problem is this:

void localizar()
{
  serialgps.begin(9600);//Iniciamos el puerto serie del gps
  while(serialgps.available()) 
{
int c = serialgps.read();

Everytime you call begin, it empties the Arduino receive buffer. If there were any GPS characters in that buffer, they will be discarded. Then, serialgps.available() will never be true, and you will never (or rarely) read any GPS characters.

Instead, you should call begin just once, in setup. Then you should constantly check for GPS characters in loop. If you don't keep reading them, the Arduino will eventually start ignoring them. Let the sketch constantly parse the characters to decode GPS data fields. Don't print the fields until the user enters the '1' command.

8. This works, but is not recommended:

void loop()
{
   x=Serial.read();
  //Serial.println(y);
  if(x==49)

You should always test Serial.available before calling Serial.read(). Serial.read() returns -1 if no characters are available. Your code only checks for '1' (ASCII 49) and '2' (ASCII 50), so your code ignores the -1 values.

9. Don't use SoftwareSerial. It is very inefficient, because it disables interrupts for long periods of time. This will interfere with other parts of your sketch, or with libraries like PulseSensor. I seriously doubt that these two libraries can coexist.

3. AGAIN, Post a wiring diagram

Because you didn't tell us which Arduino you are using, nor how the pieces are connected, I do not know what serial library to suggest for your system. This is described in How to use the forum.

If you have a Mega, Leo or Micro, use Serial1 instead of SoftwareSerial.

For other Arduinos, I would normally suggest using AltSoftSerial on the two pins it requires (8 & 9 on an UNO). Unfortunately, the PulseSensor library conflicts with the AltSoftSerial library, so you will have to use NeoSWSerial.

If NeoSWSerial doesn't work (it also uses interrupts), you will have to put the GPS device on Serial, pin 0 (required to receive GPS data) and pin 1 (optional, only needed if you send configuration commands to the GPS RX pin). Read more here about choosing and connecting the GPS device.

That page is from my NeoGPS library. It is smaller, faster, more reliable and more accurate than all other GPS libraries. Here is your sketch, modified to use AltSoftSerial and NeoGPS, along with the suggestions above, IN CODE TAGS:

#include <NeoSWSerial.h>
#include <NMEAGPS.h>

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.

NMEAGPS gps;//Declaramos el objeto gps
gps_fix fix;

NeoSWSerial serialgps( 4, 3 ); // pin 4 to GPS Tx y 3 to GPS Rx


//  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);
  serialgps.begin(9600);//Iniciamos el puerto serie del gps
  Serial.begin(115200);//Iniciamos el puerto serie
  Serial.println( F("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()
{
  // Constantly read GPS characters.
  if(gps.available( serialgps ))
  {
    // Save new GPS data when it is available, once per second.
    fix = gps.read();
  }

  // Check for a command
  if (Serial.available()) {
    char c=Serial.read();
    //Serial.println(c);

    switch (c) {
      case '1':
        digitalWrite(7, LOW);
        Serial.println( F(" GPS\n---Buscando senal--- ") );
        localizar();
        break;

      case '2':
        digitalWrite(7, HIGH);
        Serial.println( F("Sensor de Pulso\nObteniendo Datos\n") );
        pulso();
        break;

      default:
        break;
    }
  }
}

void localizar()
{
  // Print the current GPS data fields

  Serial.print( F("Latitud/Longitud: ") );
  if (fix.valid.location) {
    Serial.print( fix.latitude(),5 );
    Serial.print(", ");
    Serial.print( fix.longitude(),5);
  }
  Serial.println();

  Serial.print( F("Fecha: ") );
  if (fix.valid.date) {
    Serial.print(fix.dateTime.date); Serial.print("/");
    Serial.print(fix.dateTime.month); Serial.print("/");
    Serial.print(fix.dateTime.year);
  }
  Serial.println();

  Serial.print( F("Hora: ") );
  if (fix.valid.time) {
    Serial.print(fix.dateTime.hours); Serial.print(":");
    Serial.print(fix.dateTime.minutes); Serial.print(":");
    Serial.print(fix.dateTime.seconds);
    if (fix.dateTime_cs < 10)
      Serial.print( '0' ); // leading zero when .04 hundredths
    Serial.print('.');
    Serial.print(fix.dateTime_cs);
  }
  Serial.println();

  Serial.print( F("Velocidad(kmph): ") );
  if (fix.valid.speed)
    Serial.print(fix.speed_kph());
  Serial.println();

  Serial.println();
}


void pulso()
{
  pulseSensor.begin();
  int myBPM = pulseSensor.getBeatsPerMinute();

  if (pulseSensor.sawStartOfBeat()) {
    Serial.println( F("♥  A HeartBeat Happened ! ") );
    Serial.print("BPM: ");
    Serial.println(myBPM);
  }
}

If you want to try it, NeoSWSerial and NeoGPS are available from the IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.

Cheers,
/dev

1 Like