Radar code's

 Here is a sample Arduino code for a radar system using an ultrasonic sensor and a 5V adapter:




#include <Servo.h>

#define TRIGGER_PIN 4
#define ECHO_PIN 5
#define MAX_DISTANCE 200 // Maximum range of the ultrasonic sensor
#define SERVO_PIN 9
#define SERVO_MIN_ANGLE 0
#define SERVO_MAX_ANGLE 180
#define SERVO_SWEEP_DELAY 15 // Delay between each servo position change

Servo servo;
int servoAngle = SERVO_MIN_ANGLE;

void setup() {
  Serial.begin(9600);
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  servo.attach(SERVO_PIN);
}

void loop() {
  // Rotate the servo back and forth
  for (int i = SERVO_MIN_ANGLE; i <= SERVO_MAX_ANGLE; i++) {
    servo.write(i);
    delay(SERVO_SWEEP_DELAY);
    int distance = getDistance();
    if (distance > 0 && distance <= MAX_DISTANCE) {
      // Target detected, print the distance and angle
      Serial.print("Distance: ");
      Serial.print(distance);
      Serial.print(" cm, Angle: ");
      Serial.println(i);
    }
  }
  for (int i = SERVO_MAX_ANGLE; i >= SERVO_MIN_ANGLE; i--) {
    servo.write(i);
    delay(SERVO_SWEEP_DELAY);
    int distance = getDistance();
    if (distance > 0 && distance <= MAX_DISTANCE) {
      // Target detected, print the distance and angle
      Serial.print("Distance: ");
      Serial.print(distance);
      Serial.print(" cm, Angle: ");
      Serial.println(i);
    }
  }
}

int getDistance() {
  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);
  long duration = pulseIn(ECHO_PIN, HIGH);
  int distance = duration * 0.034 / 2; // Calculate the distance in cm
  return distance;
}

No comments:

Post a Comment

if you have any dought then let's me know

ALL CODE'S

Ecommerce front page of Code Design. CODE:-  <section class="text-gray-600 body-font">   <div class="container px-5 ...