Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
arduino uno和超声波传感器的结果不准确_Arduino_Arduino Ide_Arduino Ultra Sonic - Fatal编程技术网

arduino uno和超声波传感器的结果不准确

arduino uno和超声波传感器的结果不准确,arduino,arduino-ide,arduino-ultra-sonic,Arduino,Arduino Ide,Arduino Ultra Sonic,您好,我使用的超声波传感器有以下代码,但结果不准确,我想以厘米为单位显示与目标的距离 #include <LiquidCrystal.h> //Load Liquid Crystal Library LiquidCrystal LCD(10, 9, 5, 4, 3, 2); //Create Liquid Crystal Object called LCD int trigPin=13; //Sensor Trip pin connected to Arduino pin

您好,我使用的超声波传感器有以下代码,但结果不准确,我想以厘米为单位显示与目标的距离

    #include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(10, 9, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD

int trigPin=13; //Sensor Trip pin connected to Arduino pin 13
int echoPin=11;  //Sensor Echo pin connected to Arduino pin 11
int myCounter=0;  //declare your variable myCounter and set to 0
int servoControlPin=6; //Servo control line is connected to pin 6
float pingTime;  //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.

void setup() {

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Target Distance:");  //Print Message on First Row
}

void loop() {

  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state

  pingTime = pulseIn(echoPin, HIGH);  //pingTime is presented in microceconds
  pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
  targetDistance= speedOfSound * pingTime;  //This will be in miles, since speed of sound was miles per hour
  targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
  targetDistance= targetDistance*63360;    //Convert miles to inches by multipling by 63360 (inches per mile)

  LCD.setCursor(0,1);  //Set cursor to first column of second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  LCD.print(targetDistance); //Print measured distance
  LCD.print(" inches");  //Print your units.
  delay(250); //pause to let things settle


  }  
