import processing.serial.*; // import the serial library for Processing to get data from Arduino code Serial myPort; String angle = ""; // setting variables to store arduino serial data String distance = ""; String data = ""; float pixsDistance; int iAngle = 0; // setting variables to store numeric conversions int iDistance = 0; int index1 = 0; void setup() { size(1440, 900); // declaring screen resolution smooth(); // makes the lines display as smooth shapes myPort = new Serial(this, "/dev/cu.usbserial-1420", 9600); // connecting to serial port 1420 at 9600 baud rate myPort.bufferUntil('\n'); // check serial monitor for new lines } void draw() { noStroke(); // creates a rectangle over the screen fill(0, 4); rect(0, 0, width, height); fill(98, 245, 31); // declares the radar color drawRadar(); // draws radar background drawLine(); // draws sweeping line drawObject(); // draws object detection } void serialEvent(Serial myPort) { // reads serial data for each new line data = myPort.readStringUntil('\n'); if (data != null && data.contains(",")) { data = data.trim(); index1 = data.indexOf(","); // finds comma position angle = data.substring(0, index1); // collects angle from serial data distance = data.substring(index1 + 1); // collects distance from serial data iAngle = int(angle.trim()); // converts to integers iDistance = int(distance.trim()); // converts to integers } } void drawRadar() { pushMatrix(); // saves current state of coordinate system translate(width / 2, height - 100); // sets origin point of radar at bottom center of screen noFill(); strokeWeight(2); // sets line thickness stroke(98, 245, 31); // sets radar color to green // radar arcs arc(0, 0, 800, 800, PI, TWO_PI); // draws radar arcs as semi circles arc(0, 0, 600, 600, PI, TWO_PI); arc(0, 0, 400, 400, PI, TWO_PI); arc(0, 0, 200, 200, PI, TWO_PI); // radar lines line(-width / 2, 0, width / 2, 0); // horizontal bottom line for (int a = 30; a <= 150; a += 30) { // draws line from origin to differentiate angles, from 30 t0 150 degrees float x = -400 * cos(radians(a)); float y = -400 * sin(radians(a)); line(0, 0, x, y); } popMatrix(); // restores previous coordinate state } void drawObject() { pushMatrix(); translate(width / 2, height - 100); // sets origin point strokeWeight(9); // sets line thickness stroke(255, 10, 10); // sets detection color to red pixsDistance = iDistance * 10; // converts distance from cm to pixels if (iDistance < 40) { // if object is detected within 40 cm, draw red detection line line(pixsDistance * cos(radians(iAngle)), -pixsDistance * sin(radians(iAngle)), 380 * cos(radians(iAngle)), -380 * sin(radians(iAngle))); } popMatrix(); } void drawLine() { pushMatrix(); strokeWeight(9); // sets radar sweeping line thickness stroke(30, 250, 60); // sets sweeping line to green translate(width / 2, height - 100); // sets origin line(0, 0, 380 * cos(radians(iAngle)), -380 * sin(radians(iAngle))); // draws sweeping line at current angle popMatrix(); }