//因为csdn不支持视频,所以放上来效果图.
//构想是用彩带围绕小车一圈,然后使用之前提到的红外遥感进行方向控制,同时在电脑上显示雷达图片
//雷达arduino代码
#include<Servo.h>
const int soundTriggerPin = 13;
const int soundEchoPin = 12;
const int motorSignalPin = 3;
const int startingAngle = 15;
const int minimumAngle = 15;
const int maximumAngle = 165;
const int rotationSpeed = 1;
Servo motor;
void setup(void)
{
pinMode(soundTriggerPin, OUTPUT);
pinMode(soundEchoPin, INPUT);
motor.attach(motorSignalPin);
Serial.begin(9600);
}
void loop(void)
{
static int motorAngle = startingAngle;
static int motorRotateAmount = rotationSpeed;
motor.write(motorAngle);
delay(30);
SerialOutput(motorAngle, CalculateDistance());
motorAngle += motorRotateAmount;
if(motorAngle <= minimumAngle || motorAngle >= maximumAngle) {
motorRotateAmount = -motorRotateAmount;
}}
int CalculateDistance(void)
{
digitalWrite(soundTriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(soundTriggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(soundTriggerPin, LOW);
long duration = pulseIn(soundEchoPin, HIGH);
float distance = duration * 0.017F;
return int(distance);
}
void SerialOutput(const int angle, const int distance)
{
String angleString = String(angle);
String distanceString = String(distance);
Serial.println(angleString + "," + distanceString);
}
//雷达processing1.51代码
import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
Serial myPort;
PFont orcFont;
int iAngle;
int iDistance;
void setup() {
size(1350, 760);
smooth();
myPort = new Serial(this, "COM9", 9600);
myPort.clear();
myPort.bufferUntil('\n');
orcFont = loadFont("OCRAExtended-30.vlw");
}
void draw()
{
fill(98, 245, 31);
textFont(orcFont);
noStroke();
fill(0, 4);
rect(0, 0, width, 0.935 * height);
fill(98, 245, 31);
DrawRadar();
DrawLine();
DrawObject();
DrawText();
}
void serialEvent (Serial myPort)
{
try {
String data = myPort.readStringUntil('\n');
if (data == null) {
return;
}
int commaIndex = data.indexOf(",");
String angle = data.substring(0, commaIndex);
String distance = data.substring(commaIndex+1, data.length()-1);
iAngle = StringToInt(angle);
iDistance = StringToInt(distance);
} catch(RuntimeException e) {}
}
void DrawRadar()
{
pushMatrix();
translate(width/2, 0.926 * height);
noFill();
strokeWeight(2);
stroke(98, 245, 31);
// draws the arc lines
DrawRadarArcLine(0.9375);
DrawRadarArcLine(0.7300);
DrawRadarArcLine(0.5210);
DrawRadarArcLine(0.3130);
// draws the angle lines
final int halfWidth = width/2;
line(-halfWidth, 0, halfWidth, 0);
for(int angle = 30; angle <= 150; angle+=30) {
DrawRadarAngledLine(angle);
}
line(-halfWidth * cos(radians(30)), 0, halfWidth, 0);
popMatrix();
}
void DrawRadarArcLine(final float coefficient)
{
arc(0, 0, coefficient * width, coefficient * width, PI, TWO_PI);
}
void DrawRadarAngledLine(final int angle){
line(0, 0, (-width/2) * cos(radians(angle)), (-width/2) * sin(radians(angle)));
}
void DrawObject()
{
pushMatrix();
translate(width/2, 0.926 * height);
strokeWeight(9);
stroke(255,10,10);
int pixsDistance = int(iDistance * 0.020835 * height);
if(iDistance < 40 && iDistance != 0) {
float cos = cos(radians(iAngle));
float sin = sin(radians(iAngle));
int x1 = +int(pixsDistance * cos);
int y1 = -int(pixsDistance * sin);
int x2 = +int(0.495 * width * cos);
int y2 = -int(0.495 * width * sin);
line(x1, y1, x2, y2);
}
popMatrix();
}
void DrawLine()
{
pushMatrix();
strokeWeight(9);
stroke(30, 250, 60);
translate(width/2, 0.926 * height);
float angle = radians(iAngle);
int x = int(+0.88 * height * cos(angle));
int y = int(-0.88 * height * sin(angle));
line(0, 0, x, y);
popMatrix();
}
void DrawText()
{
pushMatrix();
fill(0, 0, 0);
noStroke();
rect(0, 0.9352 * height, width, height);
fill(98, 245, 31);
textSize(25);
text("10cm", 0.6146 * width, 0.9167 * height);
text("20cm", 0.7190 * width, 0.9167 * height);
text("30cm", 0.8230 * width, 0.9167 * height);
text("40cm", 0.9271 * width, 0.9167 * height);
textSize(40);
text("Object: " + (iDistance > 40 ? "Out of Range" : "In Range"), 0.125 * width, 0.9723 * height);
text("Angle: " + iAngle + " °", 0.52 * width, 0.9723 * height);
text("Distance: ", 0.74 * width, 0.9723 * height);
if(iDistance < 40) {
text(" " + iDistance +" cm", 0.775 * width, 0.9723 * height);
}
textSize(25);
fill(98, 245, 60);
translate(0.5006 * width + width/2 * cos(radians(30)), 0.9093 * height - width/2 * sin(radians(30)));
rotate(-radians(-60));
text("30°",0,0);
resetMatrix();
translate(0.497 * width + width/2 * cos(radians(60)), 0.9112 * height - width/2 * sin(radians(60)));
rotate(-radians(-30));
text("60°",0,0);
resetMatrix();
translate(0.493 * width + width/2 * cos(radians(90)), 0.9167 * height - width/2 * sin(radians(90)));
rotate(radians(0));
text("90°",0,0);
resetMatrix();
translate(0.487 * width + width/2 * cos(radians(120)), 0.92871 * height - width/2 * sin(radians(120)));
rotate(radians(-30));
text("120°",0,0);
resetMatrix();
translate(0.4896 * width + width/2 * cos(radians(150)), 0.9426 * height - width/2 * sin(radians(150)));
rotate(radians(-60));
text("150°",0,0);
popMatrix();
}
int StringToInt(String string)
{
int value = 0;
for(int i = 0; i < string.length(); ++i) {
if(string.charAt(i) >= '0' && string.charAt(i) <= '9') {
value *= 10;
value += (string.charAt(i) - '0');
}}
return value;}
//这里放入彩带代码
//很无语的代码,在我写的时候本来想实现其他的功能,最后变得面目全非,不喜欢的可以在github上找neopixel的实例代码进行操作
彩虹灯1(红橙黄绿青紫)
#include <Adafruit_NeoPixel.h>
//strip.setPixelColor(n, red, green, blue, white);
//其中,第一个参数n是彩带中LED的编号,最接近单片机引脚的编号为0;
//接下来的三个参数描述像素颜色,分别表示红色、绿色和蓝色的亮度级别,0为最暗,255是最大亮度;
//最后一个white是可选参数,只适用于带独立正白光的全彩LED,即RGBW型LED。
#define PIN D1 //端口
Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_GRB + NEO_KHZ800); //第一个参数是控制的灯数量
uint8_t brightness = 0; //LED的亮度
uint8_t fadeAmount = 5; //亮度变化增量
void setup() {
strip.begin();
}
//loop 不断循环
void loop() {
int k=0;
brightness=0;
while(k<=2)
{
strip.setPixelColor(k,255,brightness,0);
strip.show();
brightness += 255/2;//红黄变换,通过前两个循环改变颜色
delay(500);
k++;
}
delay(500);
while(k<=4)//只计算到绿色和青色
{
brightness -= 255/2;//补上上次循环多加的255一半
strip.setPixelColor(k,0,brightness,255);
strip.show();
delay(500);
k++;
}
delay(500);
strip.setPixelColor(k,139,0,255);//紫色
strip.show();
delay(500);
k=0;//初始化到第0个灯
strip.setPixelColor(k,139,0,255);//紫色,倒过来计算
strip.show();
k++;
brightness=127;//处理到绿色和青色
while(k<=2)
{
strip.setPixelColor(k,0,brightness,255);
strip.show();
brightness += 255/2;
delay(500);
k++;
}
delay(500);
brightness -= 255/2;
while(k<=5)
{
strip.setPixelColor(k,255,brightness,0);
strip.show();
brightness -= 255/2;//为了让灯发出橙色的光;165
delay(500);
k++;
}
delay(500);//方便下一次循环的停顿
}