1:processing
2:arduino代码,自己稍改了一下
const int TrigPin = 3;
const int EchoPin = 4;
float SR04cm;
float m;
float dis=0;
float dis2=0;
float SR04()
{
digitalWrite
(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(12);
digitalWrite(TrigPin, LOW);
SR04cm = pulseIn(EchoPin, HIGH) / 58.00; //算成厘米 //
SR04cm = (int(m * 100.0)) / 100.0; //保留两位小数
delayMicroseconds(2);return SR04cm;}
void setup()
{
XXXXXXXXXgin(9600); pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop()
{
int echocm=SR04();
XXXXXXXXXintln(echocm); delay(10);
}
processing代码,没改过
import XXXXXXXXXXXXXrial.*; Serial myPort=new Serial(this, XXXXXXXXXst()[1], 9600);float r=250;
float a=0;
float x, y;
void setup()
{
size(600, 600);
background(0);
smooth();
stroke(255);
frameRate(100);
println(XXXXXXXXXst()); }
void draw()
{
displayX();
displayY();
fill(0,3);
noStroke();
rect(0, 0, width, height);
x=r*cos(a);
y=r*sin(a);
if (myPort.available()>0)
{
int data;
data=XXXXXXXXXad(); fill(255);
text("DISTANCE:", 30, 50);
text(data, 100, 50);
stroke(0, 200, 0);
strokeWeight(4);
line(width/2, height/2, width/2+x, height/2+y);//步进电机转一下,扫描线转一下
a=a+0.0124;//弧度增加
if (data<=50)
{
stroke(255, 0, 0);
strokeWeight(10);
float locx, locy;
locx=5*(data-1)*cos(a);
locy=5*(data-1)*sin(a);
point(width/2+locx, height/2+locy);
}
}
}
void displayX()
{
for (int i=-250;i<300;i+=50)
{
fill(255);
if (i<=0)
text(i, i+300, 300);
else
text(i, i+285, 300);
}
}
void displayY()
{
for (int i=250;i>=-250;i-=50)
{
if (i!=0)
{
fill(255);
text(i, 300, 300-i);
}
}
}
截图: