//https://projecthub.arduino.cc/harshdeepsingh/mini-radar-station-with-position-indicator-and-real-time-range-variation-80a49b
#include <Servo.h>
#define trigPin 22 // Ultrasonic sensor trig pin
#define echoPin 24 // Ultrasonic sensor echo pin
#define servoPin 2 // Servo motor signal pin
Servo myservo;
// Servo position (0-180 degrees)
int pos = 0;
int ledPins[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int ledScanPos[] = {27, 29, 31, 33, 35, 37, 39, 41, 43, 45};
int ledDist[] = {26, 28, 30, 32, 34, 36, 38, 40, 42, 44};
int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);
int dt = 200;
int potPin = A0, mapped_value, distance_pot, distIndex;
long duration, distance_cm;
void setup() {
Serial.begin(9600);
myservo.attach(servoPin);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
// One direction scan
// Scan in 18-degree increments
for (pos = 0; pos <= 180; pos += 18) {
myservo.write(pos);
// Adjust scan speed as needed
delay(dt);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance_cm = duration * 0.034 / 2;
mapped_value = analogRead(potPin);
distance_pot = map(mapped_value, 0, 1023, 10, 100);
distIndex = map(mapped_value, 0, 1023, 0, 10);
for (int k = 0; k < 10; k++) {
digitalWrite(ledDist[k], k < distIndex ? HIGH : LOW);
}
// Determine which LED corresponds to the servo angle
int ledIndex = pos / 18;
//Tells the status of scan
digitalWrite(ledScanPos[ledIndex], HIGH);
if (distance_cm < distance_pot) {
// Turn on LED if obstacle detected
digitalWrite(ledPins[ledIndex], HIGH);
} else {
// Turn off LED if no obstacle
digitalWrite(ledPins[ledIndex], LOW);
}
Serial.print("Angle: ");
Serial.print(pos);
Serial.print(" degrees, Distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
}
// Other direction scan
// Scan in 18-degree increments
for (pos = 180; pos >= 0; pos -= 18) {
myservo.write(pos);
// Adjust scan speed as needed
delay(dt);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance_cm = duration * 0.034 / 2;
mapped_value = analogRead(potPin);
distance_pot = map(mapped_value, 0, 1023, 10, 100);
distIndex = map(mapped_value, 0, 1023, 0, 10);
for (int k = 0; k < 10; k++) {
digitalWrite(ledDist[k], k < distIndex ? HIGH : LOW);
}
// Determine which LED corresponds to the servo angle
int ledIndex = pos / 18;
// Tells the status of scan
digitalWrite(ledScanPos[ledIndex], LOW);
if (distance_cm < distance_pot) {
// Turn on LED if obstacle detected
digitalWrite(ledPins[ledIndex], HIGH);
} else {
// Turn off LED if no obstacle
digitalWrite(ledPins[ledIndex], LOW);
}
Serial.print("Angle: ");
Serial.print(pos);
Serial.print(" degrees, Distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
}
// Turn off all LEDs at the end of scan
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledScanPos[i], LOW);
}
// Delay between scans (adjust as needed)
delay(dt);
}