Problem Saving Serial data into an Array

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

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) {
    }
  }
}
1 Like

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

Try this:

// 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]);
    }
  }
}
1 Like

Hello,

See:

:slight_smile:

1 Like