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;
}
}
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;
}
}
#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.