Get 4 numbers from Serial

I have a code that can get a number from Serial monitor and print it back. Now I want to get only 4 numbers and after all of them have been entered one by one, print all of them at one go. I am not clear how to modify the code below to achieve this ... tried a for () loop but ended up with funny results :frowning:

The board used is a Adafruit M0 feather.

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data
boolean newData = false;
int dataNumber = 0;             

//#############################

void setup() {
  Serial.begin(9600);
  while(!Serial);
  Serial.println( " Type any number ... "); 
}

//#############################

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

//#############################

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  if (Serial.available() > 0) {
    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 showNewNumber() {
  if (newData == true) {
    dataNumber = atoi(receivedChars); 
    Serial.print("You typed ..");  
    Serial.println(dataNumber);
    newData = false;
    Serial.println( " Type next number.. "); 
  }
}

Example #5 of the serial input basics tutorial shows how to receive and parse several pieces of data from a comma delimited string received over serial using the strtok() function to parse the data. Except that that example also uses start marker.

An example using the example #2 that your code is based off of but including the strtok() function to separate the numbers. There are other ways to do the same thing, but strtok() lets you separate data of different data types. Like in example #5 where a mix of int, float and string data is sent in one comma delimited packet.

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

boolean newData = false;

int number_1;
int number_2;
int number_3;
int number_4;

void setup()
{
   Serial.begin(115200);
   Serial.println("<Arduino is ready>  Enter four integer numbers separated by commas");
   Serial.println("Like 123,56,6789, 10");
   Serial.println("make sure newline is enabled in serial monitor line endings");
}

void loop()
{
   recvWithEndMarker();
   showNewData();
   if (newData)
   {
      parseData();
   }
}

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);
      //newData = false;
   }
}

void parseData()
{
   char *strings[6]; // an array of pointers to the pieces of the above array after strtok()
   char *ptr = NULL; byte index = 0;
   ptr = strtok(receivedChars, ",");  // delimiters, semicolon
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }
   //Serial.println(index);
   // print all the parts
   Serial.println("The Pieces separated by strtok()");
   for (int n = 0; n < index; n++)
   {
      Serial.print("piece ");
      Serial.print(n);
      Serial.print(" = ");
      Serial.println(strings[n]);
   }
   // convert string data to numbers
   number_1 = atoi(strings[0]);
   number_2 = atoi(strings[1]);
   number_3 = atoi(strings[2]);
   number_4 = atoi(strings[3]);   
   Serial.print("number 1 = ");
   Serial.print(number_1);
   Serial.print("   number 2 = ");
   Serial.print(number_2);
   Serial.print("   number 3 = ");
   Serial.print(number_3);
   Serial.print("   number 4 = ");
   Serial.print(number_4);
   Serial.println(); // blank line
   newData = false;
}
1 Like

@groundFungus

Wow that was superfast response. Appreciate that ...! Thanks .

Meanwhile I also tried and got the code below. It does the job...maybe refinements are possible.

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data
boolean newData = false;
int dataNumber = 0;             // new for this version
int parameter[3];
bool f_getNumber;
byte paramNo = 0; 

//===============================
void setup() {
  Serial.begin(9600);
  while (!Serial) {};
  f_getNumber = true;
}

//===============================
void loop() {

  if ( f_getNumber) {
      recvWithEndMarker();
      storeNewNumber();
    }
}
//===============================
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  if (Serial.available() > 0) {
    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 storeNewNumber() {
  if (newData == true) {
    parameter[paramNo] = atoi(receivedChars);   // new for this version
    newData = false;
    Serial.println( " Type next number");
     paramNo++;
    if ( paramNo > 3 ) {
      Serial.println( parameter[0]);
      Serial.println( parameter[1]);
      Serial.println( parameter[2]);
      Serial.println( parameter[3]);
      f_getNumber = false;
      paramNo = 0; 
    }
  }
}

Yes, like I said, there are different ways to get there. What works for you, works for you. Using an array or arrays is good if you have data of one data type. Arrays will not work for mixed type data.

You are welcome. I have a store of code that I use a lot. That was already pretty much written. It did not take much to customize it for your use.

1 Like