Reading an "array" of numbers through serial

Hello everybody,
I'm trying to interface a device to Arduino that outputs data in the following format over serial: a,a Both numbers are always the same and they range from 0 to 2500. I want to write them on a LCD that's on the LCD Keypad shield (using LCD-4bit_mod library), plus a maximum value and use one of the keys to reset the max value. I pretty much finished everything, but I'm still stuck at actually reading the value. I googled "reading string over serial", "reading array over serial" and bunch of other things and I tried some code I found. However none worked. What code should I use? E.g. are there any functions to do this like in Processing (readStringUntil and split)? Thanks.

You might be a little clearer on what is actually being sent. Will the number (0-2500) be represented by a single character/byte like "a", or will a string of characters be sent that represent the number such as "1500" or a hex character representation? The serial receiving on an arduino is usually one character/byte at a time.

This function print an array on serial monitor as a table size N/2, 10 elements in a row:

// x command - printout data received from ADC (input raw data).
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if (incomingByte == 'x') {
      for (i=0; i<N/2; i++){
        Serial.print("\t");
        Serial.print(x[i], DEC);
        
        if ((i+1)%10 == 0) Serial.print("\n");
      } 
      Serial.print("\n");
      Serial.println(" Here you are.");
      delay(200);
    }

Make your application send an "x", and it will get in response from arduino an array x (or whatever array name you are using). Tabulation \t will help sort out elements on the other end by application, or you can use "," as a separator. And of course you can remove

  • if ((i+1)%10 == 0) Serial.print("\n"); - which is useful only for Human eyes.*

The things that are being sent are just number comma number. Ranging from 0,0 to 2500,2500 (I have no idea why do they send it twice) The sending device is, well, proprietary and can't be modified.

I don't know anything about writing to an LCD device, but the code below takes a string sent from the serial monitor and then writes back to the serial monitor. You might add the writing to the LCD to the code for testing.

//zoomkat 9-9-10 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

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

void loop() {

        //expect a string like wer,qwe rty,123 456,hyre kjhg,
        //or like hello world,who are you?,bye!,
        while (Serial.available()) {
        delay(1);  //small delay to allow input buffer to fill
    	char c = Serial.read();  //gets one byte from serial buffer
        if (c == ',') {break;}  //breaks out of capture loop to print readstring
        readString += c; //makes the string readString
        }
      
      if (readString.length() >0) {
      Serial.println(readString); //prints string to serial port out
      
      //do stuff here
      
      readString=""; //clears variable for new input
      }
   }

This should be exactly what I need! Many thanks! I'll try it and post the results.

EDIT: Done, the last thing would be converting the string your code returns it into an int, so a) LCD-4bit_mod can use it (doesn't support strings...) and b) So I can compare it to other ints (eg. if(a > maxa) {a = maxa} )

The toCharArry() method of the String class and the atoi() function are what you need.

Wow, looks like somebody else has the same problem!

Sorry, I wanted to ask the new question in a more appropriate section.