Pages: [1]   Go Down
Author Topic: Problem using String  (Read 861 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there. I googled for a problem concering my little arduino projects.
It would be to complicated to upload my whole program so I take a codepart which I've found in this forum(http://arduino.cc/forum/index.php/topic,95914.30.html) and is pretty much alike my problem:
Code:
#include <MemoryFree.h>
void setup() {
  Serial.begin(9600);
}
void loop() {
  int n = 0;
  while(n <=100) {
    int u = 20*n;
    int v = 30*n;
    String str = String(n) + ", " + String(u) + ", " + String(v) + ", " + String(freeMemory());
    Serial.println(str);
    delay(500);
    n++;
  }
  Serial.println("Done");  
}
As far as I understand, its a problem of arduinos memory that's used to store the variables.
I also followed this posts http://arduino.cc/forum/index.php/topic,95914.0.html and I think its dealing with the problem(isnt it?).
To be honest, im not a native english speaker and ive got a difficult time following these solutions.
My question is:
1.Is there a library to download to replace the broken one?
2. If not, is there a workaround(proofed? dont want to destroy my board, saved my pocketmoney for a long time <.<)
3. If nothing helps I have to surrender and stop using String?

Im sorry for creating this new topic but i really didn't get what to do in the mentioned topics above.

J.A.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
1.Is there a library to download to replace the broken one?
No. The library isn't broken. The underlying mechanism for dealing with dynamically allocated memory (free()) is.

Quote
2. If not, is there a workaround
There is a patch that can be applied to fix the free() function.

Quote
3. If nothing helps I have to surrender and stop using String?
That would be the best thing to do, given how little memory the Arduino has.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow. its awesome to see how fast you answer.
If I use the patch, do I have to implement it then on every single file in which I use String or do I use the "working String" automatically?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If I use the patch, do I have to implement it then on every single file in which I use String or do I use the "working String" automatically?
The patch fixes the free() function that is called by the String destructor. No changes to the String class are needed (Well, they are, but it is not up to you to make them) nor are changes needed to any sketches that use the String class.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok. When I'm at home I'll try the patch.(is it a community-made one or a official? Don't want to lose my warranty)
Btw, is here any thank-you-function?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is it a community-made one or a official?
Community made.

Code:
Don't want to lose my warranty
You are patching software on the PC, not something on the Arduino hardware.

Quote
Btw, is here any thank-you-function?
No, you actually have to type stuff.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, so I have to thank u manually smiley-wink
You gave me hope back! Ty sir.
I'll take this one, its the only one I found smiley
pjrc.com/teensy/arduino_contrib.html
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could do it "The Arduino Way".  smiley

Code:
#include <MemoryFree.h>
void setup() {
  Serial.begin(9600);
}
void loop() {
  for (int n = 0; n <=100; n++) {
    int u = 20*n;
    int v = 30*n;
    Serial.print(n, DEC);
    Serial.print(", ");
    Serial.print(u, DEC);
    Serial.print(", ");
    Serial.print(v, DEC);
    Serial.print(", ");
    Serial.println(freeMemory(), DEC);

    delay(500);
  }
  Serial.println("Done"); 
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well..the string which is shown is the same as in the code above.
but I'm working with serialevent-methods..that wont work..every print will call it another time..i hoped to find a solution to fix the String/free()/what-ever bug.
I haven't found the proper patch yet but if there's one, I'll find it at all cost smiley
TY though!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but I'm working with serialevent-methods..that wont work..every print will call it another time.
I'm thinking that you don't understand how the serialEvent() method gets called, or serial data processing, then.

Using one Serial.print() or a dozen Serial.print() statements makes no difference to how the data is eventually shifted out the serial port, or how it is received on the other end.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using Java. Every time I print()/println() the event is called in java(I'm using the rxtx library of java and the method is called serialEvent(SerialPortEvent oEvent))
But you are right. I'm a newbie trying to get these things to know by watching examples and trying to find out what everything does. There are easier ways to do that, sure..
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8967
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I haven't found the proper patch yet but if there's one, I'll find it at all cost smiley
TY though!

I think it may be a "virtual" (vapor?) patch, as in a recent similar String discussion mentioning it, it never appeared.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But...watch post #2..he/she said there's a patch. I don't think that he has thought that out..
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok. i found the Files. All who have the same problems like me when using Strings:
Follow these instructions:
http://code.google.com/p/arduino/issues/detail?id=857

and download the malloc.c file here:

http://code.google.com/p/arduino/issues/detail?id=468

You have to put them here: arduino-1.0.1/hardware/arduino/cores/arduino

A special Thanks to these guys who have written the code as far as I understand;

Joerg Wunsch
Gerben van den Broeke
Paul Stoffregen
Logged

Pages: [1]   Go Up
Jump to: