Skip to content

Instantly share code, notes, and snippets.

@dainiuxt
Created October 21, 2025 14:16
Show Gist options
  • Select an option

  • Save dainiuxt/ae48e2fb4a5733594ea910bd1828ff7e to your computer and use it in GitHub Desktop.

Select an option

Save dainiuxt/ae48e2fb4a5733594ea910bd1828ff7e to your computer and use it in GitHub Desktop.
Get data from Arduino and display it in a radar-like screen from Processing.
/*
Radar Screen Visualization for HC-SR04 + Servo
Uses serial data from Arduino in format "angle,distance."
Displays radar grid, sweeping beam, and obstacle points.
*/
import processing.serial.*;
Serial arduinoPort;
float radius = 350;
int radarRange = 300;
int degree = 0;
int distance = 0;
int[] distances = new int[181];
void setup() {
size(800, 600);
arduinoPort = new Serial(this, Serial.list()[0], 9600);; // adjust to your port
arduinoPort.bufferUntil('.');
smooth();
}
void draw() {
background(0);
drawRadarGrid();
drawSweep();
drawDetectedPoints();
}
// Draw radar circles and angle lines
void drawRadarGrid() {
translate(width/2, height - 50);
noFill();
stroke(0, 255, 0);
strokeWeight(1);
for (int r = 0; r <= radarRange; r += 50) {
ellipse(0, 0, r*2, r*2);
}
for (int a = 0; a <= 180; a += 30) {
float x = cos(radians(a)) * radarRange;
float y = -sin(radians(a)) * radarRange;
line(0, 0, x, y);
}
}
// Draw sweeping beam from center
void drawSweep() {
stroke(0, 255, 0);
for (int i = 0; i < 60; i += 2) {
stroke(0, map(i, 0, 60, 255, 0), 0);
float x = cos(radians(degree - i)) * radarRange;
float y = -sin(radians(degree - i)) * radarRange;
line(0, 0, x, y);
}
}
// Draw detected obstacle dots
void drawDetectedPoints() {
for (int i = 0; i < 180; i++) {
int d = distances[i];
if (d > 0 && d < radarRange) {
float x = cos(radians(i)) * d;
float y = -sin(radians(i)) * d;
stroke(0, 255, 0);
fill(0, 255, 0);
ellipse(x, y, 5, 5);
}
}
}
// Read serial input: "angle,distance."
void serialEvent(Serial port) {
String data = port.readStringUntil('.');
if (data != null) {
data = trim(data);
data = data.replace(".", "");
String[] parts = split(data, ',');
if (parts.length == 2) {
degree = int(parts[0]);
distance = int(parts[1]);
if (degree >= 0 && degree < 180) {
distances[degree] = distance;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment