1
votes

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
Can you perhaps show the output? Just the terminal. - Afridi Kayal
Yes here I paste you a few lines 82 45 184 \n 45 0 \n 81 45 242 \n 45 0 \n 80 49 237 \n 49 0 \n 79 49 231 \n 49 0 \n The lines of three elements are the input data parsed, so you can see that processing gets it, the lines of two elements are the print() of the array, as you can see, it only saves the first value, the second is kept at 0 - Victor Casado
I think the problem might be with trimming. I just tried to assign String v = "15 "; in Processing and print(int(v)); It gave me a result of 0. I am quite unsure what the problem is with your code but I think int(serialdata[2]) is returning zero. Try trimming the strings before converting them to int - Afridi Kayal

1 Answers

0
votes

I think this should work. Try printing the values in integer form to make sure the conversions are correct. int(string) returns 0 in processing if the string is not convertible. For example, when the string contains extra spaces or \n or even inconvertible forms like int("abc") returns 0.

while (port.available() > 0) {
    serialin = port.readStringUntil(10);
    try {
      String serialdata[] = splitTokens(serialin, ",");
      if (serialdata[0] != null) {
        // Try adding these lines.
        serialdata[0] = trim(serialdata[0]);
        serialdata[1] = trim(serialdata[1]);
        serialdata[2] = trim(serialdata[2]);

        print(serialdata[0]);
        print("   ");
        print(serialdata[1]);
        print("   ");
        print(serialdata[2]); // Notice you are printing serialdata[2] in string form so this statement shows the correct value
        print("   ");

        // Try printing these values to confirm wether the conversions are correct
        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]);
      }
    }