首先来到arduino中文社区下载并安装arduinoIDE(在资源中心里面)

然后来到processing官网下载并安装processing软件

本次制作用到了Arduino集成的servo库,其中包含了可以控制舵机的函数:
attach():连接设定舵机的接口;
write():用于设定舵机旋转角度的语句,可设定的角度范围一般是0°到180°;
delayMicroseconds():用于设定舵机旋转角度的语句,直接用微秒作为参数;
read():用于读取舵机角度的语句;
attached():判断舵机参数是否已发送到舵机所在接口;
detach():使舵机与其接口分离。
超声波传感器有4个引脚:GND、VCC、Trig和Echo ,模块的GND和VCC分别连接到Arduino开发板的GND和5V引脚就可以了。Trig和Echo引脚可以连接到Arduino开发板上的任意数字接口(本程序用的是10)
为了产生超声波,需要将Trig设置为高电平并持续10μs。这将使超声波传感器发出一个声波脉冲,它将声速340米/秒在空气中传播,如果碰到物体反射回来,就会在Echo引脚中接收。 Echo引脚将输出声波传播的时间,以微秒为单位.。
接下来连接线路

//arduion部分代码
#include <Servo.h>//引用arduino舵机库
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
Servo myServo;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
myServo.attach(12);
}
void loop() {
for (pos = 0; pos <= 180; pos ++) { // 0°到180°
// in steps of 1 degree
myservo.write(pos); // 舵机角度写入
delay(5); // 等待转动到指定角度
}
for (pos = 180; pos >= 0; pos --) { // 从180°到0°
myservo.write(pos); // 舵机角度写入
delay(5); // 等待转动到指定角度
}
}
//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;
Serial myPort;
String angle="";
String distance="";
String data="";
String noObject;
float pixsDistance;
int iAngle, iDistance;
int index1=0;
int index2=0;
PFont orcFont;
void setup() {
size (1000, 720); // 定义程序窗口的大小
smooth();
myPort = new Serial(this,"COM6", 9600); // 启动串口通信(注意串口传输速率应与arduino设置的一致)
myPort.bufferUntil('.'); // 从串口读取数据
orcFont = loadFont("AgencyFB-Bold-48.vlw");
}
void draw() {
fill(98,245,31);
textFont(orcFont);
// 模拟运动模糊和缓慢淡出的移动线
noStroke();
fill(0,4);
rect(0, 0, width, height-height*0.065);
fill(98,245,31);
drawRadar(); // 调用绘制雷达的函数
drawLine();
drawObject();
drawText();
}
void serialEvent (Serial myPort) { //开始从串口读取数据
//从串口读取数据直到字符'。并将其放入字符串变量data中".
data = myPort.readStringUntil('.');
data = data.substring(0,data.length()-1);
index1 = data.indexOf(","); // 找到字符','并将其放入变量' index1"
angle= data.substring(0, index1); //从位置“0”读取数据到变量index1的位置或Arduino板送入串口的角度值
distance= data.substring(index1+1, data.length()); //从位置“index1”读取数据到数据的末尾,pr是距离的值
//将String变量转换为Integer
iAngle = int(angle);
iDistance = int(distance);
}
void drawRadar() {
pushMatrix();
translate(width/2,height-height*0.074); // 将起始坐标移动到新位置
noFill();
strokeWeight(2);
stroke(98,245,31);
//绘制弧线
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);
// 绘制角线
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);
pixsDistance = iDistance*((height-height*0.1666)*0.025); //覆盖从传感器到像素的距离,将范围限制为40 CMS
if(iDistance<40){//将范围限制为40 CMS
// 根据角度和距离绘制对象
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))); // 根据角度画线
popMatrix();
}
void drawText() { // draws the texts on the screen
pushMatrix();
if(iDistance>40) {
noObject = "Out of Range";
}
else {
noObject = "In Range";
}
fill(0,0,0);
noStroke();
rect(0, height-height*0.0648, width, height);
fill(98,245,31);
textSize(25);
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(40);
text("Object: " + noObject, width-width*0.875, height-height*0.0277);
text("Angle: " + iAngle +" °", width-width*0.48, height-height*0.0277);
text("Distance: ", width-width*0.26, height-height*0.0277);
if(iDistance<40) {
text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277);
}
textSize(25);
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();
}


全部评论