Combination of IR and bluetooth control freezes the applicatoin

I have a simple application to control servo motors. I want to send commands to the application using IR and/or bluetooth.
IR implementation uses Lego power functions transmitters implemented by Arduino-LPF library by Brickostan. It does use interrupt on PIN 3.
Bluetooth is implemented by standard SoftwareSerial library.
Used hardware is Nano.

Both control works great if I use only one of them. But in case I receive a serial bluetooth command, then I receive IR command and then I receive again bluetooth command, application freezes. Last bluetooth command is not processed. Debuging I can get just behind the " if (bluetooth.available() > 0)" line, but no longer reach line.
Application is frozen, does not respond to any new commmand, neither BT or IR.
I am not sending tens of command per second, it freezes even if I make a minute break between commands.
Can somebody guide me to the root of the problem?
Application code and LPF library attached. Control by IR is the main function, but because I wanted to try BLE device, I included also this control method.

Thanks for helping. (5.99 KB)

vyhybky.ino (6.68 KB)

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

You should arrange your code so that both input streams update the same variable that holds the value for Servo.write(). That way there will only be one function to move the servo.


Thank you for your help Robin, handling the strings was really the issue.
It will be 15 years I used last time C/C++, char arrays and memory allocations.
Since that time there was always plenty of memory and CPU power.
I know that Arduino is not Core i5 running Windows, but I simply thought that compiler will handle this for me :slight_smile:
That was a good lesson about being modest and save resources.


vyhybky.ino (7.49 KB)