I am trying to write an Arduino code that reads a long string, example <11,22,33>, through the Serial Monitor and then stores the 11,22, and 33 into separate bins of a matrix array.

So the user could input: <11,22,33> And the code would store the three entries into a matrix of strings:




Whenever I try to run the code the only result that's outputted is




But the strange part is when I put print commands after the Matrix[i]=data line in the code, the output shows the correct numbers! I'm not sure where the issue is and if somehow I made all entries the final entry. Any help is appreciated!

// Example 3 - Receive with start- and end-markers

const byte numChars = 32;
char data[numChars];    //data entry for appending Serial data
char *matrix[numChars]; //matrix of data entries
static byte i=0;  //matrix index

boolean newData = false;

void setup() {
    Serial.println("<Arduino is ready>");

void loop() {

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ind = 0;  //data index
    char startMarker = '<';
    char midMarker = ',';
    char endMarker = '>';
    char rc;    //Serial read character

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc == startMarker) {
            recvInProgress = true;  //if startMarker arrives then enter secondary if below

        else if (recvInProgress) {  //begin appending characters if startMarker activated switch

            if (rc == midMarker) {  //if midMarker is reached...
                matrix[i]=data;     //append data to i-th entry in matrix
                i++;                //add to matrix index
                data[0] = '\0';     //clear data
                ind = 0;            //reset data index
            else if (rc == endMarker) {     //if endMarker is reached...
                matrix[i]=data;             //append data to last entry in matrix (ensures last data is appended)
                data[ind] = '\0';           // terminate the string
                recvInProgress = false;     
                ind = 0;                    //reset counters for data and matrix
                i = 0;
                newData = true;
            else {
                data[ind] = rc;           //eppend Serial character into data
                ind++;                    //add to data index
                if (ind >= numChars) {
                    ind = numChars - 1;

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        newData = false;

You define pointers for each value as :

char *matrix[numChars]; //matrix of data entries

However, in all cases you let it point to data:


Than you set the value starting from that address. So what happens is after you read the first value (11), and matrix[0] contains '11', than matrix[1] points to the same data which you write the value 22 to. And you do the same for 33. So you end up with 3 pointers to the same address (data) containing/overwriting the last value (33). So you get three times 33.

You should either continuing writing the string in data and refer to the correct part inside the string data. If you want to do a Serial.println, you have to end the strings with \0, so the string data and matrix references should be:

index:    0   1  2  3  4  5  6  7  8
data:     1   1 \0  2  2 \0  3  3 \0
matrix:  [0]       [1]      [2]
i:        0         1        2

It will help if you change



  • I'm not sure I understand your corrections. If I change matrix[i]=data to matrix[i]=&(data[ind]) what's printed is blank.
    – mojojojo
    Nov 7 '19 at 22:26
  • I didn't try the program, so it's a bit hard to verify out of my head. I think you should change the line at the end of my answer in both cases (if (rc == midMarker) and (rc == endMarker). Also in the midMarker case, I think you have to put i++ before the matrix[i] assignment. However, I suggest you test this function not on an Arduino but in e.g. VisualStudio on a PC. Call the function and step with a debugger through the code so you can see immediately what is wrong. Nov 7 '19 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.