Arduino超声波传感器值不是0,如果存在';在超声波传感器前面有一个物体

Arduino超声波传感器值不是0,如果存在';在超声波传感器前面有一个物体,arduino,Arduino,我有3个超声波传感器,问题是它的读数不是0,而是1606、1602和1619 另一个问题是,当我将一个对象放在传感器前面时,值没有变化 我使用的代码如下所示: #include <Arduino.h> #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <ESP8266HTTPClient.h> #include <WiFiClient.h> ESP82

我有3个超声波传感器,问题是它的读数不是0,而是1606、1602和1619

另一个问题是,当我将一个对象放在传感器前面时,值没有变化

我使用的代码如下所示:

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

ESP8266WiFiMulti WiFiMulti;

// defines pins numbers
const int trigPin_1 = D1; 
const int echoPin_1 = D0; 

const int trigPin_2 = D6;  
const int echoPin_2 = D5;  

const int trigPin_3 = D7;  
const int echoPin_3 = D8;  


String uri = "http://192.168.0.128/socketprogramming/client.php";

// defines variables
long duration_1;
int distance_1;

long duration_2;
int distance_2;


long duration_3;
int distance_3;

WiFiClient client;
HTTPClient http;


void setup()
{
  Serial.begin(115200);
 // Serial.setDebugOutput(true);
  //Serial.println();
 for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("TP-LINK_86AC", "79043120");
 // wifiMulti.addAP("secondary-network-name", "pass-to-secondary-network");
  //wifiMulti.addAP("tertiary-network-name", "pass-to-tertiary-network");

  pinMode(trigPin_1, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin_1, INPUT); // Sets the echoPin as an Input

  pinMode(trigPin_2, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin_2, INPUT); // Sets the echoPin as an Input

  pinMode(trigPin_3, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin_3, INPUT); // Sets the echoPin as an Input
}



void ultrasonic(int type){

  if(type == 1){ 

      digitalWrite(trigPin_1, LOW);
      delayMicroseconds(2);

      // Sets the trigPin on HIGH state for 10 micro seconds
      digitalWrite(trigPin_1, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin_1, LOW);

      // Reads the echoPin, returns the sound wave travel time in microseconds
      duration_1 = pulseIn(echoPin_1, HIGH);

      // Calculating the distance
      distance_1= duration_1;
      // Prints the distance on the Serial Monitor
      Serial.print("Ultrasonic 1: ");
      Serial.println(distance_1);

      String data = String("?type=") + "1" + "&level="+ distance_1 + "&deviceID=" + 1;

      if (http.begin(uri + data)) 
      {  // HTTP

        int httpCode = http.GET();

        // httpCode will be negative on error
        if (httpCode > 0) {
          // HTTP header has been send and Server response header has been handled
          Serial.printf("[HTTP] GET... code: %d\n", httpCode);

          // file found at server
          if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
              String payload = http.getString();
              Serial.println(payload);
          }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();     
      delay(1000);
  }
 }

  if(type == 2){ 

       digitalWrite(trigPin_2, LOW);
       delayMicroseconds(2);

       // Sets the trigPin on HIGH state for 10 micro seconds
       digitalWrite(trigPin_2, HIGH);
       delayMicroseconds(10);
       digitalWrite(trigPin_2, LOW);

       // Reads the echoPin, returns the sound wave travel time in microseconds
       duration_2 = pulseIn(echoPin_2, HIGH);

       // Calculating the distance
       distance_2= duration_2*0.034/2;
       // Prints the distance on the Serial Monitor
       Serial.print("Ultrasonic 2: ");
       Serial.println(distance_2);

       String data = String("?type=") + "2" + "&level="+ distance_2 + "&deviceID=" + 1;

      if (http.begin(uri + data)) 
      {  // HTTP

        int httpCode = http.GET();

        // httpCode will be negative on error
        if (httpCode > 0) {
          // HTTP header has been send and Server response header has been handled
          Serial.printf("[HTTP] GET... code: %d\n", httpCode);

          // file found at server
          if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
            String payload = http.getString();
              Serial.println(payload);

          }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();     
      delay(1000);
   }
  }

  if(type == 3){


     digitalWrite(trigPin_3, LOW);
     delayMicroseconds(2);

     // Sets the trigPin on HIGH state for 10 micro seconds
     digitalWrite(trigPin_3, HIGH);
     delayMicroseconds(10);
     digitalWrite(trigPin_3, LOW);

     // Reads the echoPin, returns the sound wave travel time in microseconds
      duration_3 = pulseIn(echoPin_3, HIGH);

      // Calculating the distance
      distance_3= duration_3*0.034/2;
      // Prints the distance on the Serial Monitor
      Serial.print("Ultrasonic 3: ");
      Serial.println(distance_3);

      String data = String("?type=") + "3" + "&level="+ distance_3 + "&deviceID=" + 1;

      if (http.begin(uri + data)) 
      {  // HTTP

        int httpCode = http.GET();

        // httpCode will be negative on error
        if (httpCode > 0) {
          // HTTP header has been send and Server response header has been handled
          Serial.printf("[HTTP] GET... code: %d\n", httpCode);

          // file found at server
          if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
            String payload = http.getString();
            Serial.println(payload);
          }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();     
      delay(1000);
   }
  }

}

