单片机教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.51hei.com提供.

标题: 超声波雷达[LCD12864 和 Processing PC 显示]

作者: 探索软件   时间: 2018-12-1 20:50
标题: 超声波雷达[LCD12864 和 Processing PC 显示]
超声波雷达[LCD12864 和 Processing PC 显示]

Arduino-LCD12864 代码:
//超声波雷达
#include< Arduino.h>
#include "LCD12864RSPI.h"
#define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
unsigned char show0[]={0xB3,0xAC,0xC9,0xF9,0xB2,0xA8,0xB2,0xE2, 0xBE,0xE0,0xC0,0xD7,0xB4,0xEF};
unsigned char show1[]={0xD7,0xAA,0xBD,0xC7};
unsigned char show2[]={0xBE,0xE0,0xC0,0xEB};
unsigned char show3[]={0xB6,0xC8};
unsigned char show4[]={0xC0,0xE5,0xC3,0xD7};
unsigned char show5[]={0xA3,0xBA};
#define TrigPin 2
#define EchoPin 4
#define PWM_pin 5
int pulsewidth = 0;  
int pos = 0;      
float Value_cm;
char jl[5];                      
char jd[3];
float temp =0;
int jdtemp =0;
void setup()
{
Serial.begin(9600);
pinMode(PWM_pin,OUTPUT);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
LCDA.Initialise();              
delay(100);
LCDA.DisplayString(0,0,show0,AR_SIZE(show0));  
LCDA.DisplayString(1,0,show1,AR_SIZE(show1));  
LCDA.DisplayString(1,2,show5,AR_SIZE(show5));  
LCDA.DisplayString(2,0,show2,AR_SIZE(show2));
LCDA.DisplayString(2,2,show5,AR_SIZE(show5));  
LCDA.DisplayString(1,6,show3,AR_SIZE(show3));
LCDA.DisplayString(2,6,show4,AR_SIZE(show4));
}

void loop(){

for (pos = 0; pos< = 180; pos += 1) {
pulse(pos);     //设置舵机指向90度
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
Value_cm = float( pulseIn(EchoPin, HIGH) * 17 )/1000;
if(Value_cm>1000){ Value_cm=0;}
Serial.println(Value_cm);
temp=Value_cm;        
dtostrf(temp,4,1,jl);
jdtemp=pos;        
dtostrf(jdtemp,3,0,jd);
LCDA.DisplayString(1,3,(unsigned char *)jd,AR_SIZE(jd));
LCDA.DisplayString(2,3,(unsigned char *)jl,AR_SIZE(jl));
}
delay(10);
for (pos = 180; pos >= 0; pos -= 1) {
pulse(pos);     //设置舵机指向-90度  
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
Value_cm = float( pulseIn(EchoPin, HIGH) * 17 )/1000;
if(Value_cm>1000){ Value_cm=0;}
Serial.println(Value_cm);
temp=Value_cm;         //读取日值
dtostrf(temp,4,1,jl);
jdtemp=pos;         //读取日值
dtostrf(jdtemp,3,0,jd);
LCDA.DisplayString(1,3,(unsigned char *)jd,AR_SIZE(jd));
LCDA.DisplayString(2,3,(unsigned char *)jl,AR_SIZE(jl));
}  
//delay(10);
}

void pulse(int angle)     //设置舵机角度为angle
{
   pulsewidth=int ((angle*11)+500);  //计算高电平时间
   digitalWrite(PWM_pin,HIGH);     //设置高电平
   delayMicroseconds(pulsewidth);   //延时pulsewidth (us)
   digitalWrite(PWM_pin,LOW);       //设置低电平
   delay(20-pulsewidth/1000);       //延时20-pulsewidth/1000 (ms)
}



Arduino- Processing PC代码:
#include< Arduino.h>
#define TrigPin 2
#define EchoPin 3
#define PWM_pin 9
int pulsewidth = 0;   //高电平时间
int pos = 0;         //
float Value_cm;

void setup()
{
     Serial.begin(9600);
     pinMode(PWM_pin,OUTPUT);
     pinMode(TrigPin, OUTPUT);
     pinMode(EchoPin, INPUT);
     pulse(0);     //设置舵机指向90度
}

void loop()
{
for (pos = 0; pos< = 180; pos += 5) {
pulse(pos);     //设置舵机指向90度
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
Value_cm = float( pulseIn(EchoPin, HIGH) * 17 )/1000;
//if(Value_cm>1000){ Value_cm=0;}
Serial.print("D= ");
Serial.print(Value_cm);
Serial.println(" [cm]");
delay(100);
}
delay(10);
for (pos = 180; pos >= 0; pos -= 5) {
pulse(pos);     //设置舵机指向-90度  
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
Value_cm = float( pulseIn(EchoPin, HIGH) * 17 )/1000;
//if(Value_cm>1000){ Value_cm=0;}
Serial.print("D= ");
Serial.print(Value_cm);
Serial.println(" [cm]");
delay(100);
}  
delay(10);
}

void pulse(int angle)     //设置舵机角度为angle
{
   pulsewidth=int ((angle*11)+500);  //计算高电平时间
   digitalWrite(PWM_pin,HIGH);     //设置高电平
   delayMicroseconds(pulsewidth);   //延时pulsewidth (us)
   digitalWrite(PWM_pin,LOW);       //设置低电平
   delay(20-pulsewidth/1000);       //延时20-pulsewidth/1000 (ms)
}



