#include <Servo.h>
Servo radarServo;
// Pins
const int servoPin = 6;
const int trigPin = 9;
const int echoPin = 10;
// Sweep settings
int angle = 0;
int stepDir = 1; // +1 goes right, -1 goes left
const int minAngle = 0;
const int maxAngle = 180;
const unsigned long servoIntervalMs = 20; // 20ms = smooth (50 Hz-ish)
unsigned long lastServoMove = 0;
long getDistanceCM() {
// Trigger pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read echo (timeout prevents freezing if no echo)
unsigned long duration = pulseIn(echoPin, HIGH, 30000UL); // 30ms timeout
if (duration == 0) return 999; // out of range / no reading
// Speed of sound ~0.0343 cm/us, divide by 2 for round trip
return (long)(duration * 0.0343 / 2.0);
}
void setup() {
Serial.begin(9600);
radarServo.attach(servoPin);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
radarServo.write(angle);
}
void loop() {
unsigned long now = millis();
// Move servo at a steady interval (smooth back-and-forth)
if (now - lastServoMove >= servoIntervalMs) {
lastServoMove = now;
radarServo.write(angle);
// Take distance reading
long distance = getDistanceCM();
// Send to PC: "angle,distance"
Serial.print(angle);
Serial.print(",");
Serial.println(distance);
// Update angle and bounce at ends
angle += stepDir;
if (angle >= maxAngle) {
angle = maxAngle;
stepDir = -stepDir;
} else if (angle <= minAngle) {
angle = minAngle;
stepDir = -stepDir;
}
}
}
import processing.serial.*;
import java.util.ArrayList;
Serial myPort;
String inData = "";
float angleDeg = 0;
float distanceCm = 0;
int maxCm = 150;
float scale = 3.0;
// ----- trail storage -----
class Hit {
float angle;
float dist;
int born; // millis() when added
Hit(float a, float d) { angle = a; dist = d; born = millis(); }
}
ArrayList<Hit> hits = new ArrayList<Hit>();
int trailMs = 1500; // how long hits stay (ms)
int maxHits = 400; // max stored hits
void setup() {
size(900, 600);
smooth();
pixelDensity(1);
println(Serial.list());
// CHANGE THIS to your port: "COM3" on Windows, "/dev/cu.usbmodem..." on Mac
myPort = new Serial(this, "/dev/cu.usbmodem1101", 9600);
myPort.bufferUntil('\n');
}
void draw() {
background(0);
translate(width/2, height*0.9);
drawRadarGrid();
// Draw sweep line
float theta = radians(angleDeg);
float rMax = maxCm * scale;
stroke(0, 255, 0);
strokeWeight(2);
line(0, 0, -rMax * cos(theta), -rMax * sin(theta));
// Draw hit trail (older hits fade out)
int now = millis();
for (int i = hits.size() - 1; i >= 0; i--) {
Hit h = hits.get(i);
int age = now - h.born;
if (age > trailMs) {
hits.remove(i);
continue;
}
float life = 1.0 - (float)age / trailMs; // 1 -> 0
float r = min(h.dist, maxCm) * scale;
float t = radians(h.angle);
// fade alpha
stroke(255, 0, 0, 255 * life);
strokeWeight(6 * life + 1);
point(-r * cos(t), -r * sin(t));
}
// HUD
resetMatrix();
fill(0, 255, 0);
textSize(18);
text("Angle: " + nf(angleDeg, 0, 0) + "\u00b0", 20, 30);
text("Distance: " + nf(distanceCm, 0, 1) + " cm", 20, 55);
}
void drawRadarGrid() {
stroke(0, 255, 0);
strokeWeight(1);
noFill();
for (int cm = 25; cm <= maxCm; cm += 25) {
float rr = cm * scale;
arc(0, 0, rr*2, rr*2, PI, TWO_PI);
}
for (int deg = 0; deg <= 180; deg += 15) {
float t = radians(deg);
float x = -(maxCm * scale) * cos(t);
float y = -(maxCm * scale) * sin(t);
line(0, 0, x, y);
}
line(-maxCm*scale, 0, maxCm*scale, 0);
float outer = maxCm * scale;
arc(0, 0, outer*2, outer*2, PI, TWO_PI);
}
void serialEvent(Serial myPort) {
inData = myPort.readStringUntil('\n');
if (inData == null) return;
inData = trim(inData);
String[] parts = split(inData, ',');
if (parts.length == 2) {
try {
angleDeg = float(parts[0]);
distanceCm = float(parts[1]);
// Add to trail only if valid
if (distanceCm > 0 && distanceCm < maxCm) {
hits.add(new Hit(angleDeg, distanceCm));
if (hits.size() > maxHits) hits.remove(0);
}
} catch (Exception e) {
// ignore bad lines
}
}
}