Problem Saving Serial data into an Array

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

        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

        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


-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:

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

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

1 Like