Processing 代码:
import processing.serial.*;
PFont font;
String message;
String temp;
int mMaxDistance;
Serial myPort;
PFont myFont;
float x =0;
float angle=PI/180;
float xDirection = 150;
String[] data;
float X1=0;
float Y1=0;
void setup() {
   size(1200, 700);
   background(0);
   font = createFont("宋体.vlw",48);
   textFont(font);
myPort = new Serial(this,"com4", 9600); //设置数据来源串口号
}

void draw() {
background(0);
smooth();
int x0=600;
int y0=600;
int r=500;
noFill();
textSize(18);
strokeWeight(2);
text("超声波测距雷达显示屏", 430,25);
textSize(14);
text("[探索软件制 CopyRight@2018]", 650,25);
stroke(0,255,0);
strokeWeight(2);
rect(50,50,2*550,550);
stroke(255,255,0);
strokeWeight(2);
rect(50,625,2*550, 40);
stroke(0,255,0);
strokeWeight(1);
arc(x0,y0,r/2,r/2,PI,TWO_PI);
//arc(x0,y0,r/1.75,r/1.75,PI,TWO_PI);
arc(x0,y0,r/1.5,r/1.5,PI,TWO_PI);
arc(x0,y0,r/1.25,r/1.25,PI,TWO_PI);
arc(x0,y0,r,r,PI,TWO_PI);
arc(x0,y0,1.25*r,1.25*r,PI,TWO_PI);  
arc(x0,y0,1.5*r,1.5*r,PI,TWO_PI);  
arc(x0,y0,1.75*r,1.75*r,PI,TWO_PI);
arc(x0,y0,2*r,2*r,PI,TWO_PI);
text("0", x0-510,y0);
line(x0, y0, x0-r*cos(0*angle),y0-r*sin(0*angle));
text("15", x0-r*cos(15*angle)-17,y0-r*sin(15*angle));
line(x0, y0, x0-r*cos(15*angle),y0-r*sin(15*angle));
text("30", x0-r*cos(30*angle)-17,y0-r*sin(30*angle));
line(x0, y0, x0-r*cos(30*angle),y0-r*sin(30*angle));
text("45", x0-r*cos(45*angle)-17,y0-r*sin(45*angle));
line(x0, y0, x0-r*cos(45*angle),y0-r*sin(45*angle));
text("60", x0-r*cos(60*angle)-17,y0-r*sin(60*angle));
line(x0, y0, x0-r*cos(60*angle),y0-r*sin(60*angle));
text("75", x0-r*cos(75*angle)-17,y0-r*sin(75*angle));
line(x0, y0, x0-r*cos(75*angle),y0-r*sin(75*angle));
text("90", x0-r*cos(90*angle)-17,y0-r*sin(90*angle));
line(x0, y0, x0-r*cos(90*angle),y0-r*sin(90*angle));
text("105", x0-r*cos(105*angle)-15,y0-r*sin(105*angle)-5);
line(x0, y0, x0-r*cos(105*angle),y0-r*sin(105*angle));
text("120", x0-r*cos(120*angle)-10,y0-r*sin(120*angle)-5);
line(x0, y0, x0-r*cos(120*angle),y0-r*sin(120*angle));
text("135", x0-r*cos(135*angle)-8,y0-r*sin(135*angle)-5);
line(x0, y0, x0-r*cos(135*angle),y0-r*sin(135*angle));
text("150", x0-r*cos(150*angle)-3,y0-r*sin(150*angle)-3);
line(x0, y0, x0-r*cos(150*angle),y0-r*sin(150*angle));

text("165", x0-r*cos(165*angle)-3,y0-r*sin(165*angle)-3);
line(x0, y0, x0-r*cos(165*angle),y0-r*sin(165*angle));

text("180", x0-r*cos(180*angle)+2,y0-r*sin(180*angle));
line(x0, y0, x0-r*cos(180*angle),y0-r*sin(180*angle));

stroke(255,255,0);
//x = x + PI/xDirection ;
x = x + HALF_PI/xDirection ;
line(x0, y0, x0-r*cos(x),y0-r*sin(x));
//stroke(0,0,0);
//line(500, 500, 500-500*cos(x),500-500*sin(x));
if(myPort.available()>0){
temp = myPort.readString();
if(temp==""){temp="0.0";}
fill(255,255,255);
stroke(255,255,255);
text("角度:", 480+10,650);
text(int(x*60), 480+50,650);
text("度", 480+80,650);
text("距离:", 480+120,650);
text(int(temp), 480+160,650);
text("厘米", 480+200,650);
}else{
temp = "";
if(temp==""){temp="0.0";}
fill(255,255,255);
stroke(255,255,255);
text("角度:", 480+10,650);
text(int(x*60), 480+50,650);
text("度", 480+80,650);
text("距离:", 480+120,650);
text(int(temp), 480+160,650);
text("厘米", 480+200,650);
}
X1=int(temp)*cos(x);
Y1=int(temp)*sin(x);
ellipse(x0-X1, y0-Y1, 15, 15);
if(x>3.14){ x=0;}
}


视频:
https://pan.baidu.com/s/1siDzLxza5sVHmeyAbSnGDw

超声波雷达.mp4

3.53 MB, 下载次数: 17, 下载积分: 黑币 -5






欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1