改進后的超聲波雷達
IMG_20190414_183031.jpg (44.01 KB, 下載次數: 113)
下載附件
2019-4-14 19:05 上傳
IMG_20190414_183041.jpg (112.77 KB, 下載次數: 92)
下載附件
2019-4-14 19:04 上傳
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);
- myServo.write(0);
- }
- void loop() {
- for(int i=0;i<=180;i++){
- myServo.write(i);
- delay(50);
- distance = calculateDistance();
- Serial.print(i);
- Serial.print(",");
- Serial.print(distance);
- Serial.println(",");
- }
- for(int i=180;i>=0;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() {
- //if(myPort.available()>0){
- //temp = myPort.readString();
- //String[] data = split(temp, ",");
- //angle= data[0];
- //distance= data[1];
- //iAngle = int(angle);
- //iDistance=int(distance);
- //}
- 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();
- }
復制代碼
效果視頻
鏈接:https://pan.baidu.com/s/1OnJaDHpzSuustdAZI7HjqA 提取碼:1hjm 復制這段內容后打開百度網盤手機App,操作更方便哦 |