import processing.serial.
*; // Import the serial library
Serial port; // Serial port object
float angle = 0; // Current sensor angle
float distance = 0; // Current distance measurement
int radarRadius = 300; // Size of radar display
boolean isConnected = false; // Track connection status
void setup() {
size(800, 600); // Window size (width, height)
smooth(); // Enable anti-aliasing
println("Available serial ports:");
printArray(Serial.list()); // Show all available ports
try {
// Connect to Arduino on COM3
port = new Serial(this, "COM3", 9600);
port.bufferUntil('.'); // Wait for '.' before processing
isConnected = true;
println("Connected to COM3");
catch (Exception e) {
println("Error connecting to COM3:");
println(e.getMessage());
println("Trying common alternatives...");
// Try common COM ports
String[] commonPorts = {"COM4", "COM1", "COM5", "COM2"};
for (String testPort : commonPorts) {
try {
port = new Serial(this, testPort, 9600);
port.bufferUntil('.');
isConnected = true;
println("Connected to " + testPort);
break;
catch (Exception ex) {
println(testPort + " failed");
if (!isConnected) {
println("Could not connect to any port. Check Arduino connection.");
// Set up text rendering
textSize(16);
textAlign(CENTER, CENTER);
}
void draw() {
background(0); // Black background
if (!isConnected) {
showConnectionError();
return; // Skip drawing if not connected
drawRadar(); // Draw radar display
drawInfoPanel(); // Draw data panel
void drawRadar() {
// Move origin to bottom center
pushMatrix();
translate(width/2, height - 50);
// Draw radar sweep background
noStroke();
fill(0, 80, 0, 150);
arc(0, 0, radarRadius*2, radarRadius*2, -PI, 0);
// Draw angle lines (0° to 180°)
stroke(100, 255, 100);
for (int i = 0; i <= 180; i += 30) {
float x = radarRadius * cos(radians(i));
float y = radarRadius * sin(radians(i));
line(0, 0, x, -y);
text(i + "°", x + 15, -y - 10);
// Draw distance circles (50cm intervals)
noFill();
stroke(100, 255, 100);
for (int r = 50; r <= radarRadius; r += 50) {
ellipse(0, 0, r*2, r*2);
text(r + "cm", 0, -r - 10);
// Draw sweep line (current sensor direction)
stroke(0, 255, 0, 150);
float sweepX = radarRadius * cos(radians(angle));
float sweepY = radarRadius * sin(radians(angle));
line(0, 0, sweepX, -sweepY);
// Draw detected objects
if (distance > 0 && distance < radarRadius) {
float objX = distance * cos(radians(angle));
float objY = distance * sin(radians(angle));
// Object with distance-based size
float objSize = map(distance, 0, radarRadius, 25, 8);
fill(255, 50, 50, 200);
noStroke();
ellipse(objX, -objY, objSize, objSize);
// Distance label
fill(255);
textSize(12);
text(nf(distance, 0, 1) + "cm", objX + 20, -objY);
popMatrix();
void drawInfoPanel() {
// Status header
fill(100, 255, 100);
textSize(20);
text("ARDUINO RADAR SYSTEM", width/2, 30);
// Data display
fill(200);
textSize(16);
text("Angle: " + nf(angle, 0, 1) + "°", width/2, 70);
text("Distance: " + (distance > 0 ? nf(distance, 0, 1) + "cm" : "No object"), width/2, 100);
// Connection status
fill(isConnected ? color(0, 255, 0) : color(255, 0, 0));
text("COM3: " + (isConnected ? "CONNECTED" : "DISCONNECTED"), width/2, 130);
void showConnectionError() {
fill(255, 0, 0);
textSize(24);
text("ARDUINO NOT CONNECTED", width/2, height/2 - 50);
fill(200);
textSize(18);
text("1. Check Arduino is connected to USB", width/2, height/2);
text("2. Verify COM port in Arduino IDE", width/2, height/2 + 40);
text("3. Close Arduino IDE before running", width/2, height/2 + 80);
text("4. Restart this sketch after connection", width/2, height/2 + 120);
void serialEvent(Serial port) {
try {
String data = port.readStringUntil('.');
if (data == null) return;
data = data.trim(); // Remove whitespace
println("Raw data: \"" + data + "\""); // Debug output
// Split angle and distance values
String[] values = split(data, ',');
if (values.length == 2) {
angle = float(values[0]);
distance = float(values[1]);
catch (Exception e) {
println("Error processing data: " + e);
void keyPressed() {
// Press 'R' to refresh connection
if (key == 'r' || key == 'R') {
println("Attempting reconnection...");
try {
port = new Serial(this, "COM3", 9600);
port.bufferUntil('.');
isConnected = true;
println("Reconnected to COM3");
catch (Exception e) {
println("Reconnection failed");
isConnected = false;