Can I use both Bluetooth Serial and GPS Serial in the same sketch?

While searching, I've seen some posts that indicate others are running Bluetooth Software Serial and GPS Software Serial psuedo simultaneously.

In the example below, when I uncomment the 4 GPS related lines in void setup(), my Bluetooth portion no longer works, when I comment those lines out again, my Bluetooth code will work.

What am I doing wrong that I'm not able to run both at the same time (in the same sketch) on my Arduino Mega?

#include <EEPROM.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>

SoftwareSerial BTSerial(12, 13);   // RX , TX

// Serial In/Out Variables
String serialInString;
boolean newData = false;

// GPS Variables
TinyGPSPlus gps;
byte last_second;
char Time[]  = "TIME:00:00:00";
char Date[]  = "DATE:00/00/2000";
#define S_RX    11                // Define software serial RX pin
#define S_TX    10               // Define software serial TX pin
const int UTC_offset = -5; // Eastern Time
 
SoftwareSerial GPSSerial(S_RX, S_TX);

String getSettingValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
} // getSettingValue

void setup() 
{
    Serial.begin(9600);
    while (!Serial)
    {
      ; // Wait for serial to connect
    }

    BTSerial.begin(9600);
    while (!BTSerial)
    {
      ; // Wait for BTSerial to connect
    }

    //GPSSerial.begin(9600);
    //while (!GPSSerial) 
    //{
    //  ; // Wait for BTSerial to connect
    //}
   
    delay(2000);

    Serial.println("");
    Serial.println("    ***** Start *****    ");
    Serial.println("");
} // setup() 

void loop() 
{
  
    while(BTSerial.available() > 0 && newData == false)
    { 
        serialInString = BTSerial.readString();
        Serial.print("New BT data: "); Serial.println(serialInString);
        char serialInCharArr[serialInString.length() + 1];
        serialInString.toCharArray(serialInCharArr, (serialInString.length() + 1));
        String serialData = "";
        
        for (int i = 0; i < serialInString.length(); i++)
        {
            if (serialInCharArr[i] == '<')
            {
                bool startCharFound = true;
                serialData = "";
            }
            else if (serialInCharArr[i] == '>')
            {
                bool endCharFound = true;
                Serial.print("End char found. serialData is: "); Serial.println(serialData);
                
                newData = false;
                String s_settingName = getSettingValue(serialData, '=', 0);
                String s_settingValue = getSettingValue(serialData, '=', 1);

                Serial.print("s_settingName: "); Serial.println(s_settingName);
                Serial.print("s_settingValue: "); Serial.println(s_settingValue);
            }
            else
            {
                serialData = serialData + serialInCharArr[i];
            }
        }
        
        delay(15);
    }


} // loop()

good news, if you do have a mega..
it has 4 hardware serial port, loose the software serial..
functions/communication/serial/

good luck.. ~q

It's just SoftwareSerial. It doesn't care what it is connected to. And you can't have two instances listening at the same time. That's right there in the documentation for SoftwareSerial.

But SoftwareSerial is a hack that you use when you don't have a real serial port. If you have an Arduino Mega then why are you using SoftwareSerial? Because some example for a different board used it and you just copied?

Be careful using Strings like that on an AVR. Once the heap is shot full of holes from String concatenation then you suddenly run out of memory for seemingly no reason. Much has been written on the subject.

I had a feeling that was the issue, but I was having a hard time finding documentation indicating that, I appreciate the confirmation.

Can I declare an alias for Serial1? As in BTSerial = Serial1; or something similar?

#define BTSerial Serial1

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.