找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8287|回复: 5
收起左侧

改进后的Arduino超声波雷达制作 附代码

[复制链接]
ID:426684 发表于 2019-4-14 19:09 | 显示全部楼层 |阅读模式
改进后的超声波雷达

IMG_20190414_183031.jpg IMG_20190414_183041.jpg


Arduino 代码:
  1. #include <Servo.h>.
  2. const int trigPin = 10;
  3. const int echoPin = 11;
  4. long duration;
  5. int distance;
  6. Servo myServo;
  7. void setup() {
  8.   pinMode(trigPin, OUTPUT);
  9.   pinMode(echoPin, INPUT);
  10.   Serial.begin(9600);
  11.   myServo.attach(2);
  12.   myServo.write(0);
  13. }
  14. void loop() {
  15.   for(int i=0;i<=180;i++){  
  16.   myServo.write(i);
  17.   delay(50);
  18.   distance = calculateDistance();
  19.   Serial.print(i);
  20.   Serial.print(",");
  21.   Serial.print(distance);
  22.   Serial.println(",");
  23.   }
  24.   for(int i=180;i>=0;i--){  
  25.   myServo.write(i);
  26.   delay(50);
  27.   distance = calculateDistance();
  28.   Serial.print(i);
  29.   Serial.print(",");
  30.   Serial.print(distance);
  31.   Serial.println(",");
  32.   }
  33. }
  34. int calculateDistance(){
  35.   digitalWrite(trigPin, LOW);
  36.   delayMicroseconds(2);
  37.   digitalWrite(trigPin, HIGH);
  38.   delayMicroseconds(10);
  39.   digitalWrite(trigPin, LOW);
  40.   duration = pulseIn(echoPin, HIGH);
  41.   distance= duration*0.034/2;
  42.   return distance;
  43. }

  44. Processing 代码:
  45. import processing.serial.*;
  46. import java.awt.event.KeyEvent;
  47. import java.io.IOException;
  48. PFont font;
  49. Serial myPort;
  50. String angle="";
  51. String distance="";
  52. String data="";
  53. String noObject;
  54. float pixsDistance;
  55. String temp;
  56. int iAngle, iDistance;
  57. int index1=0;
  58. int index2=0;
  59. PFont orcFont;
  60. void setup() {

  61. size (1136, 768); //
  62. smooth();
  63.   font = createFont("宋体.vlw",48);
  64.   textFont(font);
  65. myPort = new Serial(this,"COM4",9600); //设置数据来源串口号
  66. }
  67. void draw() {
  68.   fill(98,245,31);
  69.   noStroke();
  70.   fill(0,4);
  71.   rect(0, 0, width, height-height*0.065);
  72.   fill(98,245,31);
  73.   drawRadar();
  74.   drawLine();
  75.   drawObject();
  76.   drawText();
  77. }

  78. void serialEvent (Serial myPort) {
  79. temp = myPort.readString();
  80. String[] data = split(temp, ",");
  81. angle= data[0];
  82. distance= data[1];
  83. iAngle = int(angle);
  84. iDistance=int(distance);  
  85. }


  86. void drawRadar() {
  87.   //if(myPort.available()>0){
  88.   //temp = myPort.readString();
  89.   //String[] data = split(temp, ",");
  90.   //angle= data[0];
  91.   //distance= data[1];
  92.   //iAngle = int(angle);
  93.   //iDistance=int(distance);
  94.   //}
  95.   print(temp);
  96.   print(iAngle);
  97.   print("-");
  98.   println(iDistance);
  99.   pushMatrix();
  100.   translate(width/2,height-height*0.074);
  101.   noFill();
  102.   strokeWeight(2);
  103.   stroke(98,245,31);
  104.   // draws the arc lines
  105.   arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI);
  106.   arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI);
  107.   arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI);
  108.   arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI);
  109.   // draws the angle lines
  110.   line(-width/2,0,width/2,0);
  111.   line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
  112.   line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
  113.   line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90)));
  114.   line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120)));
  115.   line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150)));
  116.   line((-width/2)*cos(radians(30)),0,width/2,0);
  117.   popMatrix();
  118. }
  119. void drawObject() {
  120.   pushMatrix();
  121.   translate(width/2,height-height*0.074);
  122.   strokeWeight(9);
  123.   stroke(255,10,10); // red color
  124.   //pixsDistance=iDistance*((height-height*0.1666)*0.025);
  125.   pixsDistance=iDistance*((height-height*0.800)*0.025);
  126.   if(iDistance<40){
  127.   line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(width-width*0.505)*cos(radians(iAngle)),-(width-width*0.505)*sin(radians(iAngle)));
  128.   }
  129.   popMatrix();
  130. }
  131. void drawLine() {
  132.   pushMatrix();
  133.   strokeWeight(9);
  134.   stroke(30,250,60);
  135.   translate(width/2,height-height*0.074);
  136.   //line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle)));
  137.   line(0,0,(height-height*0.18)*cos(radians(iAngle)),-(height-height*0.18)*sin(radians(iAngle)));
  138.   popMatrix();
  139. }
  140. void drawText() {
  141. textSize(16);
  142. strokeWeight(2);
  143. text("超声波测距雷达显示屏", 430,25);
  144. textSize(12);
  145. text("[探索软件制 CopyRight@2018]", 650,25);

  146.   pushMatrix();
  147.   if(iDistance>40) {
  148.   noObject = "区域外";
  149.   }
  150.   else {
  151.   noObject = "区域内";
  152.   }
  153.   fill(0,0,0);
  154.   noStroke();
  155.   rect(0, height-height*0.0648, width, height);
  156.   fill(98,245,31);
  157.   textSize(16);
  158.   text("10cm",width-width*0.3854,height-height*0.0833);
  159.   text("20cm",width-width*0.281,height-height*0.0833);
  160.   text("30cm",width-width*0.177,height-height*0.0833);
  161.   text("40cm",width-width*0.0729,height-height*0.0833);
  162.   textSize(16);
  163.   text("目标: " + noObject, width-width*0.875, height-height*0.0277);
  164.   text("角度: " + iAngle +" °", width-width*0.55, height-height*0.0277);
  165.   text("距离: ", width-width*0.26, height-height*0.0277);
  166.   text("                      厘米", width-width*0.225, height-height*0.0277);
  167.   text("     " + iDistance , width-width*0.225, height-height*0.0277);
  168.   textSize(20);
  169.   fill(98,245,60);
  170.   translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30)));
  171.   rotate(-radians(-60));
  172.   text("30°",0,0);
  173.   resetMatrix();
  174.   translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60)));
  175.   rotate(-radians(-30));
  176.   text("60°",0,0);
  177.   resetMatrix();
  178.   translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90)));
  179.   rotate(radians(0));
  180.   text("90°",0,0);
  181.   resetMatrix();
  182.   translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120)));
  183.   rotate(radians(-30));
  184.   text("120°",0,0);
  185.   resetMatrix();
  186.   translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150)));
  187.   rotate(radians(-60));
  188.   text("150°",0,0);
  189.   popMatrix();
  190. }
复制代码

效果视频

链接:https://pan.baidu.com/s/1OnJaDHpzSuustdAZI7HjqA  提取码:1hjm  复制这段内容后打开百度网盘手机App,操作更方便哦

评分

参与人数 1黑币 +100 收起 理由
admin + 100 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:302495 发表于 2019-4-15 20:50 | 显示全部楼层
我来第一个赞
回复

使用道具 举报

ID:525478 发表于 2019-9-19 10:45 | 显示全部楼层
跟着点赞  太棒了
回复

使用道具 举报

ID:373684 发表于 2019-9-26 01:29 | 显示全部楼层
可以测多 远的啊  ???电脑上那界面是不是 自己做 的 啊 ???
回复

使用道具 举报

ID:1071473 发表于 2023-4-14 10:19 | 显示全部楼层
请问楼主   使用的是什么板子?
回复

使用道具 举报

ID:1071473 发表于 2023-4-14 15:33 | 显示全部楼层
用的配件都是什么型号
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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