Arduino uno ultrasonic radar


    āđ€āļ›็āļ™āļāļēāļĢāđƒāļŠ้āđ‚āļĄāļ”ูāļĨ ultrasonic āļĄāļēāđƒāļŠ้āļ‡āļēāļ™āđ€āļ›็āļ™āđ€āļĢāļ”āļēāļĢ์āļ—ี่āļ„āļ§āļšāļ„ุāļĄāļ”้āļ§āļĒ arduino uno āđ‚āļ”āļĒāļ—ี่āļĄีāļāļēāļĢāđāļŠāļ”āļ‡āļœāļĨāļ­āļ­āļāļŦāļ™้āļēāļˆāļ­āļ”้āļ§āļĒāļāļēāļĢāđ€āļ‚ีāļĒāļ™āđ‚āļ„้āļ” Processing āļĢูāļ›āđāļšāļšāļāļēāļĢāļ—āļģāļ‡āļēāļ™āđ€āļ›็āļ™āļ”ัāļ‡āļ§ีāļ”ิāđ‚āļ­āļ”้āļēāļ™āļĨ่āļēāļ‡

āļ­ุāļ›āļāļĢāļ“์āļ—ี่āđƒāļŠ้āļĄีāļ”ัāļ‡āļ™ี้
1. Arduino uno
2. Micro Servo 9g sg90
3. HC-SR04 Ultrasonic module
4. Wire āļŠāļēāļĒāđ„āļŸāļ•่āļēāļ‡āđ†

āļĢูāļ›āđāļšāļšāļāļēāļĢāļ•่āļ­āļ§āļ‡āļˆāļĢāđ€āļ›็āļ™āļ”ัāļ‡āļ™ี้


āđƒāļ™āļŠ่āļ§āļ™āđ‚āļ„้āļ” Arduino āđ€āļ›็āļ™āļ”ัāļ‡āļ™ี้

// Include the Servo library
#include <Servo.h>

// Defines Trig and Echo pins of the Ultrasonic Sersor
const int trigPin = 10;
const int echoPin = 11;
// Variables for the duration and the distance
long duration;
int distance;
bool rotate_toggle = false; // switch rotate left and right

Servo myServo; // Creates a servo object for controlling the servo motor

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  myServo.attach(12); // Defines on which pin is the servo motor attached
}

