Arduino Servo Radar

HC-SR04 ultrasonik sensör ve SG90 servo motor kullanılarak geliştirilen gerçek zamanlı radar sistemi.

Proje Hakkında

Bu projede Servo motor sürekli olarak 0° ile 180° arasında hareket ederken, HC-SR04 ultrasonik sensör mesafe ölçümü gerçekleştirir. Arduino, elde edilen açı ve mesafe verilerini seri haberleşme üzerinden bilgisayara gönderir. Gelen veriler Processing yazılımı kullanılarak klasik bir radar ekranı şeklinde görselleştirilir.

Kullanılan Donanımlar

Arduino Uno
HC-SR04 Sensör
SG90 Servo Motor
Processing IDE

Devre Bağlantıları

Arduino Servo Radar Bağlantı Şeması

Arduino Kodu

#include <Servo.h>

const int trigPin = 10;
const int echoPin = 11;
const int servoPin = 12; 

long duration;
int distance;
Servo myServo;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  myServo.attach(servoPin);
}

void loop() {
  for (int i = 15; i <= 165; i++) {
    myServo.write(i);
    delay(30);
    distance = calculateDistance();
    Serial.print(i); Serial.print(","); Serial.print(distance); Serial.print(".");
  }
  for (int i = 165; i > 15; i--) {
    myServo.write(i);
    delay(30);
    distance = calculateDistance();
    Serial.print(i); 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;
}

Processing Kodu

/*import processing.serial.*; 
import java.awt.event.KeyEvent; 
import java.io.IOException;

Serial myPort; 
String angle="";
String distance="";
String data="";
String noObject;
float pixsDistance;
int iAngle, iDistance;
int index1=0;
PFont myFont;

void setup() {
  size(1280, 720);
  smooth();
  
  String portName = "COM7"; 
  myPort = new Serial(this, portName, 9600); 
  myPort.bufferUntil('.'); 
  
  myFont = createFont("Arial", 48);
}

void draw() {
  scale(1280.0 / 1920.0);
  
  fill(98, 245, 31);
  textFont(myFont);
  
  noStroke();
  fill(0, 4); 
  rect(0, 0, 1920, 1010); 
  
  fill(98, 245, 31); 
  
  drawRadar(); 
  drawLine();
  drawObject();
  drawText();
}

void serialEvent(Serial myPort) { 
  data = myPort.readStringUntil('.');
  if (data != null) {
    data = data.substring(0, data.length() - 1);
    
    index1 = data.indexOf(","); 
    if (index1 != -1) {
      angle = data.substring(0, index1); 
      distance = data.substring(index1 + 1, data.length()); 
      
      try {
        iAngle = Integer.parseInt(angle.trim());
        iDistance = Integer.parseInt(distance.trim());
      } catch (Exception e) {
      }
    }
  }
}

void drawRadar() {
  pushMatrix();
  translate(960, 1000); 
  noFill();
  strokeWeight(2);
  stroke(98, 245, 31);
  
  // Çemberler
  arc(0, 0, 1800, 1800, PI, TWO_PI);
  arc(0, 0, 1400, 1400, PI, TWO_PI);
  arc(0, 0, 1000, 1000, PI, TWO_PI);
  arc(0, 0, 600, 600, PI, TWO_PI);
  
  // Açı çizgileri - İşaret hataları düzeltilerek radarla %100 senkron yapıldı
  line(-960, 0, 960, 0);
  line(0, 0, 960 * cos(radians(30)), -960 * sin(radians(30)));
  line(0, 0, 960 * cos(radians(60)), -960 * sin(radians(60)));
  line(0, 0, 960 * cos(radians(90)), -960 * sin(radians(90)));
  line(0, 0, 960 * cos(radians(120)), -960 * sin(radians(120)));
  line(0, 0, 960 * cos(radians(150)), -960 * sin(radians(150)));
  popMatrix();
}

void drawObject() {
  pushMatrix();
  translate(960, 1000); 
  strokeWeight(9);
  stroke(255, 10, 10); 
  
  pixsDistance = iDistance * 22.5; 
  
  if (iDistance < 40) {
    line(pixsDistance * cos(radians(iAngle)), -pixsDistance * sin(radians(iAngle)), 950 * cos(radians(iAngle)), -950 * sin(radians(iAngle)));
  }
  popMatrix();
}

void drawLine() {
  pushMatrix();
  strokeWeight(9);
  stroke(30, 250, 60);
  translate(960, 1000); 
  line(0, 0, 950 * cos(radians(iAngle)), -950 * sin(radians(iAngle))); 
  popMatrix();
}

void drawText() { 
  pushMatrix();
  if (iDistance > 40) {
    noObject = "Menzil Disi";
  } else {
    noObject = "Hedef Tespit Edildi";
  }
  
  fill(0, 0, 0);
  noStroke();
  rect(0, 1010, 1920, 100); 
  
  fill(98, 245, 31);
  
  // Mesafe yazıları kendi çemberlerinin tam üstüne matematiksel olarak hizalandı
  textSize(25);
  textAlign(RIGHT, BASELINE);
  text("10cm", 1260, 990);
  text("20cm", 1460, 990);
  text("30cm", 1660, 990);
  text("40cm", 1860, 990);
  
  // Alt panel yazıları
  textAlign(LEFT, BASELINE);
  textSize(40);
  text("Durum: " + noObject, 240, 1060);
  text("Aci: " + iAngle + " °", 950, 1060);
  
  if (iDistance < 40) {
    text("Mesafe: " + iDistance + " cm", 1350, 1060);
  } else {
    text("Mesafe: -", 1350, 1060);
  }
  
  // Açı dereceleri (30°, 60°, vs.) trigonometri ile mükemmel hizalandı
  pushMatrix();
  translate(960, 1000); // Radar merkezine in
  textAlign(CENTER, CENTER);
  textSize(25);
  fill(98, 245, 60);
  
  for (int a = 30; a <= 150; a += 30) {
    pushMatrix();
    float r = 1000; // Yazıların çizgilerden ne kadar uzakta olacağı
    float x = r * cos(radians(a));
    float y = -r * sin(radians(a));
    translate(x, y);
    rotate(radians(90 - a)); // Çemberin eğimine göre yazıyı döndür
    text(a + "°", 0, 0);
    popMatrix();
  }
  popMatrix();
  
  popMatrix(); 
}
// --- KODUN SONU --- */

Galeri

Servo Radar Projesi