How can i clear function "string"

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?

  

Thank you!!!

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

1 Like

First, String is not a function.

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.

3 Likes

You don't have to do all your printing in one hit.

3 Likes

Would setting value=""; after each bluetoothSerial.println(value); make a difference
?

1 Like

Just don’t even create the String in the first place, do


  Humidity = analogRead (Humidity_pin);
  int soilhumidity = map(Humidity, 0, 1023, 100, 0);
  rain = analogRead (rain_pin);
  int raindrop = map(rain, 0, 1023, 100, 0);
  float humidity_air = dht.readHumidity();       
  float temperature_air = dht.readTemperature();  
  bluetoothSerial.print(soilhumidity);
  bluetoothSerial.write(',');
  bluetoothSerial.print(raindrop);
  bluetoothSerial.write(',');
  bluetoothSerial.print(humidity_air);
  bluetoothSerial.write(',');
  bluetoothSerial.println(temperature_air);
2 Likes

That's my 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.

SoftwareSerial bluetoothSerial(0,1);

a7

String is a type of variable that fits large RAM environments and huge code where an Uno ferinstance is a tight environment where optimizing pays.

You appear to have edited the original post, and deleted the code.

Neat.

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.

AVR LibC Homepage listing all the covered chips, a long list!

The Reference of all the C Standard Libraries, links down to gnarly code details.
Contains the PROGMEM library. This is quality tech docs!

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)

1 Like

String keeps the text in a box it controls.

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?

Fair - mastering array management and knowledge of the c-strings and associated functions is definitely a good thing to know.

The smaller the hardware, the closer to the metal I want to get!

The things some wizards squeezed out of 1MHz 6502/6510's was inspiring!

I mostly don't need string.h. It's usually easier to arrange things down to array manipulation and print from PROGMEM now.

That’s what those functions do too

Using loops, no library required. I do my text IO char by char as each arrives since 1981.

Any time you modify a String, you create a completely NEW string. That is why they waste yor Arduino memory.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.