超声波测距传感器的读取与显示
Arduino读取超声波传感器
int outputPin =2; //接超声波Trig到数字D2脚
int inputPin =3; //接超声波Echo到数字D3脚
void setup()
{
Serial.begin(9600);
pinMode(inputPin,INPUT);
pinMode(outputPin,OUTPUT);
}
void loop()
{
digitalWrite(outputPin,LOW);
delayMicroseconds(2);
digitalWrite(outputPin,HIGH);//发出持续时间为10微秒到Trig脚驱动超声波检测
delayMicroseconds(10);
digitalWrite(outputPin,LOW);
int distance =pulseIn(inputPin,HIGH); //接收脉冲的时间
distance =distance/58; //将脉冲时间转换为距离值
Serial.print("The distance is :");
Serial.println(distance);
delay(50);
}
串口监测
Processing绘制距离与提示
读取一张电动车的图片,在右边画上一堵墙,墙上的读数是电动车与墙的距离。用键盘“左”和“右”键控制电动车原理或靠近墙。与墙的距离会实时显示在屏幕右上角。最远为450cm,最近为0cm。
PImage electrocar;
int x =0,y =220;
int distance =0;
void setup()
{
size(640,480);
electrocar =loadImage("diandongche.jpg"); //读取图片
background(255);
image(electrocar,x,y);
}
void draw()
{
background(255);
fill(30,40,40);
rect(600,80,30,395);
image(electrocar,x,y);
distance=450-x*450/(600-electrocar.width); //换算与墙之间的距离,设其最大值为450m
text("Distance is "+distance+"CM",250,50);
}
void keyPressed() //特殊键的key值为CODED,此时需要keyCode再次判断
{
if(key==CODED)
{
switch(keyCode)
{
case LEFT: //按下“左”键
if(x>0)
{
x=x-1;
}
else
{
x=0;
}
break;
case RIGHT: //按下“右”键
if(x<(600-electrocar.width))
{
x=x+1;
}
else
{
x= 600-electrocar.width;
}
break;
default:
break;
}
}
}
根据该距离值与墙的距离绘制电动车。超声波测距传感器读取的数值大,则电动车离墙的距离远,读取的数值小,则电动车离墙的距离近。
processing代码:
import processing.serial.*;
Serial myPort;
PImage electrocar;
int x =0,y =220;
int distance =0;
void setup()
{
size(640,480);
electrocar =loadImage("diandongche.jpg");
myPort =new Serial(this,"COM3",9600);
}
void draw()
{
if(myPort.available()>0)
{
distance =myPort.read();
println(distance);
}
background(255);
fill(30,40,40);
rect(600,80,30,395);
x =(450-distance)*(600-electrocar.width)/450; //将读取的距离值换算成实际的坐标
image(electrocar,x,y);
text("Distance is "+distance+"CM",250,50);
}
Arduino代码
int outputPin =2; //接超声波Trig到数字D2脚
int inputPin =3; //接超声波Echo到数字D3脚
void setup()
{
Serial.begin(9600);
pinMode(inputPin,INPUT);
pinMode(outputPin,OUTPUT);
}
void loop()
{
digitalWrite(outputPin,LOW);
delayMicroseconds(2);
digitalWrite(outputPin,HIGH);//发出持续时间为10微秒到Trig脚驱动超声波检测
delayMicroseconds(10);
digitalWrite(outputPin,LOW);
int distance =pulseIn(inputPin,HIGH); //接收脉冲的时间
distance =distance/58; //将脉冲时间转换为距离值
Serial.write(distance);
delay(50);
}
控制彩色LED灯
int Red =3;
int Green =5;
int Blue =6;
void setup()
{
pinMode(Red,OUTPUT);
pinMode(Green,OUTPUT);
pinMode(Blue,OUTPUT);
}
void loop()
{
analogWrite(Red,random(0,255));
analogWrite(Green,random(0,255));
analogWrite(Blue,random(0,255));
delay(200);
}
Processing进度条控制
int redDisplay,greenDisplay,blueDisplay; //定义R,G,B 3个颜色分量显示的长度变量
int red,green,blue; //定义R,G,B三种颜色分量的值的变量
void setup()
{
size(700,500);
}
void draw()
{
background(204);
rectMode(CORNER);
noFill(); //不填充颜色
strokeWeight(2); //三个进度条的边框厚度为2个像素
stroke(255,0,0); //描边颜色为红色
rect(50,100,500,50); //绘制红色分量进度条的边框
stroke(0,255,0); //描边颜色为绿色
rect(50,200,500,50); //绘制绿色分量进度条的边框
stroke(0,0,255); //描边颜色为蓝色
rect(50,300,500,50); //绘制蓝色分量进度条的边框
fill(red,0,0); //填充颜色为当前红色分量的值
rect(50,100,redDisplay,50); //以当前红色分量的长度画出进度条
fill(0,green,0); //填充颜色为当前绿色分量的值
rect(50,200,greenDisplay,50); //以当前绿色分量的长度画出进度条
fill(0,0,blue); //填充颜色为当前蓝色分量的值
rect(50,300,blueDisplay,50); //以当前蓝色分量的长度画出进度条
fill(red,green,blue); //以当前R,G,B分量合成的颜色值为填充,画出一个矩形
rect(300,400,50,50);
}
void mouseClicked() //定义一个鼠标事件
{
if(mouseX<=550&&mouseX>=50)
{
if(mouseY<=150&&mouseY>=100) //当鼠标指针在该范围时,读取mouseX的值,映射到0~255,为红色颜色分量的值
{
red =(int)map(mouseX,50,550,0,255);
redDisplay =mouseX-50;
}
else if(mouseY<=250&&mouseY>=200)
{
green =(int)map(mouseX,50,550,0,255);
greenDisplay =mouseX-50;
}
else if(mouseY<=350&&mouseY>=300)
{
blue=(int)map(mouseX,50,550,0,255);
blueDisplay =mouseX-50;
}
}
}
扫描二维码关注公众号,回复:
10875860 查看本文章
Processing调节彩色LED灯
processing通过R,G,B分量经串口发送到Arduino上面。Arduino经过处理数据,将R,G,B各分量传送到RGB彩色LED模块,就能实现颜色的改变。
processing代码
import processing.serial.*;
Serial port =new Serial(this,"COM3",9600);
int redDisplay,greenDisplay,blueDisplay;
int red,green,blue;
String strRed,strGreen,strBlue;
String message;
void setup()
{
size(700,500);
}
void draw()
{
background(204);
frame(); //绘制进度条边框
RGBDisplay(); //绘制进度条
sendMessage(); //发送数据
}
void frame() //绘制进度条边框函数
{
rectMode(CORNER);
noFill();
stroke(255,0,0);
strokeWeight(2);
rect(50,100,500,50);
stroke(0,255,0);
rect(50,200,500,50);
stroke(0,0,255);
rect(50,300,500,50);
}
void RGBDisplay() //绘制进度条函数
{
rectMode(CORNER);
noStroke();
fill(red,0,0);
rect(50,100,redDisplay,50);
fill(0,green,0);
rect(50,200,greenDisplay,50);
fill(0,0,blue);
rect(50,300,blueDisplay,50);
fill(red,green,blue);
rect(300,400,50,50);
fill(0);
rect(400,400,50,50);
}
void mouseClicked() //单击鼠标事件,改变现实的进度条,同时更新R,G,B各分量的值
{
if(mouseX<=550&&mouseX>=50)
{
if(mouseY<=150&&mouseY>=100)
{
red =(int)map(mouseX,50,550,0,255);
redDisplay =mouseX-50;
}
if(mouseY<=250&&mouseY>=200)
{
green =(int)map(mouseX,50,550,0,255);
greenDisplay =mouseX-50;
}
if(mouseY<=350&&mouseY>=300)
{
blue =(int)map(mouseX,50,550,0,255);
blueDisplay =mouseX-50;
}
if(mouseX<=450&&mouseX>=400)
if(mouseY>=400&&mouseY<=450)
{
shut(); //当鼠标在该区域单击时,关闭LED灯
}
}
}
void sendMessage() //定义发送数据函数
{
strRed =red+""; //将red分量转换为字符串类型
if(red<100) //当red分量小于100时,将red转换为字符串,并且在前面加上字符0
strRed ="0"+red;
if(red<10) //当red分量小于10时,将red转换为字符串,并且在前面加上字符00
strRed="00"+red;
strGreen =green+"";
if(green<100)
strGreen="0"+green;
if(green<10)
strGreen="00"+green;
strBlue =blue+"";
if(blue<100)
strBlue ="0"+blue;
if(blue<10)
strBlue="00"+blue;
message ="a"+strRed+strGreen+strBlue+"s"; //将R,G,B各分量连接为字符串,通过串口发送到Arduino
port.write(message);
}
void shut() //定义关闭LED灯函数
{
redDisplay =0;
greenDisplay =0;
blueDisplay =0;
red =0;
green =0;
blue =0;
}
遥感控制坦克
Arduino读取摇杆数值
int Xaxis =A0;
int Yaxis =A1;
int SW =A2;
int value =0; //该变量读取模拟口的值
void setup()
{
Serial.begin(9600);
}
void loop()
{
value =analogRead(Xaxis); //读取X轴模拟端口0
Serial.print("X:");
Serial.print(value,DEC);
value =analogRead(Yaxis); //读取Y轴模拟端口1
Serial.print("| Y:");
Serial.print(value,DEC); //读取SW按钮模拟端口2
value =analogRead(SW);
Serial.print("| SW:");
Serial.println(value,DEC); //该值设置需大小适中,太大了延时较长
delay(40);
}
Processing绘制坦克和键盘控制移动
int x=300,y=200; //坦克的起始坐标
int speed =3; //坦克的移动速度
class Tank
{
Tank()
{
}
void DisplayTank(int px,int py,char direction) //显示坦克的函数
{
background(204);
switch(direction) //判断坦克的方向
{
case 'F': //前进
fill(255,255,0);
rect(px-10,py,20,40);
rect(px+30,py,20,40);
ellipse(px+20,py+20,20,25);
rect(px+17,py-10,6,19);
break;
case 'B': //后退
fill(255,255,0);
rect(px-10,py,20,40);
rect(px+30,py,20,40);
ellipse(px+20,py+20,20,25);
rect(px+17,py+30,6,19);
break;
case 'L': //左行
fill(255,255,0);
rect(px,py-10,40,20);
rect(px,py+30,40,20);
ellipse(px+20,py+20,25,20);
rect(px-10,py+17,19,6);
break;
case 'R': //右行
fill(255,255,0);
rect(px,py-10,40,20);
rect(px,py+30,40,20);
ellipse(px+20,py+20,25,20);
rect(px+30,py+17,19,6);
break;
default:
break;
}
}
void boundary(int px,int py) //坦克超出边界判断
{
if(px<10) x=10;
if(py<10) y =10;
if(px>(width-50)) x=width-50;
if(py>(height-50)) y =height-50;
}
}
Tank tank;
void setup()
{
size(640,480);
tank =new Tank();
tank.DisplayTank(x,y,'F'); //出现在初始位置
}
void draw()
{
}
void keyPressed()
{
if(key==CODED) //特殊键的key值为CODED,此时需要keyCode再次判断
{
switch(keyCode)
{
case UP:
y=y-speed;
tank.boundary(x,y);
tank.DisplayTank(x,y,'F');
break;
case DOWN:
y=y+speed;
tank.boundary(x,y);
tank.DisplayTank(x,y,'B');
break;
case LEFT:
x=x-speed;
tank.boundary(x,y);
tank.DisplayTank(x,y,'L');
break;
case RIGHT:
x=x+speed;
tank.boundary(x,y);
tank.DisplayTank(x,y,'R');
break;
default:
break;
}
}
}
摇杆控制坦克移动
processing代码
import processing.serial.*;
Serial myPort;
public static final char HEADER ='M';
public static final short LF =10;
public static final short portIndex =2;
public static final short centerX=515;
public static final short centerY=517;
int x=300,y=200;
int speed =3;
char Direction;
class Tank
{
Tank()
{
}
void DisplayTank(int px,int py,char direction)
{
background(204);
switch(direction)
{
case 'F':
fill(255,255,0);
rect(px-10,py,20,40);
rect(px+30,py,20,40);
ellipse(px+20,py+20,20,25);
rect(px+17,py-10,6,19);
break;
case 'B':
fill(255,255,0);
rect(px-10,py,20,40);
rect(px+30,py,20,40);
ellipse(px+20,py+20,20,25);
rect(px+17,py+30,6,19);
break;
case 'L':
fill(255,255,0);
rect(px,py-10,40,20);
rect(px,py+30,40,20);
ellipse(px+20,py+20,25,20);
rect(px-10,py+17,19,6);
break;
case 'R':
fill(255,255,0);
rect(px,py-10,40,20);
rect(px,py+30,40,20);
ellipse(px+20,py+20,25,20);
rect(px+30,py+17,19,6);
break;
default:
break;
}
}
void boundary(int px,int py)
{
if(px<10) x=10;
if(py<10) y=10;
if(px>(width-50)) x=width-50;
if(py>(height-50)) y=height-50;
}
char tankDirection(int px,int py)
{
if((px<(centerX-30))&&(abs(px-centerX)>abs(py-centerY)))
Direction ='L';
if((px>(centerX+30))&&(abs(px-centerX)>abs(py-centerY)))
Direction ='R';
if((py<(centerY-30))&&(abs(px-centerX)<abs(py-centerY)))
Direction ='F';
if((py>(centerY+30))&&(abs(px-centerX)<abs(py-centerY)))
Direction ='B';
if((px>=(centerX-30))&&(px<=(centerX+30))&&(py>=(centerY-30))&&(py<=(centerY+30)))
Direction='S';
return Direction;
}
}
Tank tank;
void setup()
{
size(640,480);
tank =new Tank();
tank.DisplayTank(x,y,'F');
myPort =new Serial(this,"COM3",9600);
myPort.clear();
}
void draw()
{
}
void serialEvent(Serial myPort)
{
String message =myPort.readStringUntil(LF);
char direction;
if(message!=null)
{
print(message);
String [] data =message.split(",");
println(data[1]);
println(data[2]);
if(data[0].charAt(0)==HEADER)
{
if(data.length>2)
{
int tempX =Integer.parseInt(data[1]);
int tempY =Integer.parseInt(data[2]);
print("x="+tempX);
print(",y="+tempY);
direction =tank.tankDirection(tempX,tempY);
switch(direction)
{
case 'F':
y =y-speed;
tank.boundary(x,y);
tank.DisplayTank(x,y,'F');
break;
case 'B':
y =y+speed;
tank.boundary(x,y);
tank.DisplayTank(x,y,'B');
break;
case 'L':
x =x-speed;
tank.boundary(x,y);
tank.DisplayTank(x,y,'L');
break;
case 'R':
x =x+speed;
tank.boundary(x,y);
tank.DisplayTank(x,y,'R');
break;
case 'S':
break;
default:
break;
}
}
}
}
}
arduino代码
int potXPin =A0;
int potYPin =A1;
char HEADER ='M';
void setup()
{
Serial.begin(9600);
}
void loop()
{
int x =analogRead(potXPin); //读取摇杆X轴数据
int y =analogRead(potYPin); //读取摇杆Y轴数据
Serial.print(HEADER); //输出数据帧头
Serial.print(","); //逗号分隔符
Serial.print(x,DEC); //X轴数据
Serial.print(","); //逗号分隔符
Serial.print(y,DEC); //Y轴数据
Serial.print(","); //逗号分隔符
Serial.println(); //输出换行符作为帧尾
delay(40);
}