pingTime = pulseIn(echoPin, HIGH);  // pingTime is presented in microseconds
targetDistance = pingTime * pingTime_multiplier;
#包含//加载液晶库
液晶显示器(10,9,5,4,3,2)//创建名为LCD的液晶对象
int-trigPin=13//传感器跳闸针脚连接至Arduino针脚13
int-echoPin=11//传感器回波引脚连接至Arduino引脚11
int-myCounter=0//声明变量myCounter并设置为0
int伺服控制引脚=6//伺服控制线路连接到针脚6
浮动时间//ping从传感器移动到目标并返回的时间
浮动目标距离//到目标的距离(英寸)
浮动声速=776.5//温度为77度时的声速,以英里/小时为单位。
无效设置(){
Serial.begin(9600);
引脚模式(trigPin,输出);
pinMode(echoPin,输入);
begin(16,2);//告诉Arduino启动16列2行LCD
setCursor(0,0);//将LCD光标设置到左上角第0列第0行
LCD.print(“目标距离:”;//在第一行打印消息
}
void循环(){
digitalWrite(触发器引脚,低);//将触发器引脚设置为低
延迟微秒(2000);//让信号稳定下来
digitalWrite(trigPin,高);//将trigPin设置为高
延迟微秒(15);//高状态下的延迟
digitalWrite(trigPin,低位);//ping现在已发送
延迟微秒(10);//高状态下的延迟
pingTime=pulseIn(echoPin,高);//pingTime以微秒表示
pingTime=pingTime/1000000;//将pingTime除以1000000(微秒)转换为秒
pingTime=pingTime/3600;//将pingTime除以3600(一小时内的秒数)转换为小时数
targetDistance=声速*pingTime;//这将以英里为单位,因为声速是英里每小时
targetDistance=targetDistance/2;//请记住ping在目标之间来回移动,因此必须除以2才能获得实际目标距离。
targetDistance=targetDistance*63360;//乘以63360(英寸/英里),将英里转换为英寸
LCD.setCursor(0,1);//将光标设置到第二行的第一列
LCD.print(“”;//打印空格以清除该行
LCD.setCursor(0,1);//再次将光标设置到第二行的第一列
LCD.print(targetDistance);//打印测量距离
LCD.print(“英寸”);//打印单位。
延迟(250);//停下来让事情解决
}  
我将感谢任何帮助

我想以厘米为单位显示与目标的距离

    #include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(10, 9, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD

int trigPin=13; //Sensor Trip pin connected to Arduino pin 13
int echoPin=11;  //Sensor Echo pin connected to Arduino pin 11
int myCounter=0;  //declare your variable myCounter and set to 0
int servoControlPin=6; //Servo control line is connected to pin 6
float pingTime;  //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.

void setup() {

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Target Distance:");  //Print Message on First Row
}

void loop() {

  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state

  pingTime = pulseIn(echoPin, HIGH);  //pingTime is presented in microceconds
  pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
  targetDistance= speedOfSound * pingTime;  //This will be in miles, since speed of sound was miles per hour
  targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
  targetDistance= targetDistance*63360;    //Convert miles to inches by multipling by 63360 (inches per mile)

  LCD.setCursor(0,1);  //Set cursor to first column of second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  LCD.print(targetDistance); //Print measured distance
  LCD.print(" inches");  //Print your units.
  delay(250); //pause to let things settle


  }  
pingTime = pulseIn(echoPin, HIGH);  // pingTime is presented in microseconds
targetDistance = pingTime * pingTime_multiplier;
谷歌说一英寸是2.54厘米,所以用这个多倍于你的结果

结果并不准确

为了获得更准确的结果,请取多个样本(3个或更多)的平均值,您可以实施一些启发式方法,这些方法会丢弃严重超出/低于范围的结果

假设三个读数相距约0-5cm,则第四个读数相距40cm很可能是错误的。因此,只需对三个类似结果进行平均

我想以厘米为单位显示与目标的距离

    #include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(10, 9, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD

int trigPin=13; //Sensor Trip pin connected to Arduino pin 13
int echoPin=11;  //Sensor Echo pin connected to Arduino pin 11
int myCounter=0;  //declare your variable myCounter and set to 0
int servoControlPin=6; //Servo control line is connected to pin 6
float pingTime;  //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.

void setup() {

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Target Distance:");  //Print Message on First Row
}

void loop() {

  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state

  pingTime = pulseIn(echoPin, HIGH);  //pingTime is presented in microceconds
  pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
  targetDistance= speedOfSound * pingTime;  //This will be in miles, since speed of sound was miles per hour
  targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
  targetDistance= targetDistance*63360;    //Convert miles to inches by multipling by 63360 (inches per mile)

  LCD.setCursor(0,1);  //Set cursor to first column of second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  LCD.print(targetDistance); //Print measured distance
  LCD.print(" inches");  //Print your units.
  delay(250); //pause to let things settle


  }  
pingTime = pulseIn(echoPin, HIGH);  // pingTime is presented in microseconds
targetDistance = pingTime * pingTime_multiplier;
谷歌说一英寸是2.54厘米,所以用这个多倍于你的结果

结果并不准确

为了获得更准确的结果,请取多个样本(3个或更多)的平均值,您可以实施一些启发式方法,这些方法会丢弃严重超出/低于范围的结果


假设三个读数相距约0-5cm,则第四个读数相距40cm很可能是错误的。因此,只需对三个类似结果进行平均。

将英寸乘以2.54即可转换为厘米

float targetDistanceCentimeters = targetDistance*2.54;
您的代码看起来是正确的,所以我猜这是硬件问题。根据我的经验,超声波传感器通常在测量到任何物体的距离时非常糟糕,这些物体不是墙壁或其他表面相对平坦的大型物体。因此,如果你想测试你的系统的准确性,那么就用墙或书这样的物体来测试它。 如果您的传感器在进行测量时正在移动,请确保它没有移动过快。 如果要测量到小物体或薄物体的距离,则需要过滤并取测量值的平均值。这取决于你想要的准确度,我想说平均20个左右的过滤测量值会给你合适的结果


另一个要考虑的是,你有正确的电源电压,并且传感器没有指向某个角度的地板,因为这可能会引起不必要的反射。 乘以2.54将英寸转换为厘米

float targetDistanceCentimeters = targetDistance*2.54;
您的代码看起来是正确的,所以我猜这是硬件问题。根据我的经验,超声波传感器通常在测量到任何物体的距离时非常糟糕,这些物体不是墙壁或其他表面相对平坦的大型物体。因此,如果你想测试你的系统的准确性,那么就用墙或书这样的物体来测试它。 如果您的传感器在进行测量时正在移动,请确保它没有移动过快。 如果要测量到小物体或薄物体的距离,则需要过滤并取测量值的平均值。这取决于你想要的准确度,我想说平均20个左右的过滤测量值会给你合适的结果


另一个要考虑的是,你有正确的电源电压,并且传感器没有指向某个角度的地板,因为这可能会引起不必要的反射。 您在
循环的每次迭代中都要执行大量浮点计算。不必执行这些计算,您可以预先计算单个乘数,将
pingTime
转换为以厘米为单位的距离

    #include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(10, 9, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD

int trigPin=13; //Sensor Trip pin connected to Arduino pin 13
int echoPin=11;  //Sensor Echo pin connected to Arduino pin 11
int myCounter=0;  //declare your variable myCounter and set to 0
int servoControlPin=6; //Servo control line is connected to pin 6
float pingTime;  //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.

void setup() {

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Target Distance:");  //Print Message on First Row
}

void loop() {

  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state

  pingTime = pulseIn(echoPin, HIGH);  //pingTime is presented in microceconds
  pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
  targetDistance= speedOfSound * pingTime;  //This will be in miles, since speed of sound was miles per hour
  targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
  targetDistance= targetDistance*63360;    //Convert miles to inches by multipling by 63360 (inches per mile)

  LCD.setCursor(0,1);  //Set cursor to first column of second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  LCD.print(targetDistance); //Print measured distance
  LCD.print(" inches");  //Print your units.
  delay(250); //pause to let things settle


  }  
pingTime = pulseIn(echoPin, HIGH);  // pingTime is presented in microseconds
targetDistance = pingTime * pingTime_multiplier;
首先,我们需要从你的776.5英里/小时计算音速

776.5英里/小时*1609.34(米/英里)=1249652.51米/小时

1249652.51米/小时/3600=347.126米/秒

所以我们可以预先计算一个乘数