Processing : drawing radar with sensor

Hi !

Description

I have some problem while doing my project.
I am trying to draw radar with incoming values which is variable(value from two potentiometer)
I got two variable value from my sensor and its form is like “x,y.”.
And I’m trying to draw a line.; ------ line(x,y,x(previous x), y(previous))
Also, I would like to save its log file.

This is Arduino code for potentiometer :

int valx = 0;
int valy = 0;

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

void loop() {
valx = analogRead(A0);
valy = analogRead(A1);

Serial.print(valx);
Serial.print(",");
Serial.print(valy);
Serial.println(".");
delay(1000);
}

This is Processing code I write:

import processing.serial.*;
PrintWriter output;
Serial myPort;
String myString=null;
float x=0;
float y=0;
float pre_data=0;
float inByte = 0;
void setup () {
size (1000,1000);
myPort = new Serial(this, “COM16”, 9600);
output = createWriter(“CNSAT radar log file.txt”);

}

void draw(){
translate(width/2,height/2);
drawrader();
drawtarget();
line(x, y-pre_data, x, y - inByte);
if (x>= width) {
println(“Out of range”)

}else {
}
pre_data = inByte;
}

void drawradar() {
background(0);
noFill();
strokeWeight(2);
stroke(98,245,31);
ellipse(0,0,900,900);
ellipse(0,0,700,700);
ellipse(0,0,500,500);
ellipse(0,0,300,300);
ellipse(0,0,100,100);
strokeWeight(1);
line(-400,-400,400, 400);
line(400,-400,-400,400);
line(0,-400,0,400);
line(-400,0,400,0);
textAlign(RIGHT);
text(“10cm”,50,0);
text(“20cm”,150,0);
text(“30cm”,250,0);
text(“40cm”,350,0);
text(“50cm”,450,0);
}

void drawtarget() {
fill(0,7);
rect(-400,400,width, height);
strokeWeight(3);
stroke(255,0,0);
}

void SerialEvent(Serial p ) {
if(myPort.available() >0){
try{
myString = p.readStringUntil(’.’);
if (myString != null) {
myString = trim(myString);
println(myString);
String[]list = split( myString, ‘,’);//list 1 and list 2 + . is seperated
x = float(list [0]);
y = float(list [1].replace(",",""));
output.println(x+","+","+y+",");
ellipse(x,y,5,5);
line(x,y,4,4);
}
}catch(Exception e ) {
}
}
}

void keyPressed() {
output.flush();
output.close();
}

So, I did research about if and write my code.
However, unfortunately, it failed. (value is incoming but it does not draw line in rader)

And these are Three Questions I have

  1. Is my code is correct to draw radar?
  2. Is my code is correct to save x,y value?

