Please Help with combining two codes, both use softwareSerial on mega!

Hello, I'm trying to combine two codes to work as one file. The two codes use softwareserial work fine when uploaded individually, but i would like to to upload them using a single code so they carry out all the functions at once. For example i have a Bluetooth and buttons code that does all that is required, and i have a fingerprint and lcd code that also does what its required. But these work once each code is uploaded. However i would like to upload as a single file so everything works with a single code and not individual codes. But my knowledge of combining the two is limited. Can someone please try to add them for me. Due to length of the codes i will have to put them as attachments

Why use SoftSerial at all on a Mega when it has 4 separate hardware serial interfaces ?

I don't know how to use the hardware interfaces. i had trouble trying to use the tx and rx. Thanks

Give up trying to use 2 instances of SoftwareSerial as it is almost impossible to get 2 working at the same time.

On the Mega there are 4 hardware serial interfaces named Serial, Serial1, Serial2 and Serial 3.
The Arduino Mega has three additional serial ports: Serial1 on pins 19 (RX) and 18 (TX), Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX).

Tx on the Mega should be connected to Rx on the serial device and vice versa. If the device being connected to uses RS232 as opposed to TTL for communication then you will need extra hardware to do the conversion.

You can start any of them like this

Serial1.begin(9600);  // set baud rate to suit
Serial2.begin(9600);  // set baud rate to suit
Serial3.begin(9600);  // set baud rate to suit

You can test whether any data is available like this

if (Serial1.availabl())
{
  /do stuff
}
etc

and read from any of them

aChar = Serial1.read()
etc

and write to the

Serial1.write(aByte);
Serial1.begin(9600);
Serial2.begin(9600);

Connect TX of the serial port 1 to RX of the bluetooth module, connect RX of the serial port 1 to the TX of the bluetooth module.
Same for the LCD.

Just pay attention as to what the modules mark as TX or RX; they might have swapped RX and TX around to make it easier for you and more complicated for me.

Try the individual sketches first with the above, next combine them.

The bluetooth and scanner am using, have the use of libraries and in the library they use the softwareSerial so from there i do not know how else to go around using hardware. I have spent alot of time on the softwareSerial to work and afraid i dont have much time to try out the hardware since i have no clue. But will i do some research.

Change these

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

to

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial1);

HardwareSerial &bluetooth = Serial2;

(untested)

Also, it should not be necessary to drop the speed of the bluetooth serial to 9600.

oqibidipo:
Change these

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);




to



Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial1);

HardwareSerial &bluetooth = Serial2;



(untested)

Also, it should not be necessary to drop the speed of the bluetooth serial to 9600.

testing individually the LCD is working, the bluetooth however is not working only the buttons are working after the change.

since i have changed the HardwareSerial &bluetooth = Serial2;

What do i change these to?

int bluetoothTx = 53; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 52; // RX-I pin of bluetooth mate, Arduino D3

because of the following

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

What do i change these to?

   int bluetoothTx = 53; // TX-O pin of bluetooth mate, Arduino D2

int bluetoothRx = 52; // RX-I pin of bluetooth mate, Arduino D3



because of the following 



SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

Delete all three lines. You've already defined the bluetooth port to be Serial2.

Cheers,
/dev