#include
const int trigPin = 10;
const int echoPin = 11;
const int joyX = A0; // Joystick Pin
long duration;
int distance;
int servoPos = 90; // Starting position
bool autoMode = true; // Track if we are in auto-sweep
Servo myServo;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
myServo.attach(12);
myServo.write(servoPos);
}
void loop() {
int xVal = analogRead(joyX); // Read Joystick
// CHECK FOR USER OVERRIDE
// If joystick is moved significantly left (<300) or right (>700)
if (xVal < 300 || xVal > 700) {
autoMode = false;
// Manual Control Logic
if (xVal < 300 && servoPos > 15) servoPos -= 2;
if (xVal > 700 && servoPos < 165) servoPos += 2;
myServo.write(servoPos);
sendData(servoPos);
delay(20);
}
else {
// RESUME AUTO-SCAN
// This logic performs one "step" of the sweep per loop
static int direction = 1;
servoPos += direction;
if (servoPos >= 165 || servoPos <= 15) {
direction *= -1; // Reverse direction
}
myServo.write(servoPos);
sendData(servoPos);
delay(30);
}
}
// Helper function to keep the loop clean
void sendData(int angle) {
distance = calculateDistance();
Serial.print(angle);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
int calculateDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2;
}
Code for UI
import processing.serial.*;
Serial myPort;
String angle="", distance="", data="";
int iAngle = 0, iDistance = 0;
int index1=0;
// Theme Colors
color clrBG = color(10, 15, 20);
color clrMain = color(0, 255, 150); // Mint glow
color clrObject = color(255, 50, 50); // Danger red
color clrGrid = color(0, 255, 150, 50);
void setup() {
size(1200, 700);
smooth(8);
// Try-Catch prevents the grey screen freeze if COM3 is busy or missing
try {
myPort = new Serial(this, "COM3", 9600);
myPort.bufferUntil('.');
} catch (Exception e) {
println("Serial Error: Check COM3 connection.");
}
}
void draw() {
// Motion blur trail
noStroke();
fill(clrBG, 25);
rect(0, 0, width, height);
// Center coordinates for the radar
pushMatrix();
translate(width/2, height - 100);
drawGrid();
drawSweepLine();
drawTarget();
popMatrix();
drawHUD();
}
void serialEvent (Serial myPort) {
data = myPort.readStringUntil('.');
if (data != null) {
data = data.substring(0, data.length()-1);
index1 = data.indexOf(",");
if (index1 > -1) {
iAngle = int(data.substring(0, index1));
iDistance = int(data.substring(index1+1));
}
}
}
void drawGrid() {
noFill();
strokeWeight(1);
stroke(clrGrid);
// Concentric Arcs
for (int i = 1; i <= 4; i++) {
float d = (width - 150) * (i * 0.25);
arc(0, 0, d, d, PI, TWO_PI);
}
// Angle Lines
for (int a = 30; a <= 150; a += 30) {
line(0, 0, (width/2) * cos(radians(a+180)), (width/2) * sin(radians(a+180)));
}
}
void drawSweepLine() {
// Glow effect: Draw multiple lines with decreasing opacity
for (int i = 0; i < 10; i++) {
stroke(clrMain, 255 - (i * 25));
strokeWeight(10 - i);
line(0, 0, (height - 150) * cos(radians(iAngle + 180)), (height - 150) * sin(radians(iAngle + 180)));
}
}
void drawTarget() {
// Convert cm to pixels (matching your original scaling)
float r = map(iDistance, 0, 40, 0, width/2 - 75);
if (iDistance < 40 && iDistance > 0) {
float x = r * cos(radians(iAngle + 180));
float y = r * sin(radians(iAngle + 180));
// Draw the object blip
fill(clrObject);
noStroke();
ellipse(x, y, 15, 15);
// Danger line
stroke(clrObject, 150);
strokeWeight(2);
line(x, y, x, 0);
}
}
void drawHUD() {
// Bottom Bar
fill(15, 25, 35);
noStroke();
rect(0, height - 70, width, 70);
fill(clrMain);
textSize(16);
textAlign(LEFT);
text("RADAR SYSTEM: ONLINE", 40, height - 30);
textAlign(CENTER);
textSize(24);
text("ANGLE: " + iAngle + "°", width/2 - 100, height - 30);
// Alarm color for close objects
if (iDistance < 15 && iDistance > 0) fill(clrObject);
text("DIST: " + (iDistance > 40 ? "---" : iDistance + " cm"), width/2 + 150, height - 30);
}