How to store serial data into an array

Hey guys I'm new to arduino and all of the programming. I have very little experience with this but I need to know, how can I store data from the serial monitor into an empty array. I'm trying to build the code for a matrix keyboard connected to the Arduino UNO which will be used to input a 4 digit number. Another 4 digit number will be input into the serial monitor and then, both the "external" number and the serial monitor's number will be compared. If they're equal, an LED will be turned on. The number from the Serial Monitor must be stored in an array and that's where I'm lost, i simply don't know how to do it. I'm not using any special library (my professor told me not to use any) therefore everything is built with "if" and for loops. I might sound dumb but I truly don't know how to do this so I'd be very glad if you could help me with this. Thank you so much.

Have a look at serial basics (and Example 2 - Receiving several characters from the Serial Monitor)

J-M-L:
Have a look at serial basics (and Example 2 - Receiving several characters from the Serial Monitor)

I tried to run the sketch program example2 but the results did not appear in the serial monitor, how it works from the source
I fill the value on the serial monitor <47,11,1,2,3,9,29,0> because I want the array to contain the data
47
11
1
2
3
9
29
0
thank you for your help

de2r:
I tried to run the sketch program example2 but the results did not appear in the serial monitor, how it works from the source
I fill the value on the serial monitor <47,11,1,2,3,9,29,0> because I want the array to contain the data
47
11
1
2
3
9
29
0
thank you for your help

Did you set the serial monitor mode to newline? It is on the bottom right of the serial monitor. The default is no line ending.

cyberjupiter:
Did you set the serial monitor mode to newline? It is on the bottom right of the serial monitor. The default is no line ending.

Thanks for the help, the program is running well
but I have a problem adding a bit of the program to the sketch to see if the array contains the data as it is written on the serial monitor

void showNewData() {
   if (newData == true) {
       Serial.print("This just in ... ");
       Serial.println(receivedChars);
       Serial.println(receivedChars[ndx]);
       newData = false;
   }
}

on the error message screen

sketch_nov01b:46: error: 'ndx' was not declared in this scope

  • Serial.println(receivedChars[ndx]);*
  • ^*
    exit status 1
    'ndx' was not declared in this scope

Post the full code so we can see everything. I believe 'ndx'(based on Robin2 serial input tutorials) is only declared in a function called 'recvNewData()' or something like that. Why would you print 'ndx' anyway? It only contains the last character you received through serial monitor.

cyberjupiter:
Post the full code so we can see everything. I believe 'ndx'(based on Robin2 serial input tutorials) is only declared in a function called 'recvNewData()' or something like that. Why would you print 'ndx' anyway? It only contains the last character you received through serial monitor.

the full code like this,thanks for help

// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

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

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
   
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        Serial.println(receivedChars[ndx]);
        newData = false;
    }
}

You see the problem yet?

'ndx' is declared in a function called 'recvWithEndMarker()'. Once the function ends, the variable does not exist anymore until the next function call. What you were trying to do is accessing a non-existent variable.

One way to solve this is to make 'ndx' a global variable like this

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data
static byte ndx = 0;

boolean newData = false;

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

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    char endMarker = '\n';
    char rc;
   
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        Serial.println(receivedChars[ndx]);
        newData = false;
    }
}

That will work. But you have not explained why are you doing this and what you expected it to do..

Serial.println(receivedChars[ndx]);

cyberjupiter:
You see the problem yet?

'ndx' is declared in a function called 'recvWithEndMarker()'. Once the function ends, the variable does not exist anymore until the next function call. What you were trying to do is accessing a non-existent variable.

One way to solve this is to make 'ndx' a global variable like this

const byte numChars = 32;

char receivedChars[numChars];   // an array to store the received data
static byte ndx = 0;

boolean newData = false;

void setup() {
   Serial.begin(9600);
   Serial.println("");
}

void loop() {
   recvWithEndMarker();
   showNewData();
}

void recvWithEndMarker() {
   char endMarker = '\n';
   char rc;
 
   while (Serial.available() > 0 && newData == false) {
       rc = Serial.read();

if (rc != endMarker) {
           receivedChars[ndx] = rc;
           ndx++;
           if (ndx >= numChars) {
               ndx = numChars - 1;
           }
       }
       else {
           receivedChars[ndx] = '\0'; // terminate the string
           ndx = 0;
           newData = true;
       }
   }
}

void showNewData() {
   if (newData == true) {
       Serial.print("This just in ... ");
       Serial.println(receivedChars);
       Serial.println(receivedChars[ndx]);
       newData = false;
   }
}




That will work. But you have not explained why are you doing this and what you expected it to do..


Serial.println(receivedChars[ndx]);

I've tried it, I just want to make sure only and it turns out after the upload results on serial monitor like this

This just in ... <47,11,1,2,1,13,10,38,0>
<

once again I thank you for helping and responding to my question

So does that result satisfy you or not?. If it doesn't you would have to explain what you ACTUALLY want to do and what is the expected result.