2 softserials - not working properly

Hi,

I have a arduino MEGA with a bluetooth module connected to another arduino UNO

I have 2 softserials:
one for sending commands to another arduino and wait for a reply if the command is executed
the other one is for the bluetooth connection, to listen for a command (or in the future sending some data). But when the first command is send through softserial 1 and is being executed, I can send a 1 or 0 though bluetooth, but at the end of the execution (softserial one) the code stops, and I can only receive something on my softserial 2. it does not read a new command from my file on the SD card…

code:

//------------------------------------------------------------------------------
// CONSTANTS
//------------------------------------------------------------------------------
#define BAUD (57600) // How fast is the Arduino talking SERIAL MONITOR (just for us, humans)?
#define BAUD2 (4800) // How fast is the Arduino talking SOFTSERIAL
#define BAUD3 (9600) // How fast is the Arduino talking SOFTSERIAL for BlueTooth connection


//------------------------------------------------------------------------------
// INCLUDES
//------------------------------------------------------------------------------
#include <SD.h>
#include <Wire.h>
#include <SoftwareSerial.h>//https://www.arduino.cc/en/Tutorial/TwoPortReceive

//------------------------------------------------------------------------------
// GLOBALS
//------------------------------------------------------------------------------
// WORKING VALUES
char inputString [100];
char inputChar;

boolean ResetFile = true;
long randNumber;
String filename;

int stringIndex = 0; // String stringIndexing int;
int lineIndex = 1;


int Received = 0;
int LEDstate = 0;

// SOFTWARE SERIAL #1: RX = digital pin 10, TX = digital pin 11
SoftwareSerial mySerial(11, 12); // RX, TX

// SOFTWARE SERIAL #2: RX = digital pin 10, TX = digital pin 13
SoftwareSerial mySerialBT(10, 13); // RX, TX


//CONNECTIONS
File myFile; // instance of a file


void setup(){

 // Open serial communications and wait for port to open:
  Serial.begin(BAUD);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("ARDUINO MEGA - SD SHIELD");
 

  // set the data rate for the SoftwareSerial port
  
  mySerial.begin(BAUD2);
  while (!mySerial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


mySerialBT.begin(BAUD3);
 while (!mySerialBT) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0)); //NEEDED FOR THE RANDOM FUNCTION

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT); //MEGA: SS

  //ONLY FOR THE UNO
  pinMode(53, OUTPUT);  //MEGA: 53, UNO 10
  digitalWrite(53, HIGH); //MEGA: 53, UNO 10

  // see if the card is present and can be initialized:
  if (!SD.begin(4)){
    Serial.println("Initialization failed: card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  
  Serial.println("Initialization done.");

  //OPEN A RANDOM FILE FROM THE SD CARD
  OpenFile();
}


void OpenFile() {

  if (ResetFile == true){
    Serial.println("Reset Drawing");
    filename = "TEST.txt"; //reset the drawing
    ResetFile = false;
  }else{
    //Choose a random filenumber
    randNumber = random(1, 5);  //CHOOSE RANDMLY A FILENUMBER BETWEEN 1 AND 4
  
    filename = "TEST" + String(randNumber);
    filename = filename + ".txt";
    ResetFile = true;
  }
 
  // Open up the file we're going to log to!
  myFile = SD.open(filename);
  if (! myFile){
    Serial.print("error opening File: ");
    Serial.println(filename);
    Serial.println("No such file in directory");
    // Wait forever since we cant write data
    while (1) ;
  }

    Serial.print("....Reading ");
    Serial.print(filename);
    Serial.println(":");
    delay(100);
}
 
void loop() {

 mySerial.begin(BAUD2);
 inputChar = myFile.read(); // Gets one byte from serial buffer
  
  if (inputChar != '\n')
      { // define breaking char here
        if (inputChar == 'Q')  //end of the file
          {
           Serial.println("---END OF FILE---");
            myFile.close();
            //OPEN A NEW RANDOM FILE OR RESET FILE
            lineIndex=1;
            OpenFile();
          }
          else
          {
            inputString[stringIndex] = inputChar; // Store it
            stringIndex++; // Increment where to write next
          }
      }
      else
      {
        Serial.print("line ");
        Serial.print(lineIndex);
        Serial.print(": "); // shows that the program is cycling, for debugging only
        Serial.println(inputString);     
      }        
      mySerial.write(inputString);
      
        stringIndex = 0; // clear the value for the next line
        lineIndex++;
        memset(inputString, 0, sizeof(inputString));

     //Wait until the other UNO repsonse with "1" //or "0" (1=ok, 0=nok)
     while (mySerial.available() == 0) {


//////////////////BLUETOOTH///////////////////////////

 mySerialBT.begin(BAUD3);
 while (mySerialBT.available() > 0) {  
    Received = mySerialBT.read();
 }


if (LEDstate == 0 && Received == '1')
  {
    Serial.println("LED on");
    LEDstate=1; 
  }
if (LEDstate ==1 && Received == '0')
  {
    Serial.println("LED off");
    LEDstate=0;
  }
   ///////////////////////////////////////
   
                char iByte = mySerial.read();
            
                
              if (iByte =='1') {
                  Serial.println("Command executed, next line please..");
                }
      }         
}

Don't use SoftwareSerial, it's a hack that eats up all of your resources, is slow and unreliable.

Your Arduino MEGA has multiple hardware UARTs. Use those instead.

Pieter

A Mega has 3 additional hardware serial ports; a quick view at your code does not seem to show a reason why you can't use those instead software serial.

Also, why e.g.

while (!mySerialBT) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

It clearly states that that is only required for native USB and SoftwareSerial is definitely not native USB.

Problem solved,

thanks anyway (and thanks to bring the 2 other 'normal' serials of a MEGA to my attention)