and in order to clear all the "old" characters when a new string has to be displayed, I have a simple function which add spaces till the end of the lcd.
However for some reason it's not working as expected, it only adds 4 spaces with the word in the example. I can't see where the problem is..
I tried with a normal character 'a' instead of the space and it prints out only 4 'a'.
String make_str(String str){
for(int i = 0; i < (16 - str.length()); i++)
str += ' ';
return str;
}
make_str("Inserire"); //this will return "Inserire____" instead of "Inserire________"
String make_str(String str){
for(int i = 0; i < (16 - str.length()); i++) {
str += ' ';
}
return str;
}
make_str("Inserire"); //this will return "Inserire____" instead of "Inserire________"
make_str is returning a string on each iteration of the for loop at the moment. You need the curly braces around the for loop so it only returns once it's done iterating.
{
lcd.print(str);
for(int i = str.length(); i<16;i++) lcd.print(' ');
}
The function takes a String as a parameter, puts it to lcd, then fills up with space characters to end-of-line.
works similar when using nullterminated strings, but instead "str.length() you'd have to use strlen(str) for getting length of a nullterminated string.
You've received some better ways of achieving what you want, but not an explanation of why it didn't work, which might be useful in future.
You are checking the length of the string while it is expanding, so when you've added 4 spaces it's now 12 long and the 'for' loop stops when i is 4 (16-12). You should have measured the string length just once, before the loop:
String make_str(String str){
int len = str.length();
for(int i = 0; i < (16 - len); i++)
str += ' ';
return str;
}
but as others have said probably best not to use String at all.
Another thing to keep in mind is that, when it comes to string data, there are multiple ways to skin the cat. In most cases, using the String object uses more memory than a simple C string (note lower case 's'). In the program below, the String version uses 3090 bytes:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// String msg = "Test it";
// msg = makeString(msg);
char msg[17] = "Test it";
makeCString(msg, sizeof(msg));
Serial.println("0123456789ABCDEF");
Serial.print(msg);
Serial.println("<");
}
void loop() {
}
/*
String makeString(String str) // String object function
{
for (int i = str.length(); i < 16; i++)
str += ' ';
return str;
}
*/
void makeCString(char str[], int length) // Simple C string function
{
char spaces[16] = " "; // 16 spaces
int end = strlen(str);
strcat(str, &spaces[end]);
}
The simple C string version only uses 1650 bytes. (I don't have an LCD display handy, but this would work on the LCD display, too, with minor changes.)
void makeCString(char str[], int length) // Simple C string function
{
char spaces[16] = " "; // 16 spaces
int end = strlen(str);
strcat(str, &spaces[end]);
}
This is probably a good example of why people start to use String rather than 'simple C strings'.
It's got loads of errors and it doesn't work. In fact it resets the Arduino in my test.
a char of 16 spaces needs an array 17 long, not 16.
you're not using the 'length' argument
most importantly you're doing a strcat() to the end of a fixed-length string, overwriting other variables.
a char of 16 spaces needs an array 17 long, not 16.
My bad, it should be 17 in the function, as it is in setup(). However, since I initialized it with 16 spaces as a string, does the compiler override the 16? I don't know. Even if it does override, it should be 17 if for no other reason than documentation.
quilkin:
you're not using the 'length' argument
Turns out, you don't need to. See next comment.
quilkin:
most importantly you're doing a strcat() to the end of a fixed-length string, overwriting other variables.
I don't think so. By passing in the address of where to start in the spaces[] array (i.e., spaces[len]) I am only going to concatenate part of the spaces[] array, not all of it. For example, if the incoming string is 10 characters long, only the segment of spaces[] from 10-17 is added, which includes the null, and falls within the limits of msg[] as defined in setup().
As I said in the beginning, there are multiple ways of doing what the OP wants to do. If you prefer to wobble along on the crutches provided by the String class, that's fine. To me, wasting that much memory just doesn't make sense.
econjack:
I don't think so. By passing in the address of where to start in the spaces[] array (i.e., spaces[len]) I am only going to concatenate part of the spaces[] array, not all of it. For example, if the incoming string is 10 characters long, only the segment of spaces[] from 10-17 is added, which includes the null, and falls within the limits of msg[] as defined in setup().
No, you're adding a string of up to 16 chars long onto the end of a fixed-length bit of memory which may be only 5 or 6 bytes long. Try this sketch to see what happens:
[code]
void makeCString(char str[], int length)// Simple C string function
{
char spaces[] = " "; // 16 spaces
int end = strlen(str);
strcat(str, &spaces[end]);
}
void setup() {
Serial.begin(9600);
delay(100);
Serial.println("starting:");
char str5[] = "five";
char str6[] = "six";
Serial.println("original strings:");
Serial.println(str5);
Serial.println(str6);
makeCString(str5, 16);
makeCString(str6, 16);
Serial.println("new strings:");
Serial.println(str5);
Serial.println(str6);
}
void loop() {
// put your main code here, to run repeatedly:
}
Of course your code won't work because you allocated memory space equal to the current string content in setup(). In my version, the message string is allocated 17 bytes of space, of which only part is used up by the "Test it" string. My version uses the first 8 bytes (including the null) but has enough allocated space for 9 more characters. Your code has no memory left in the char arrays. You allocated two arrays with 4 and 3 bytes each and then call the function to append spaces to a string that is completely filled up.
I just used the function that you wrote, as any other user might try to do.
If it works, please can you provide a small demo program to show it working?
quilkin:
I just used the function that you wrote, as any other user might try to do.
If it works, please can you provide a small demo program to show it working?
I already did. When you use any function in C, it assumes that you are passing it meaningful parameters. You never gave setup() a place to put the resulting string. Using your example, but adding the string to hold the result, we get:
void makeCString(char str[])// Simple C string function
{
char spaces[] = " "; // 16 spaces
int end = strlen(str);
strcat(str, &spaces[end]);
}
void setup() {
char result[17]; // Room for 16 spaces plus NULL
Serial.begin(9600);
Serial.println("starting:");
char str5[] = "five";
char str6[] = "six";
Serial.println("original strings:");
Serial.println(str5);
Serial.println(str6);
strcpy(result, str5); // Do first string
makeCString(result);
Serial.println("new strings:");
Serial.println("0123456789ABCDEF");
Serial.print(result);
Serial.println("<");
strcpy(result, str6); // Do second string
makeCString(result);
Serial.print(result);
Serial.println("<");
}
void loop() {
// put your main code here, to run repeatedly:
}
Even though you added two strings to the mix and a number of extra print statements, the non-object string version is still over 1200 bytes smaller than the String version. I did remove the length of the input string in the function call since it proved unnecessary.
Fine. So this function works OK if passed a string at least 17 bytes long. Anything shorter and it crashes. The name of the function doesn't say that, nor any comment in it.
Hence my original comment about why people start might using String rather than char arrays. I rest my case.
quilkin:
Fine. So this function works OK if passed a string at least 17 bytes long. Anything shorter and it crashes. The name of the function doesn't say that, nor any comment in it.
Hence my original comment about why people start might using String rather than char arrays. I rest my case.
Really? So I'm supposed to write documentation too? If you're using it with a 16 column display and you need a null termination byte, it would seem the user would know how long the string shoud be to use it safely. If the user is dumb enough to pass a buffer with less space that they intend to write, they deserve to have it crash. If you need training wheels on the function, you write it.