void loop()
{

  if ((WiFiMulti.run() == WL_CONNECTED)) 
   {
      Serial.print("[HTTP] begin...\n");
      ultrasonic(1);
      ultrasonic(2);
      ultrasonic(3);

    } else {
      Serial.printf("[HTTP} Unable to connect\n");
    }
  //.run();
}
#包括
#包括
#包括
#包括
#包括
ESP8266WiFiMulti WiFiMulti;
//定义管脚编号
常数int trigPin_1=D1;
常数int echoPin_1=D0;
常数int trigPin_2=D6;
常数int echoPin_2=D5;
常数int trigPin_3=D7;
常数int echoPin_3=D8;
字符串uri=”http://192.168.0.128/socketprogramming/client.php";
//定义变量
长时程_1;
int距离_1;
长时程_2;
int距离_2;
长时程_3;
int距离_3;
无线客户端;
HTTPClient-http;
无效设置()
{
序列号开始(115200);
//Serial.setDebugOutput(true);
//Serial.println();
对于(uint8_t=4;t>0;t--){
Serial.printf(“[设置]等待%d..\n”,t);
Serial.flush();
延迟(1000);
}
WiFi.模式(WiFi_STA);
WiFiMulti.addAP(“TP-LINK86ac”,“79043120”);
//wifiMulti.addAP(“辅助网络名称”,“传递到辅助网络”);
//wifiMulti.addAP(“三级网络名称”,“传递到三级网络”);
pinMode(trigPin_1,输出);//将trigPin设置为输出
pinMode(echoPin_1,输入);//将echoPin设置为输入
pinMode(trigPin_2,输出);//将trigPin设置为输出
pinMode(echoPin_2,输入);//将echoPin设置为输入
pinMode(trigPin_3,输出);//将trigPin设置为输出
pinMode(echoPin_3,输入);//将echoPin设置为输入
}
真空超声波(int型){
如果(type==1){
数字写入(trigPin_1,低电平);
延迟微秒(2);
//将trigPin设置为高状态10微秒
数字写入(trigPin_1,高);
延迟微秒(10);
数字写入(trigPin_1,低电平);
//读取echoPin,以微秒为单位返回声波传播时间
持续时间_1=脉冲强度(echoPin_1,高);
//计算距离
距离_1=持续时间_1;
//在串行监视器上打印距离
连续打印(“超声波1:”);
串行打印Ln(距离_1);
字符串数据=字符串(“?type=“)+”1“+”&level=“+distance_1+”&deviceID=“+1;
if(http.begin(uri+data))
{//HTTP
int httpCode=http.GET();
//httpCode在出现错误时为负值
如果(httpCode>0){
//HTTP标头已发送,服务器响应标头已处理
Serial.printf(“[HTTP]获取…代码:%d\n”,httpCode);
//在服务器上找到文件
如果(httpCode==HTTP_代码_确定| | httpCode==HTTP_代码_永久移动){
字符串有效负载=http.getString();
Serial.println(有效载荷);
}
}否则{
Serial.printf(“[HTTP]GET…失败,错误:%s\n”,HTTP.errorToString(httpCode.c_str());
}
http.end();
延迟(1000);
}
}
如果(type==2){
数字写入(trigPin_2,低电平);
延迟微秒(2);
//将trigPin设置为高状态10微秒
数字写入(trigPin_2,高);
延迟微秒(10);
数字写入(trigPin_2,低电平);
//读取echoPin,以微秒为单位返回声波传播时间
持续时间_2=脉冲强度(echoPin_2,高);
//计算距离
距离_2=持续时间_2*0.034/2;
//在串行监视器上打印距离
连续打印(“超声波2:”);
串行打印ln(距离_2);
字符串数据=字符串(“?type=”)+“2”+”和level=“+distance_2+”&deviceID=“+1;
if(http.begin(uri+data))
{//HTTP
int httpCode=http.GET();
//httpCode在出现错误时为负值
如果(httpCode>0){
//HTTP标头已发送,服务器响应标头已处理
Serial.printf(“[HTTP]获取…代码:%d\n”,httpCode);
//在服务器上找到文件
如果(httpCode==HTTP_代码_确定| | httpCode==HTTP_代码_永久移动){
字符串有效负载=http.getString();
Serial.println(有效载荷);
}
}否则{
Serial.printf(“[HTTP]GET…失败,错误:%s\n”,HTTP.errorToString(httpCode.c_str());
}
http.end();
延迟(1000);
}
}
如果(类型==3){
数字写入(trigPin_3,低电平);
延迟微秒(2);
//将trigPin设置为高状态10微秒
数字写入(trigPin_3,高);
延迟微秒(10);
数字写入(trigPin_3,低电平);
//读取echoPin,以微秒为单位返回声波传播时间
持续时间_3=脉冲强度(echoPin_3,高);
//计算距离
距离_3=持续时间_3*0.034/2;
//在串行监视器上打印距离
连续打印(“超声波3:”);
串行打印Ln(距离_3);
字符串数据=字符串(“?type=“)+”3“+”&level=“+distance_3+”&deviceID=“+1;
if(http.begin(uri+data))
{//HTTP
int httpCode=http.GET();
//httpCode在出现错误时为负值
如果(httpCode>0){
//HTTP标头已发送,服务器响应标头已处理
Serial.printf(“[HTTP]获取…代码:%d\n”,httpCode);
//在服务器上找到文件
如果(httpCode==HTTP_代码_确定| | httpCode==HTTP_代码_永久移动){
字符串有效负载=http.getString();
Serial.println(有效载荷);
}
}否则{
Serial.printf(“[HTTP]GET…失败,错误:%s\n”,HTTP.errorToString(httpCode.c_str());
}
http.end();
延迟(1000);
}
}
}
void循环()
{
如果((WiFiMulti.run()==WL_已连接))
{
Serial.print(“[HTTP]begin…\n”);
超声波(1);
超声(2);
超声(3);
}否则{
Serial.printf(“[HTTP}无法连接\n”);
}
  duration_1 = pulseIn(echoPin_1, HIGH);
  // Calculating the distance
  distance_1= duration_1*0.034/2;
  // Prints the distance on the Serial Monitor
  Serial.print("Ultrasonic 1: ");
  Serial.println(distance_1);