久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 9484|回復: 5
打印 上一主題 下一主題
收起左側

改進后的Arduino超聲波雷達制作 附代碼

[復制鏈接]
跳轉到指定樓層
樓主
改進后的超聲波雷達



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 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏4 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:302495 發表于 2019-4-15 20:50 | 只看該作者
我來第一個贊
回復

使用道具 舉報

板凳
ID:525478 發表于 2019-9-19 10:45 | 只看該作者
跟著點贊  太棒了
回復

使用道具 舉報

地板
ID:373684 發表于 2019-9-26 01:29 | 只看該作者
可以測多 遠的啊  ???電腦上那界面是不是 自己做 的 啊 ???
回復

使用道具 舉報

5#
ID:1071473 發表于 2023-4-14 10:19 | 只看該作者
請問樓主   使用的是什么板子?
回復

使用道具 舉報

6#
ID:1071473 發表于 2023-4-14 15:33 | 只看該作者
用的配件都是什么型號
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 中文字幕在线一区二区三区 | 欧美色综合天天久久综合精品 | 久久精品国产免费高清 | 亚洲视频手机在线 | 日韩免费中文字幕 | 国产高清精品一区二区三区 | 97国产精品视频 | 欧美a区 | 九九亚洲 | 日韩成人精品一区二区三区 | 中文精品一区二区 | 永久免费视频 | 国产三级一区二区三区 | 久久一二 | 99国产精品久久久 | 少妇一级淫片免费播放 | 91麻豆精品国产91久久久更新资源速度超快 | 欧美xxxx网站 | 亚洲精品久久久久中文字幕欢迎你 | 成人毛片在线视频 | 久久久91精品国产一区二区三区 | 97精品一区二区 | 欧美一级片在线看 | 亚洲一区二区三区视频在线 | 一本色道精品久久一区二区三区 | 午夜精品视频在线观看 | 久草热播 | 欧美另类视频在线 | 欧美一区二区三区在线 | 中文字幕乱码一区二区三区 | 国产第一区二区 | 精品欧美一区二区三区精品久久 | 一级黄a视频| 欧美在线激情 | 久久三区 | www.啪啪.com| 免费精品视频在线观看 | 亚洲一区久久 | 99精品一区二区三区 | 欧美a区| 日本成人中文字幕 |