Arduino & Processing 超音波雷达问题
本帖最后由 sunbluegogo 于 2013-10-26 18:44 编辑有天看到這篇
Arduino + Processing – Make a Radar Screen to Visualise Sensor Data from SRF-05
http://luckylarry.co.uk/arduino-projects/arduino-processing-make-a-radar-screen-to-visualise-sensor-data-from-srf-05-part-2-visualising-the-data/
剛好手邊有現成材料想試試看
虽然步骤看起来很简单,
但是Processing的图就是不会动作
检查过程式码也只在Arduino那里发现少了「<Servo.h> 」而已
想问问看板上有没有朋友知道是哪里有问题?
我的软体环境
Arduino 0023
Processing 2.0.3 哇哦,看上去很 cool, 能做到电影里长出现的那种效果,图示方式检测到运动周边运动物体? 理论上可以检测到运动的物体
这个真的很cool 你的Processing 沒有連到序列阜
已经重新修复好了,代码如下:
IDE代码:
/*
luckylarry.co.uk
Radar Screen Visualisation for SRF-05
Sends sensor readings for every degree moved by the servo
values sent to serial port to be picked up by Processing
*/
#include <Servo.h> // include the standard servo library
Servo leftRightServo; // set a variable to map the servo
int leftRightPos = 0; // set a variable to store the servo position
const int numReadings = 10; // set a variable for the number of readings to take
int index = 0; // the index of the current reading
int total = 0; // the total of all readings
int average = 0; // the average
int echoPin = 2; // the SRF05's echo pin
int initPin = 3; // the SRF05's init pin
unsigned long pulseTime = 0;// variable for reading the pulse
unsigned long distance = 0; // variable for storing distance
/* setup the pins, servo and serial port */
void setup() {
leftRightServo.attach(9);
// make the init pin an output:
pinMode(initPin, OUTPUT);
// make the echo pin an input:
pinMode(echoPin, INPUT);
// initialize the serial port:
Serial.begin(9600);
}
/* begin rotating the servo and getting sensor values */
void loop() {
for (leftRightPos = 0; leftRightPos < 180; leftRightPos++) { // going left to right.
leftRightServo.write(leftRightPos);
for (index = 0; index <= numReadings; index++) { // take x number of readings from the sensor and average them
digitalWrite(initPin, LOW);
delayMicroseconds(50);
digitalWrite(initPin, HIGH); // send signal
delayMicroseconds(50); // wait 50 microseconds for it to return
digitalWrite(initPin, LOW); // close signal
pulseTime = pulseIn(echoPin, HIGH); // calculate time for signal to return
distance = pulseTime / 58; // convert to centimetres
total = total + distance; // update total
delay(10);
}
average = total / numReadings; // create average reading
if (index >= numReadings){ // reset the counts when at the last item of the array
index = 0;
total = 0;
}
Serial.print("X"); // print leading X to mark the following value as degrees
Serial.print(leftRightPos); // current servo position
Serial.print("V"); // preceeding character to separate values
Serial.println(average); // average of sensor readings
}
/*
start going right to left after we got to 180 degrees
same code as above
*/
for (leftRightPos = 180; leftRightPos > 0; leftRightPos--) { // going right to left
leftRightServo.write(leftRightPos);
for (index = 0; index <= numReadings; index++) {
digitalWrite(initPin, LOW);
delayMicroseconds(50);
digitalWrite(initPin, HIGH);
delayMicroseconds(50);
digitalWrite(initPin, LOW);
pulseTime = pulseIn(echoPin, HIGH);
distance = pulseTime / 58;
total = total + distance;
delay(10);
}
average = total / numReadings;
if (index >= numReadings){
index = 0;
total = 0;
}
Serial.print("X");
Serial.print(leftRightPos);
Serial.print("V");
Serial.println(average);
}
}
修改成功 代码如下:
/*
luckylarry.co.uk
Radar Screen Visualisation for SRF-05
Sends sensor readings for every degree moved by the servo
values sent to serial port to be picked up by Processing
*/
#include <Servo.h> // include the standard servo library
Servo leftRightServo; // set a variable to map the servo
int leftRightPos = 0; // set a variable to store the servo position
const int numReadings = 10; // set a variable for the number of readings to take
int index = 0; // the index of the current reading
int total = 0; // the total of all readings
int average = 0; // the average
int echoPin = 2; // the SRF05's echo pin
int initPin = 3; // the SRF05's init pin
unsigned long pulseTime = 0;// variable for reading the pulse
unsigned long distance = 0; // variable for storing distance
/* setup the pins, servo and serial port */
void setup() {
leftRightServo.attach(9);
// make the init pin an output:
pinMode(initPin, OUTPUT);
// make the echo pin an input:
pinMode(echoPin, INPUT);
// initialize the serial port:
Serial.begin(9600);
}
/* begin rotating the servo and getting sensor values */
void loop() {
for (leftRightPos = 0; leftRightPos < 180; leftRightPos++) { // going left to right.
leftRightServo.write(leftRightPos);
for (index = 0; index <= numReadings; index++) { // take x number of readings from the sensor and average them
digitalWrite(initPin, LOW);
delayMicroseconds(50);
digitalWrite(initPin, HIGH); // send signal
delayMicroseconds(50); // wait 50 microseconds for it to return
digitalWrite(initPin, LOW); // close signal
pulseTime = pulseIn(echoPin, HIGH); // calculate time for signal to return
distance = pulseTime / 58; // convert to centimetres
total = total + distance; // update total
delay(10);
}
average = total / numReadings; // create average reading
if (index >= numReadings){ // reset the counts when at the last item of the array
index = 0;
total = 0;
}
Serial.print("X"); // print leading X to mark the following value as degrees
Serial.print(leftRightPos); // current servo position
Serial.print("V"); // preceeding character to separate values
Serial.println(average); // average of sensor readings
}
/*
start going right to left after we got to 180 degrees
same code as above
*/
for (leftRightPos = 180; leftRightPos > 0; leftRightPos--) { // going right to left
leftRightServo.write(leftRightPos);
for (index = 0; index <= numReadings; index++) {
digitalWrite(initPin, LOW);
delayMicroseconds(50);
digitalWrite(initPin, HIGH);
delayMicroseconds(50);
digitalWrite(initPin, LOW);
pulseTime = pulseIn(echoPin, HIGH);
distance = pulseTime / 58;
total = total + distance;
delay(10);
}
average = total / numReadings;
if (index >= numReadings){
index = 0;
total = 0;
}
Serial.print("X");
Serial.print(leftRightPos);
Serial.print("V");
Serial.println(average);
}
} 为什么我已回复就删掉的? 修改成功:
/*
luckylarry.co.uk
Radar Screen Visualisation for SRF-05
Sends sensor readings for every degree moved by the servo
values sent to serial port to be picked up by Processing
*/
#include <Servo.h> // include the standard servo library
Servo leftRightServo; // set a variable to map the servo
int leftRightPos = 0; // set a variable to store the servo position
const int numReadings = 10; // set a variable for the number of readings to take
int index = 0; // the index of the current reading
int total = 0; // the total of all readings
int average = 0; // the average
int echoPin = 2; // the SRF05's echo pin
int initPin = 3; // the SRF05's init pin
unsigned long pulseTime = 0;// variable for reading the pulse
unsigned long distance = 0; // variable for storing distance
/* setup the pins, servo and serial port */
void setup() {
leftRightServo.attach(9);
// make the init pin an output:
pinMode(initPin, OUTPUT);
// make the echo pin an input:
pinMode(echoPin, INPUT);
// initialize the serial port:
Serial.begin(9600);
}
/* begin rotating the servo and getting sensor values */
void loop() {
for (leftRightPos = 0; leftRightPos < 180; leftRightPos++) { // going left to right.
leftRightServo.write(leftRightPos);
for (index = 0; index <= numReadings; index++) { // take x number of readings from the sensor and average them
digitalWrite(initPin, LOW);
delayMicroseconds(50);
digitalWrite(initPin, HIGH); // send signal
delayMicroseconds(50); // wait 50 microseconds for it to return
digitalWrite(initPin, LOW); // close signal
pulseTime = pulseIn(echoPin, HIGH); // calculate time for signal to return
distance = pulseTime / 58; // convert to centimetres
total = total + distance; // update total
delay(10);
}
average = total / numReadings; // create average reading
if (index >= numReadings){ // reset the counts when at the last item of the array
index = 0;
total = 0;
}
Serial.print("X"); // print leading X to mark the following value as degrees
Serial.print(leftRightPos); // current servo position
Serial.print("V"); // preceeding character to separate values
Serial.println(average); // average of sensor readings
}
/*
start going right to left after we got to 180 degrees
same code as above
*/
for (leftRightPos = 180; leftRightPos > 0; leftRightPos--) { // going right to left
leftRightServo.write(leftRightPos);
for (index = 0; index <= numReadings; index++) {
digitalWrite(initPin, LOW);
delayMicroseconds(50);
digitalWrite(initPin, HIGH);
delayMicroseconds(50);
digitalWrite(initPin, LOW);
pulseTime = pulseIn(echoPin, HIGH);
distance = pulseTime / 58;
total = total + distance;
delay(10);
}
average = total / numReadings;
if (index >= numReadings){
index = 0;
total = 0;
}
Serial.print("X");
Serial.print(leftRightPos);
Serial.print("V");
Serial.println(average);
}
} 我在研究这个,原来有人已经成功了
页:
[1]