I’m still having a hard time understanding this problem hope you can help me, so I’m writting stuff in the Serial monitor window and I’m able to put that into a array but after, I want to take that result and put it into a char* array, for a reason I don’t understand on the first pass it looks like that assuming that I wrote Red in the terminal and my array already had some stuff into it:
Result:
Red
Green
Yellow
But on the second an others subsequent pass if I write other stuff it looks like that:
Result:
Car
Car
Yellow
It takes the latest stuff that I wrote and instead of putting it at the second position it write it on the first and second, on the third pass on the first,second and third position and so on…
The code looks like that, I’ve put only the section where it writes to the array taking account that the int n was set to 0 in the header.
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars); //The result after /n and adding /0 from the terminal window
Serial.println(n);// printing just for troubleshooting the position in the array
allChars[n]=receivedChars; //allChars is the char* array that I'm trying to write to.
n++; //increment for the next time to write in the next position of the array
int i;
for (i=0; i<4; i++){
Serial.println(allChars[i]); // printing for troubleshooting the whole array allChars
}
newData = false;
}
}
So in your first example receivedChars contains "red" and you make allChars[0] point to receivedChars and when you print allChars[0] it prints "red". But allChars doesn't contain "red" it just points to receivedChars.
Next you put "car" in receivedChars. And allChars[0] still points to receivedChars which no longer contains "red" it now contains "car" so when you print allChars[0] you get "car".
audioguru67:
Ho Man!!!! Now I get it.... I'm so dumb!!!!!
Ok now I understand what I want to do is:
allChars[0]=receivedChars //Red
allChars[1]=receivedChars //Green...and so on
How can I do that?
I don't think you get it. Look at what you wrote. If allChars[0] and allChars[1] are BOTH equal to receivedChars then they can't possibly be one printing red and one printing green. receivedChars can't be both things at once.
I think what you want to do is not to make allChars[0] point to received chars but to a copy of what receivedChars had in it at that point in time. In that case you can use strdup to make a new thing that is a copy of what was in receivedChars and make allChars[0] point to that. But you have to be sure to remember to free that memory when you're done or you're going to crash your arduino. Then receivedChars could get a new value and allChars[0] points to a copy of what receivedChars used to have so printing allChars[0] would print whaqt receivedChars used to contain.
Finally got some result going on with strncpy I was able to copy one array to the other but sometimes I have weird result… I need to find why.
Here is my code for the copy section
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
Serial.println(n);
strncpy(allChars[n], receivedChars, 8);// Copy the content of receivedChars to allChars
n++;
int i;
for (i=0; i<4; i++){
Serial.println(allChars[i]);
}
newData = false;
}
}
Show the whole thing. How is allChars defined? If it is just an array of pointers with no storage allocated then you are stomping on memory you don't own and that can cause all kinds of crazy stuff to happen.
#define MAXLENGTH 33
char allChars[10][MAXLENGTH]; // Room for 10 entries of 32 or few characters, one for NULL
int index;
void setup() {
Serial.begin(9600);
}
void loop() {
char temp[MAXLENGTH];
int charsRead;
int entries = 0;
Serial.println("Enter up to 32 characters and press Enter. Enter # to end.");
while (true) {
if (Serial.available() > 0) {
charsRead = Serial.readBytesUntil('\n', temp, sizeof(temp) - 1);
temp[charsRead] = NULL;
if (temp[0] == '#' || entries > 9) { // Done entering stuff
break;
} else {
strcpy(allChars[entries], temp);
entries++; // Move to next one...
}
}
}
for (int i = 0; i < entries; i++) {
Serial.println(allChars[i]);
}
}
Enter test data on the Serial monitor’s Send textbox, pressing Enter after each entry. Enter ‘#’ and Enter to end the test data stream. See if you can work your way through the code to understand what the other posters are saying.