极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 19767|回复: 0

超声波雷达

[复制链接]
发表于 2018-12-8 18:29:15 | 显示全部楼层 |阅读模式
Arduino+Processing  超声波雷达
硬件:
Arduino UNO
超声波探测器 x1
9克舵机 x1

软件:
Arduino IDE
Processing (2.1)

Arduino 代码:
#include <Servo.h>.
const int trigPin = 10;
const int echoPin = 11;
long duration;
int distance;
Servo myServo;
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  myServo.attach(2);
}
void loop() {
  for(int i=15;i<=165;i++){  
  myServo.write(i);
  delay(50);
  distance = calculateDistance();
  Serial.print(i);
  Serial.print(",");
  Serial.print(distance);
  Serial.println(",");
  }
  for(int i=165;i>15;i--){  
  myServo.write(i);
  delay(50);
  distance = calculateDistance();
  Serial.print(i);
  Serial.print(",");
  Serial.print(distance);
  Serial.println(",");
  }
}
int calculateDistance(){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance= duration*0.034/2;
  return distance;
}
* 输出角度和距离

Processing 代码:
import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
PFont font;
Serial myPort;
String angle="";
String distance="";
String data="";
String noObject;
float pixsDistance;
String temp;
int iAngle, iDistance;
int index1=0;
int index2=0;
PFont orcFont;
void setup() {

size (1136, 768); //
smooth();
  font = createFont("宋体.vlw",48);
  textFont(font);
myPort = new Serial(this,"com4", 9600); //设置数据来源串口号
}
void draw() {
  fill(98,245,31);
  noStroke();
  fill(0,4);
  rect(0, 0, width, height-height*0.065);
  fill(98,245,31);
  drawRadar();
  drawLine();
  drawObject();
  drawText();
}
void serialEvent (Serial myPort) {
  temp = myPort.readString();
  String[] data = split(temp, ",");
  angle= data[0];
  distance= data[1];
  iAngle = int(angle);
  iDistance=int(distance);

}
void drawRadar() {
  print(temp);
  print(iAngle);
  print("-");
  println(iDistance);
  pushMatrix();
  translate(width/2,height-height*0.074);
  noFill();
  strokeWeight(2);
  stroke(98,245,31);
  // draws the arc lines
  arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI);
  arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI);
  arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI);
  arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI);
  // draws the angle lines
  line(-width/2,0,width/2,0);
  line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
  line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
  line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90)));
  line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120)));
  line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150)));
  line((-width/2)*cos(radians(30)),0,width/2,0);
  popMatrix();
}
void drawObject() {
  pushMatrix();
  translate(width/2,height-height*0.074);
  strokeWeight(9);
  stroke(255,10,10); // red color
  //pixsDistance=iDistance*((height-height*0.1666)*0.025);
  pixsDistance=iDistance*((height-height*0.800)*0.025);
  if(iDistance<40){
  line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(width-width*0.505)*cos(radians(iAngle)),-(width-width*0.505)*sin(radians(iAngle)));
  }
  popMatrix();
}
void drawLine() {
  pushMatrix();
  strokeWeight(9);
  stroke(30,250,60);
  translate(width/2,height-height*0.074);
  //line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle)));
  line(0,0,(height-height*0.18)*cos(radians(iAngle)),-(height-height*0.18)*sin(radians(iAngle)));
  popMatrix();
}
void drawText() {
textSize(16);
strokeWeight(2);
text("超声波测距雷达显示屏", 430,25);
textSize(12);
text("[探索软件制 CopyRight@2018]", 650,25);

  pushMatrix();
  if(iDistance>40) {
  noObject = "区域外";
  }
  else {
  noObject = "区域内";
  }
  fill(0,0,0);
  noStroke();
  rect(0, height-height*0.0648, width, height);
  fill(98,245,31);
  textSize(16);
  text("10cm",width-width*0.3854,height-height*0.0833);
  text("20cm",width-width*0.281,height-height*0.0833);
  text("30cm",width-width*0.177,height-height*0.0833);
  text("40cm",width-width*0.0729,height-height*0.0833);
  textSize(16);
  text("目标: " + noObject, width-width*0.875, height-height*0.0277);
  text("角度: " + iAngle +" °", width-width*0.55, height-height*0.0277);
  text("距离: ", width-width*0.26, height-height*0.0277);
  text("                      厘米", width-width*0.225, height-height*0.0277);
  text("     " + iDistance , width-width*0.225, height-height*0.0277);
  textSize(20);
  fill(98,245,60);
  translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30)));
  rotate(-radians(-60));
  text("30°",0,0);
  resetMatrix();
  translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60)));
  rotate(-radians(-30));
  text("60°",0,0);
  resetMatrix();
  translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90)));
  rotate(radians(0));
  text("90°",0,0);
  resetMatrix();
  translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120)));
  rotate(radians(-30));
  text("120°",0,0);
  resetMatrix();
  translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150)));
  rotate(radians(-60));
  text("150°",0,0);
  popMatrix();
}
* Processing 版本 2.1

效果视频:


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-25 18:46 , Processed in 0.060709 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表