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