void loop() {
  // rotates the servo motor from 15 to 165 degrees
  for (int i=15;i<=165;i++){
    if (rotate_toggle == false){
      myServo.write(i);
      if (i==165){
        rotate_toggle = true;  
      }  
    }
    else {
      myServo.write(165 - i);
      if (i==165){
        rotate_toggle = false;  
      }  
    }
    delay(30);
    distance = calculateDistance();
    if (rotate_toggle == false){
      Serial.print(i);
    }
    else{
      Serial.print(165-i);
    }
    Serial.print(",");
    Serial.print(distance);
    Serial.print(".");  
  }
}
// Function for calculating the distance measured by the Ultrasonic sensor
int calculateDistance(){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the tricPin on HIGH state for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
  distance = duration*0.034/2;
  return distance;
}

    āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ„ืāļ­āļˆāļ°āļāļģāļŦāļ™āļ”āđƒāļŦ้ servo āļĄีāļāļēāļĢāļŦāļĄุāļ™āļāļ§āļēāļ”āđ„āļ›āļāļĨัāļšāđ‚āļ”āļĒāļāļģāļŦāļ™āļ”āđƒāļŦ้āļŦāļĄุāļ™āļˆāļēāļ 15 āļ­āļ‡āļĻāļēāđ„āļ›āļ–ึāļ‡ 165 āļ­āļ‡āļĻāļē āļˆāļēāļāļ™ั้āļ™āđƒāļŦ้āļŦāļĄุāļ™āļĒ้āļ­āļ™āļāļĨัāļšāļ—āļēāļ‡āđ€āļ”ิāļĄāļ—āļģāđƒāļŦ้āđ€āļĢāļ”āļēāļĢ์āļ‚āļ­āļ‡āđ€āļĢāļēāđ€āļ§āļĨāļēāļŠāđāļāļ™āļˆāļ°āļāļ§āļēāļ”āđ„āļ›āđāļĨāļ°āļāļĨัā āļˆāļēāļāļ™ั้āļ™āļĄีāļŸัāļ‡āļ์āļŠัāļ™āļ—ี่āđƒāļŠ้āđƒāļ™āļāļēāļĢāļ„āļģāļ™āļ§āļ“āļĢāļ°āļĒāļ°āļ—āļēāļ‡āļˆāļēāļ ultrasonic āđ‚āļ”āļĒāļŠั่āļ‡āļāļēāļĢāļ—āļģāļ‡āļēāļ™āđ„āļ›āļ—ี่āļ‚āļē trig āļ‚āļ­āļ‡ ultrasonic module āļˆāļēāļāļ™ั้āļ™āļ­่āļēāļ™āļ„่āļēāļĢāļ°āļĒāļ°āļ—āļēāļ‡āļ—ี่āđ„āļ”้āļˆāļēāļāļ‚āļē echo āļ™āļģāļœāļĨāļ—ี่āđ„āļ”้āļĄāļēāļ„ูāļ“āļ”้āļ§āļĒ 0.034/2 āļ‹ึ่āļ‡āđ€āļ›็āļ™āļŠูāļ•āļĢāđƒāļ™āļāļēāļĢāļ„āļģāļ™āļ§āļ“āļ‚āļ­āļ‡ ultrasonic   āđƒāļ™āļ—ี่āļ™ี้āļĄีāļāļēāļĢāđāļŠāļ”āļ‡āļœāļĨāļ­āļ­āļāļ—āļēāļ‡ Serial āļāļģāļŦāļ™āļ” bandwidth āļ—ี่ 9600 āļ„่āļēāļ—ี่āđāļŠāļ”āļ‡āđ„āļ”้āđāļ่ āļ­āļ‡āļĻāļē(degree) āļ‚āļ­āļ‡ servo āļ—ี่āļāļģāļĨัāļ‡āļŦāļĄุāļ™āļัāļšāļ„่āļēāļĢāļ°āļĒāļ°āļ—āļēāļ‡(distance) āļ›ิāļ”āļ”้āļ§āļĒ .  āđƒāļ™āļŠ่āļ§āļ™āļ‚āļ­āļ‡ processing āļˆāļ°āļ™āļģāļ‚้āļ­āļĄูāļĨāđƒāļ™āđ„āļ›āļŠāļĢ้āļēāļ‡āļŦāļ™้āļēāļˆāļ­āđ€āļĢāļ”āļēāļĢ์āļ•่āļ­āđ„āļ›

āđƒāļ™āļŠ่āļ§āļ™āđ‚āļ„้āļ” Processing āđ€āļ›็āļ™āļ”ัāļ‡āļ™ี้


import processing.serial.*; // imports library for serial communication
import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
import java.io.IOException;

Serial myPort; // defines Object Serial
// default variables
String angle = "";
String distance = "";
String data = "";
String noObject;
float pixsDistance;
int iAngle, iDistance;
int index1 = 0;
int index2 = 0;
PFont orcFont;

void setup(){
  size(1080, 610);
  smooth();
  myPort = new Serial(this, "COM3", 9600); // starts the serial communication
  myPort.bufferUntil('.'); // read data from serial port up to the character '.'. it read angle,distance.
  orcFont = loadFont("OCRAExtended-30.vlw");
}

void draw(){
  fill(98, 245, 31);
  textFont(orcFont);
  
  noStroke();
  fill(0, 4);
  rect(0, 0, width, 580);
  
  fill(98, 245, 31); // green color
  // call functions for drawing the radar
  drawRadar();
  drawLine();
  drawObject();
  drawText();
}

void serialEvent(Serial myPort){
  data = myPort.readStringUntil('.');
  data = data.substring(0, data.length()-1);
  
  index1 = data.indexOf(",");
  angle = data.substring(0, index1);
  distance = data.substring(index1+1, data.length());
  
  iAngle = int(angle);
  iDistance = int(distance);
}

void drawRadar() {
  pushMatrix();
  int radar_x = 540;
  translate(radar_x, 580); // move starting coordinates to new location
  noFill();
  strokeWeight(2);
  stroke(98, 245, 31);
  // draw the arc lines
  arc(0, 0, 970, 970, PI, TWO_PI);
  arc(0, 0, 750, 750, PI, TWO_PI);
  arc(0, 0, 500, 500, PI, TWO_PI);
  arc(0, 0, 250, 250, PI, TWO_PI);
  // draw the angle lines
  line(-radar_x, 0, -radar_x, 0);
  line(0,0,-radar_x*cos(radians(30)),-radar_x*sin(radians(30)));
  line(0,0,-radar_x*cos(radians(60)),-radar_x*sin(radians(60)));
  line(0,0,-radar_x*cos(radians(90)),-radar_x*sin(radians(90)));
  line(0,0,-radar_x*cos(radians(120)),-radar_x*sin(radians(120)));
  line(0,0,-radar_x*cos(radians(150)),-radar_x*sin(radians(150)));
  line(-620*cos(radians(30)),0,620,0);
  popMatrix();
}

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

void drawObject() {
  pushMatrix();
  translate(540, 580);
  strokeWeight(9);
  stroke(255, 10, 10); // red color
  pixsDistance = iDistance*22.5; // covers the distance from the sensor from cm to pixels
  // limiting the range to 40 cms
  if (iDistance < 40){
    // draws the object according to the angle and the distance
    line(pixsDistance*cos(radians(iAngle))/1.8, 
        -pixsDistance*sin(radians(iAngle))/1.8,
        540*cos(radians(iAngle)),
        -540*sin(radians(iAngle)));
  }
  popMatrix();
}

void drawText() {
  pushMatrix();
  if(iDistance>40) {
    noObject = "Out of Range";
  }
  else {
    noObject = "In Range";
  }
  fill(0,0,0);
  noStroke();
  rect(0, 580, width, 610);
  fill(98,245,31);
  textSize(15);
  text("10cm",625,575);
  text("20cm",750,575);
  text("30cm",875,575);
  text("40cm",980,575);
  textSize(20);
  text("Object: " + noObject, 140, 600);
  text("Angle: " + iAngle +" °", 550, 600);
  text("Distance: ", 710, 600);
  if(iDistance<40) {
    text("        " + iDistance +" cm", 730, 600);
  }
  textSize(12);
  fill(98,245,60);
  translate(530+540*cos(radians(30)),560-540*sin(radians(30)));
  rotate(-radians(-60));
  text("30°",0,0);
  resetMatrix();
  translate(530+540*cos(radians(60)),560-540*sin(radians(60)));
  rotate(-radians(-30));
  text("60°",0,0);
  resetMatrix();
  translate(530+540*cos(radians(90)),570-540*sin(radians(90)));
  rotate(radians(0));
  text("90°",0,0);
  resetMatrix();
  translate(530+540*cos(radians(120)),575-540*sin(radians(120)));
  rotate(radians(-30));
  text("120°",0,0);
  resetMatrix();
  translate(530+540*cos(radians(150)),580-540*sin(radians(150)));
  rotate(radians(-60));
  text("150°",0,0);
  popMatrix(); 
}

   āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ›āļĢāļ°āļāļ­āļšāļ”้āļ§āļĒāļŸัāļ‡āļ์āļŠัāļ™āļāļēāļĢāļ§āļēāļ”āđ€āļŠ้āļ™āļŠีāđ€āļ‚ีāļĒāļ§āđ€āļžื่āļ­āļšāļ­āļāļ§่āļēāđ€āļĢāļ”āļēāļĢ์āļāļģāļĨัāļ‡āļāļ§āļēāļ”āđ„āļ›āļˆุāļ”āđ„āļŦāļ™ āļŸัāļ‡āļ์āļŠัāļ™āļ§āļēāļ”āđ€āļŠ้āļ™āļŠีāđāļ”āļ‡āđ€āļžื่āļ­āļšāļ­āļāļĢāļ°āļĒāļ°āļ‚āļ­āļ‡āļ§ัāļ•āļ–ุāļ—ี่āđ€āļĢāļ”āļēāļĢ์āļ•āļĢāļ§āļˆāļˆัāļšāđ„āļ”้ āļŸัāļ‡āļ์āļŠัāļ™āļ§āļēāļ”āļŠāđ€āļāļĨāļ‚āļ­āļ‡āđ€āļĢāļ”āļēāļĢ์ āļŸัāļ‡āļāļŠ์āļŠัāļ™āđāļŠāļ”āļ‡āļ‚้āļ­āļĄูāļĨāļ•ัāļ§āļ­ัāļāļĐāļĢāđāļĨāļ°āļŸัāļ‡āļ์āļŠัāļ™āļ­่āļēāļ™āļ„่āļēāļˆāļēāļ Serial āļ‚āļ­āļ‡ Arduino āļ•่āļ­āđ„āļ›āļ™ี้āļˆāļ°āđ€āļ›็āļ™āļāļēāļĢāļ­āļ˜ิāļšāļēāļĒāļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ‚āļ­āļ‡āđāļ•่āļĨāļ°āļŸัāļ‡āļ์āļŠัāļ™

void drawRadar āļŸัāļ‡āļ์āļŠัāļ™āļ§āļēāļ”āļŠāđ€āļāļĨāļ‚āļ­āļ‡āđ€āļĢāļ”āļēāļĢ์


void drawLine āļŸัāļ‡āļ์āļŠัāļ™āļāļēāļĢāļ§āļēāļ”āđ€āļŠ้āļ™āļŠีāđ€āļ‚ีāļĒāļ§āđ€āļžื่āļ­āļšāļ­āļāļ§่āļēāđ€āļĢāļ”āļēāļĢ์āļāļģāļĨัāļ‡āļāļ§āļēāļ”āđ„āļ›āļˆุāļ”āđ„āļŦāļ™


void drawObjecct āļŸัāļ‡āļ์āļŠัāļ™āļ§āļēāļ”āđ€āļŠ้āļ™āļŠีāđāļ”āļ‡āđ€āļžื่āļ­āļšāļ­āļāļĢāļ°āļĒāļ°āļ‚āļ­āļ‡āļ§ัāļ•āļ–ุāļ—ี่āđ€āļĢāļ”āļēāļĢ์āļ•āļĢāļ§āļˆāļˆัāļšāđ„āļ”้ āļāļģāļŦāļ™āļ”āđƒāļŦ้āļˆัāļšāļ§ัāļ•āļ–ุāđ„āļ”้āđ„āļāļĨāļ—ี่āļŠุāļ”āđƒāļ™āļĢāļ°āļĒāļ° 40cm. āđ€āļ™ื่āļ­āļ‡āļˆāļēāļāļ„่āļēāļ—ี่āļ­่āļēāļ™āđ„āļ”้āļˆāļēāļ Serial āļ—āļģāđƒāļŦ้āļ•āļ­āļ™āļ§āļēāļ”āđ€āļŠ้āļ™āļ­āļ­āļāļĄāļē  āđ„āļ”้āļĢāļ°āļĒāļ°āļ—ี่āđ„āļĄ่āļ•āļĢāļ‡āļัāļšāļŠāđ€āļāļĨāļ—ี่āļ­āļ­āļāđāļšāļšāđ„āļ§้āļˆึāļ‡āļ—āļģāđƒāļŦ้āļ•้āļ­āļ‡āļŦāļēāļĢāļ„่āļēāļĢāļ°āļĒāļ°āļ—ี่āđ„āļ”้āļ”้āļ§āļĒ 1.8 (āļ„่āļēāļ™ี้āļŦāļēāļˆāļēāļāļāļēāļĢāļ›āļĢัāļšāļ•ัāļ§āđ€āļĨāļ‚āļ•āļēāļĄāļ„āļ§āļēāļĄāđ€āļŦāļĄāļēāļ°āļŠāļĄ)


void drawText āļŸัāļ‡āļāļŠ์āļŠัāļ™āđāļŠāļ”āļ‡āļ‚้āļ­āļĄูāļĨāļ•ัāļ§āļ­ัāļāļĐāļĢ


void SerialEvent āļ­ัāļāļĐāļĢāđāļĨāļ°āļŸัāļ‡āļ์āļŠัāļ™āļ­่āļēāļ™āļ„่āļēāļˆāļēāļ Serial āļ‚āļ­āļ‡ Arduino āļ–้āļēāļĢูāļ›āđāļšāļšāļ—ี่āļ­่āļēāļ™āđ„āļ”้āļˆāļ°āđ€āļ›็āļ™ 100,30. āļ™ั่āļ™āļŦāļĄāļēāļĒāļ„āļ§āļēāļĄāļ§่āļēāļ•āļ­āļ™āļ™ี้ servo āļŦāļĄุāļ™āđ„āļ›āļ—ี่ 100 āļ­āļ‡āļĻāļē āļˆัāļšāļ§ัāļ•āļ–ุāđ„āļ”้āđƒāļ™āļĢāļ°āļĒāļ° 30cm.
āļ āļēāļžāļ•ัāļ§āļ­āļĒ่āļēāļ‡āļŦāļ™้āļēāļˆāļ­āđ€āļĢāļ”āļēāļĢ์