This is being done on a button interupt. It works to an extent, each time the button is pressed it updates the lcd, however it seems to concatenate forever as far as I can tell based on whats coming out on the lcd.
The first time I would get what I expect, but the second time I get another ':' and the value stored in the next Camelot[navigator]
I thought that
string = "";
was going to clear the string for the next update of the lcd, however it doesnt by the looks of things??
This is being done on a button interupt. It works to an extent, each time the button is pressed it updates the lcd, however it seems to concatenate forever as far as I can tell based on whats coming out on the lcd.
The first time I would get what I expect, but the second time I get another ':' and the value stored in the next Camelot[navigator]
I thought that
string = "";
was going to clear the string for the next update of the lcd, however it doesnt by the looks of things??
What is "string"? If it a char* as you suggest in the subject, then use memset() or set the first byte of the pointer to "\0"
Have you used Serial.print to check that string is what you think it is? Do you set the cursor between writes (lcd.home() or something)? It may be that concatenation happens because you don't return the cursor and/or clear the lcd between writes.
Anyway, the way use the "string" variable seems suspicious:
The code may seem to work even if you haven't allocated enough memory for string. if it overflows (strcat writes beyond the allocated memory), depending on what variables etc. the memory holds it may or may not crash. But you really should make sure you have allocated enough memory, e.g.,
draw is the function to draw to the lcd - its a graphical lcd, so it redraws the entire screen.
However if I try to allocate memory to it like char* string[20] or char string[20], I get the error:
incompatible types in assignment of 'char*' to 'char [20]
However if I try to allocate memory to it like char* string[20] or char string[20], I get the error:
incompatible types in assignment of 'char*' to 'char [20]
On what line do you get the error "incompatible types in assignment of 'char*' to 'char [20]"? I don't know what the signature of draw function in your code is, but I'm pretty sure the piece of code I posted works as I explained.
Did you declare "string" both as "char *string;" and "char string[20];"? You should only keep the latter. And 20 here is only an example. Make sure that the string is long enough to hold the whole text you want to display.
So what happens when you make a char pointer named string and then you try and make a char array named string?
No I haven't declared two variables called string of different types, the code is as is in my last post however I replaced the line char* string with the variants I posted after.
The code worked fine before I wanted to concatenate strings. At that point it didnt seem to clear each time it was called.
First time I would see the string
Did you try the piece of code I suggested? The way you manipulate the string in the code you posted is wrong. It may seem to work at some extent, but will surely mess up the memory.
You are still not allocating memory for string. When you do the assignment:
string = Chord[navigator];
you start using the memory allocated for Chord[navigator]. Lets assume that the initial value of Chord[navigator] is "foo" and Camelot[navigator] is "bar".
After your code executes the strcat statements, both string and Chord[navigator] point to char array "foo : bar". (And probably writes beyond the memory you allocated for Chord[navigator]).
When your code runs the strcat for second time (e.g. in the loop fuction) you are concatenating "foo : bar", ": " and "bar". And after third iteration string will be "foo: bar : bar : bar" and so on. Seems to be pretty much what you said you saw on the lcd
With each iteration the string keeps growing untill it runs beyond the Arduino's address space. At that point, if not earlier, you sketch will crash.
A (char *) points to a char variable. Usually the char variable is in an array of char variables, possibly a many-dimension array. Without a char variable to point to, a (char *) isn't much use.
You're supposed to set up the spaces and pointers for the functions to use.
Ahh sorry guys I miss understood that I needed to strcat the first one as well. Strings have always given me a little bit of confuson.
Thanks for all your help on this.
EDIT:
No sorry that still continues to ammend the string (the string is globally defined) I need to clear it
I tried: