Hi!
I have a project in Arduino IDE, where i use function String to send data to android via bluetooth, if i use string, after 1-2 minutes of usage my servomotor no longer responds to commands because of what's loading the board memory I think.
How can I free the memory of the board?
Maybe by using C style strings (zero terminated arrays of chars) instead of memory hungry Strings (objects of the String class) or perhaps by simply sending the values separately instead of concatenating them together into a single entity
And further - nothing in this code snippet can occupy all mcu memory in 2 minutes. Therefore your issue in somewhere else in the code.
Please show a whole code.
What Arduino board are you using? It looks odd to use 0 and 1 for your Bluetooth connection pins as that is the real UART used for loading the sketch as well as serial input and output through the serial monitor in the IDE.
When a String Object adds a new char, it copies itself with the added char then deletes the old smaller copy that leaves a hole in the heap. Add 1 more char, the new copy won't fit in the hole so allocate on top of the heap space for the latest then delete the old leaving room for the next oohhh-we-can-plus-text copy.
But you can prevent that by making a fixed length String like char arrays are fixed length and just be a little fatter and cycle-wasting to hang onto BASIC-like text commands distanced from how they work to the point of oblivious.
I use char array strings made of ASCII text chars terminated with a 0 char, ASCII NULL.
The text can be anywhere like inside of the SD/SPI buffer as long as you can address it you can use string.h commands (in the AVR C/C++ standard C library) to manipulate text or just move bytes around in arrays without the library.
Agree that not using the String class is more efficient. You can nevertheless address the issue you pointed by just reserving enough space upfront in the String instance, using the same value you would have for the fixed size buffer. (And it can still grow)
Char arrays are open and string.h functions use pointers.
Operate on text in place where that place may be a file buffer you will write.
When using the many KB of flash to store constant data, strings and values...
you can't do the String thing in PROGMEM. It's a C library! AVR LibC has the docs!
Why use Strings except ease of long habit or not knowing better?