If you have some points take makes you difficult to understand my code please leave message.
I really appreciate for your help! :smile:`

Could you clarify, with a picture or doodle, what you expect it to look like?

Thank you for your interest :smile:

You should not have those lines in your serialEvent() but in draw. Serial evet is a separate thread and it should not touch the main drawing thread. Instead, you can update some global fields and then draw based on those fields.

Maybe you should consider using an array of fixed size to store your data as in this example or you could use a IntList or ArrayList. I believe there are some older posts demonstrating this radar concept. If you still need help with the display let me know and I can put something together for you. If we go this way, I will need that you post some raw data from your device. You can collect this data using the serial monitor from the arduino IDE. You can post it here. By the way, I would need just few seconds of data.

Before I forget. You should consider using a different delimiter for your data. Using , is ok but . as a delimiter is not a good idea. You could go for other options like ; or EOL aka. \n.

Kf

I greatly gratitude for posting some older post of radar concept. (I also did research for a long time but they were not enough to solve my problem.) And here is IDE data from Arduino.

619,285;
628,278;
623,294;
553,260;
440,170;
309,79;
284,49;
177,29;
0,29;
0,30;
122,89;
150,252;
139,156;
137,29;
155,19;
219,30;
290,151;
325,159;
394,161;
385,192;
346,178;
280,184;
154,101;
84,18;
0,1;
0,3;
0,12;
104,135;
147,203;
88,243;
0,153;
0,88;
93,189;
197,377;
338,528;
363,536;
228,426;
86,326;
115,184;
355,49;
532,3;
462,16;
302,198;

Thank you again for your sincere advice :smile:

This is untested code so you might need to tweak this code to get it working. Notice minor changes for ino. For the pde, if you are not familiar with noLoop/redraw, you can check the reference.

There is something I did not understand about your code structure:

line(x, y-pre_data, x, y - inByte);

if (x>= width) {

println(“Out of range”)
}else {

}
pre_data = inByte;

The following changes were made based on your picture above.

Kf

Arduino

const char SEP=',';
const char EOL=';';  //End of line

int valx = 0;
int valy = 0;

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

void loop() {
  valx = analogRead(A0);
  valy = analogRead(A1);

  Serial.print(valx);
  Serial.print(SEP);
  Serial.print(valy);
  Serial.println(EOL);
  delay(1000);
}

PDE file



import processing.serial.*;

final String SEP=",";
final char EOL=';';
final int MAX_ENTRIES=1000;

PrintWriter output;
Serial myPort;
String myString=null;
float x=0;
float y=0;
float pre_data=0;
float inByte = 0;

ArrayList<PVector> data;


void setup () {
  size (600, 400);
  myPort = new Serial(this, "COM16", 9600);
  output = createWriter("CNSAT radar log file.txt");

  data=new ArrayList<PVector>();
  noLoop();  //Sketch will be updated only when new data arrives via redraw()
}

void draw() {
  translate(width/2, height/2);

  drawradar();
  drawtarget();
  saveData();
}

void keyPressed() {
  output.flush();
  output.close();
}




void SerialEvent(Serial p ) {
  if (myPort.available() >0) {

    myString = p.readStringUntil(EOL);
    if (myString != null) {
      myString = trim(myString);
      println(myString);
      String[]list = split( myString, SEP);//list 1 and list 2 + . is seperated
      x = float(list [0]);
      y = float(list [1].replace(",", ""));

      PVector v = new PVector(x, y);
      data.add(v);
      redraw();  //Triggers draw
    }
  }
}

void saveData() {

  //Only save data if max entries reached
  //Saves the data and clears the array
  if (data.size()<MAX_ENTRIES)
    return;

  try {
    for (int i=0; i<data.size(); i++) {
      output.println(x+","+","+y+"\n");
    }
  }
  catch(Exception e ) {
  }

  //Clears array for next chunk of data
  data.clear();
}

void drawtarget() {
  fill(0, 7);
  rect(-width/2, -height/2, width/2, height/2);
  strokeWeight(3);

  fill(200);
  stroke(255, 0, 0);

  //OPTION1: If you want to draw points, you need only the next two lines
  PVector pv=data.get(data.size()-1); //Get last entry
  ellipse(pv.x, pv.y, 5, 5);

  //OPTION2: To draw lines, you need this next 
  if (data.size() <1 ) {
    return; //Nothing to do as not enough points
  }

  PVector secondLast=data.get(data.size()-2);
  PVector last=data.get(data.size()-1);

  line(secondLast.x, secondLast.y, last.x, last.y);
}


void drawradar() {

  background(0);
  noFill();
  strokeWeight(2);
  stroke(98, 245, 31);
  ellipse(0, 0, 900, 900);
  ellipse(0, 0, 700, 700);
  ellipse(0, 0, 500, 500);
  ellipse(0, 0, 300, 300);
  ellipse(0, 0, 100, 100);
  strokeWeight(1);
  line(-400, -400, 400, 400);
  line(400, -400, -400, 400);
  line(0, -400, 0, 400);
  line(-400, 0, 400, 0);
  textAlign(RIGHT);
  text("10cm", 50, 0);
  text("20cm", 150, 0);
  text("30cm", 250, 0);
  text("40cm", 350, 0);
  text("50cm", 450, 0);
}
1 Like

Thank you very much indeed and Thank you again for giving me sincere advice.

For few days, I succeed in understanding code and I tried to solve problem (ArrayIndexoutofBoundsException-1).
As I am learning processing, it is difficult to solve it by myself. Can you please give me some tips to solve this error?

That happens bc the array is empty. You can introduce a check right above this line:

if(data.size()>0) { .... draw data }

Kf