Two port receive not working (soft serial)

I am working on a project using arduino uno . The project has one finger print scanner and one bluetooth module .Since both bluetooth module and finger scanner works on RX TX pins .So i had used soft serial for having two RX,TX pins
Both the Finger scanner and Bluetooth does the same work, both turn on a LED
I have separate code for both(FP ,BT) both the sketch is working well individually
But when is joined both code together both finger scanner and Bluetooth is not working
This is the Finger print sketch : //working

#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
int led1 = 12, led2 = 11;
int getFingerprintIDez();
SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
void setup()  
{
  Serial.begin(9600);
  Serial.println("fingertest");
  pinMode(led1 ,OUTPUT);
  pinMode(led2 ,OUTPUT);
  finger.begin(57600);
    if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1);
  }
  Serial.println("Waiting for valid finger...");
}
void loop()  
{
  getFingerprintIDez();
  delay(50);
}
uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  
  // OK converted!
  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }   
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence); 
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK)  return -1;
  
  // found a match!
  digitalWrite(12,HIGH);
  delay(200);
  digitalWrite(11,HIGH);
  delay(500);
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence);
  return finger.fingerID; 
}

This is Bluetooth module sketch : //working

#include <SoftwareSerial.h>
int led1= 12, led2 = 11, led3 = 10, led4 = 9, led5 = 8, led6 = 7;
String readString;
int input = 0;
SoftwareSerial mySerial(2, 3); 
void setup()
{
  mySerial.begin(9600);
  Serial.begin(9600);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  pinMode(led5,OUTPUT);
  pinMode(led6,OUTPUT);
}

void loop()
{
  if(mySerial.available() > 0)
  {     
      input = mySerial.read();   
  }
  switch (input)
  {
    case 1:
      digitalWrite(12, HIGH);
      break;
    case 2:
    {
      digitalWrite(12, LOW); 
      digitalWrite(10, LOW); 
      digitalWrite(9, LOW);  
      digitalWrite(7, LOW);  
    } 
      break;
    case 3:                  
    digitalWrite(11, HIGH);   
      break;
    case 4:
      digitalWrite(10, HIGH);  
      break;
    case 5:
      digitalWrite(10, LOW);  
      break;
    case 6: 
      digitalWrite(9, HIGH);  
      break;
    case 7:
      digitalWrite(9, LOW);   
      break;  
    case 8:
      digitalWrite(7, HIGH);  
      break;
    case 9:
      digitalWrite(7, LOW);   
   
  }
}

And this is final code : //not working

#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
int led1= 12, led2 = 11, led3 = 10, led4 = 9, led5 = 8, led6 = 7;
String readString;
int input = 0;
int getFingerprintIDez();
SoftwareSerial FPSerial(2, 3);
SoftwareSerial mySerial(4, 5); 
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&FPSerial);
void setup()
{
  mySerial.begin(9600);
  Serial.println("fingertest");
  Serial.begin(9600);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  pinMode(led5,OUTPUT);
  pinMode(led6,OUTPUT);
  finger.begin(57600);
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1);
  }
  Serial.println("Waiting for valid finger...");
}

void loop()
{
  getFingerprintIDez();
  delay(50);.
  if(mySerial.available() > 0)
  {     
      input = mySerial.read();   
  }
  switch (input)
  {
    case 1:
      digitalWrite(12, HIGH);
      break;
    case 2:
    {
      digitalWrite(12, LOW); 
      digitalWrite(10, LOW); 
      digitalWrite(9, LOW);  
      digitalWrite(7, LOW);  
    } 
      break;
    case 3:                  
    digitalWrite(11, HIGH);   
      break;
    case 4:
      digitalWrite(10, HIGH);  
      break;
    case 5:
      digitalWrite(10, LOW);  
      break;
    case 6: 
      digitalWrite(9, HIGH);  
      break;
    case 7:
      digitalWrite(9, LOW);   
      break;  
    case 8:
      digitalWrite(7, HIGH);  
      break;
    case 9:
      digitalWrite(7, LOW);   
   
  }
}
uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  
  // OK converted!
  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }   
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence); 
}
nt getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK)  return -1;
  
  // found a match!
  digitalWrite(12,HIGH);
  delay(200);
  digitalWrite(11,HIGH);
  delay(500);
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence);
  return finger.fingerID; 
}

