# Problem Saving Serial data into an Array

**URL:** <https://discourse.processing.org/t/problem-saving-serial-data-into-an-array/17003>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [January 10, 2020, 4:01pm UTC](https://discourse.processing.org/t/problem-saving-serial-data-into-an-array/17003 "2020-01-10T16:01:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hachimbala](https://avatars.discourse-cdn.com/v4/letter/h/cdc98d/32.png) [@hachimbala](https://discourse.processing.org/u/hachimbala)\
**Post date:** [January 10, 2020, 4:01pm UTC](https://discourse.processing.org/t/problem-saving-serial-data-into-an-array/17003/1 "2020-01-10T16:01:59Z")

</div>

Hello, I am developing an Arduino 360 radar, my arduino is sending correctly the info, and the Processing app is receiving it correctly too, but when I try to save the data into an array, it doesn’t update the values.

The device has two HCSR04 placed back to back and a servo that rotates from 0 to 179 and back to 0, the Arduino send the data “Angle,Distance\_1,Distance\_2” over the serial, and Processing gets it without problem, you can see the data received and parsed in the serial console using the lines

```
    print(serialdata[0]);
    print(" ");
    print(serialdata[1]);
    print(" ");
    print(serialdata[2]);"

```

where serialdata[0] takes the angle, serialdata[1] the first distance and serialdata[2] the second distance, so the data is received correctly, then I want to save it in an array of 360 elements(one per each degree), the first distance is saved correctly (0 to 179) but the second distance is not updatet, it is kept at 0 as I inicialize the array in the setup() function, so I think that the problem is in the line “data[i+180] = int(serialdata[2]);”, I have tried doing “data[int(i+180)] = int(serialdata[2]);”, “data[i+int(180)] = int(serialdata[2]);” but I haven’t solved it, what am I doing wrong?

Thank you so much

```auto
import processing.serial.*; // invocar la librería Serial

Serial port; // se declara una variable para la com. serial
String serialin;
int data[] = new int[360];
PFont f;

void setup() {
  port = new Serial(this, "COM3", 9600); 
  size(1280, 720);
  f = createFont("Arial", 16, true); // Arial, 16 point, anti-aliasing on
  textFont(f, 36);
  frameRate(60);
  for (int i = 0; i < 360; i++) {
    data[i] = 0;
  }
}

void draw() {
  background(26, 26, 36, 200);
  textSize(32);
  stroke(255, 255, 255, 150);
  fill(26, 26, 36, 200);
  strokeWeight(3);
  text("ghJ", 1150, 690);
  circle(640, 360, 600);
  circle(640, 360, 500);
  circle(640, 360, 400);
  circle(640, 360, 300);
  circle(640, 360, 200);
  circle(640, 360, 100);

  for (int i = 0; i < 360; i++) {
    point(640 + (100+data[i])*cos(radians(i)), 360 + (100+data[i])*sin(radians(i)));
  }

  while (port.available() > 0) {
    serialin = port.readStringUntil(10);
    try {
      String serialdata[] = splitTokens(serialin, ",");
      if (serialdata[0] != null) {
        print(serialdata[0]);
        print(" ");
        print(serialdata[1]);
        print(" ");
        print(serialdata[2]);
        print(" ");
        int i = int(serialdata[0]);
        data[i] = int(serialdata[1]);
        data[i+180] = int(serialdata[2]);

        print(data[int(serialdata[0])]);
        print(" ");
        println(data[int(serialdata[0])+180]);
      }
    } 
    catch (java.lang.RuntimeException e) {
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 11, 2020, 1:04am UTC](https://discourse.processing.org/t/problem-saving-serial-data-into-an-array/17003/2 "2020-01-11T01:04:34Z")

</div>

hi, looks like a interesting project,  
sorry i wait so long to answer but i expected someone might see the problem,  
because i not see it ?

-a- yes, the code regarding your question

```auto
        int i = int(serialdata[0]);
        data[i] = int(serialdata[1]);
        data[i+180] = int(serialdata[2]);

```

looks good  
( as long you not have communication problems )  
still i would recommend to add a line for recheck the “i range” like

```auto
        int i = int(serialdata[0]);
        if ( i >0 && i < 180 ) { 
          data[i] = int(serialdata[1]);
          data[i+180] = int(serialdata[2]);
        }

```

* * *

-b- and again, as you NOT say that you have communication problems  
also following ideas are not that relevant, but just take a look:  
recommend:  
-1- a `trim()`  
-2- a `if ( serialin != null )`  
-3- a check if data existing prior to ask for them `if ( data.length >=3 )`  
-4- use the [https://processing.org/reference/libraries/serial/serialEvent\_.html](https://processing.org/reference/libraries/serial/serialEvent_.html)

* * *

-c- can there be a timing problem?  
at what rate that lines come in?  
because the good but heavy printing  
might conflict with 100 lines per sec…

* * *

update to -b-

arduino code:

```auto
int dt = 100; // msec between sending

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

void loop() {
  Serial.print( analogRead(0) ); // A0
  Serial.print(",");
  Serial.print( analogRead(1) ); // A1
  Serial.print(",");
  Serial.print( analogRead(2) ); // A2
  Serial.println(",");
  delay(dt); // sample rate
}

/*
// with A0 jumper to 5V see like
  1023,885,788,
  1023,884,786,
  1023,884,786,
  1023,883,786,
  1023,885,788,
*/

```

processing code

```auto
import processing.serial.*;

Serial myPort;
String datas; // _________________________________________ latest arduino text line
int[] datai; // __________________________________________ as array of integers
boolean log = true;//false; // ___________________________ diagnostic print

void setup_serial() { // _________________________________ USB arduino..
  printArray(Serial.list());
  String portName = Serial.list()[1]; // _________________ adjust 0.. x port
  myPort = new Serial(this, portName, 115200);
  myPort.clear();
  myPort.bufferUntil('\n');
  println("try connect to "+portName);
}

void serialEvent(Serial p) { // __________________________ handle serial data
  datas = trim(p.readStringUntil('\n'));
  if (datas != null) {
    if ( log ) println(datas); // ________________________ optional print every GOOD line
    datai = int( split(datas, ",") ); // _________________ create int array from CSV type line
    if ( datai.length >= 3 && log ) { // _________________ if find min 2 "," and understood 3 integer
       for ( int i=0; i < datai.length; i++ ) print(datai[i]+" , ");
       println();
    } else println("shortline");
  }
}

void setup() {
  size(100, 100);
  setup_serial();
}

void draw() {
  background(0, 0, 80);
}

/* // prints:
[0] "COM1"
[1] "COM7"
try connect to COM7
1023,979,940,
1023 , 979 , 940 , 0 , 
1023,979,941,
1023 , 979 , 941 , 0 , 
616,
shortline
1023,913,815,
1023 , 913 , 815 , 0 , 
1023,964,901,
1023 , 964 , 901 , 0 , 
1023,976,929,
 */

```

tested win 10 / processing 3.5.3  
arduino 1.8.10 / arduino leonardo

---

<div class="post-metadata">

**Author:** ![f42ter](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f42ter/32/16793_2.png) [@f42ter](https://discourse.processing.org/u/f42ter)\
**Post date:** [January 11, 2020, 6:17am UTC](https://discourse.processing.org/t/problem-saving-serial-data-into-an-array/17003/3 "2020-01-11T06:17:47Z")

</div>

Try this:

```auto
// Serial Communication
Serial myPort;
String[] msg=new String[7];
int[] value=new int[7];

bool debug = true; 
// something cut here 
// .....
// something cut here

void serialEvent(Serial p) { 
  String inString = p.readString(); 
  if (inString != null) {
   print(inString);
    inString=trim(inString);
    msg = split(inString, ',');
    for (int i=0; i < msg.length; i++ ) {
      value[i]=int (msg[i]);
      if (debug) println(value[i]+":"+i);
// println(msg[i]);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 11, 2020, 3:40pm UTC](https://discourse.processing.org/t/problem-saving-serial-data-into-an-array/17003/4 "2020-01-11T15:40:18Z")

</div>

Hello,

See:

> [@Problem i face in serial communication](https://discourse.processing.org/t/problem-i-face-in-serial-communication/17023/2):
>
> Hello, You receive a string which includes a newline character ( ‘\n’ ) You need to remove the newline character ( ‘\n’ ) that is in the received string before splitting it so you can convert to an int. Example: [https://processing.org/reference/trim\_.html](https://processing.org/reference/trim_.html)[https://processing.org/reference/String.html](https://processing.org/reference/String.html) (discusses newline character and escape sequences such as \n) slight_smile String s1 = "123,456,890\n"; String [] list1 = split(s1, ','); printArray(list1); println(int(list1[2])…

🙂