The library has the following known limitations:

  • If using multiple software serial ports, only one can receive data at a time.
    If your project requires simultaneous data flows, see Paul Stoffregen's AltSoftSerial library.

f your project requires simultaneous data flows, see Paul Stoffregen's AltSoftSerial library.

Or use a Mega with 4 hardware serial ports.

oqibidipo:
Arduino - SoftwareSerial

The library has the following known limitations:

  • If using multiple software serial ports, only one can receive data at a time.
    If your project requires simultaneous data flows, see Paul Stoffregen's AltSoftSerial library.

Sure would like to see code snippet (OMG he said snippet) to prove the above. It seems as another canned answer frequently posted here without much thought given to it.

Without reference to the SofwareSerial code the above statement is without much merit.

Assuming the SoftwareSerial limitation holds true,and from the brief look at the posted software you do not necessary need to have BOTH serial ports open SAME time.

When you are ready to read the finger print activate (begin) the port and when you are done use "end".
Serial communication in comparison with the processor speed is slower than molasses in winter and using "begin" and "end" MAY solve PART of the problem.

I am not 100% convinced it is WHOLE problem.

I would suggest to emulate the hardware to verify that the software works first.
It worked individually, but who knows if you are unknowingly sharing some variable - I did not look.

Instead of if ( Serial.available ) do just if(1) to see if the rest of the code works first without troubleshooting both hardware and software same time.

Vaclav:
Sure would like to see code snippet (OMG he said snippet) to prove the above. It seems as another canned answer frequently posted here without much thought given to it.

Without reference to the SofwareSerial code the above statement is without much merit.

SoftwareSerial: listen()

Description

Enables the selected software serial port to listen. Only one software serial port can listen at a time; data that arrives for other ports will be discarded. Any data already received is discarded during the call to listen() (unless the given instance is already listening).

SoftwareSerial.h

class SoftwareSerial : public Stream
{
private:
    // ...
    
    [color=red]// one receive buffer shared by all instances[/color]
    static char _receive_buffer[_SS_MAX_RX_BUFF];
    static volatile uint8_t _receive_buffer_tail;
    static volatile uint8_t _receive_buffer_head;

    [color=red]// this instance is the one that receives incoming data[/color]
    static SoftwareSerial *active_object;

    // ...
};

Enough evidence?

oqibidipo:
Arduino - SoftwareSerialListen

SoftwareSerial: listen()

Description

Enables the selected software serial port to listen. Only one software serial port can listen at a time; data that arrives for other ports will be discarded. Any data already received is discarded during the call to listen() (unless the given instance is already listening).

SoftwareSerial.h

class SoftwareSerial : public Stream

{
private:
    // ...
   
    // one receive buffer shared by all instances
    static char _receive_buffer[_SS_MAX_RX_BUFF];
    static volatile uint8_t _receive_buffer_tail;
    static volatile uint8_t _receive_buffer_head;

// this instance is the one that receives incoming data
    static SoftwareSerial *active_object;

// ...
};





Enough evidence?

Thank you.
So my crazy idea to end the one instance and start another is IN THEORY feasible.
Or build YASS ( yet another SoftwareSerial ) with its own buffer...
Or somebody else SotwareSerial without static buffer...
..or.. sky is the limit... its is after all software!

Vaclav:
Sure would like to see code snippet (OMG he said snippet) to prove the above.

We are not stopping you from writing some test code.

...R

Or build YASS ( yet another SoftwareSerial ) with its own buffer...

Feel free. You'll find that it is not as easy as it might sound. Using the right hardware is far easier.