0

现在我正在使用 2 个超声波传感器,但我发现它们在户外变得不稳定并给出随机读数.....虽然它们在房间内正常运行......我该如何解决这个问题???那是代码,似乎关闭的传感器总是在读取,我检查了接线,它很好,所以我真的不知道问题出在哪里??

    #define trig 2
    #define echo 3
    #define led 7
    #define relay 4
    #define trig2 10
    #define echo2 9
    #define led2 8

  long duration,distance,OnSensor,OffSensor;
void setup() {
  // put your setup code here, to run once:
  pinMode(echo,INPUT);
  pinMode(echo2,INPUT);
  Serial.begin(9600);
  pinMode(led,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(relay,OUTPUT);
  pinMode(trig,OUTPUT);
  pinMode(trig2,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(led,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(relay,LOW);
  SonarSensor(trig2, echo2);
  OffSensor = distance;
  delay(50);
  SonarSensor(trig, echo);
  OnSensor=distance;

  if(OnSensor<80&&OffSensor>=80){//sensor 1 activated
    digitalWrite(led,HIGH);
    digitalWrite(relay,HIGH);
    delay(150);
    digitalWrite(led,LOW);
    digitalWrite(relay,LOW);
    delay(1000);
    digitalWrite(led,HIGH);
    digitalWrite(relay,HIGH);
    delay(150);
    digitalWrite(led,LOW);
    digitalWrite(relay,LOW);

  }
  else if(OnSensor>80&&OffSensor>80){//No sensor activated
    digitalWrite(led,LOW);
    digitalWrite(relay,LOW);
  }
  else if(OnSensor>80&&OffSensor<80){//sensor2 activated
        digitalWrite(led2,HIGH);
        digitalWrite(relay,LOW);
        delay(5000);
  }
  else if(OnSensor<80&&OffSensor<80){//Both sensors activated
       digitalWrite(led2,HIGH);
       digitalWrite(led,HIGH);
       digitalWrite(relay,LOW);
       delay(3000);
  }
}
void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
}
4

1 回答 1

0

I think there are 2 problem here

  1. You use 2 sensor
  2. List item

Use use it outdoor

From my experience the ultra sonic wave can bouncing around object many time. depend on your setup, second sensor might read echo form first sensor. follow these step 1. Try to use 1 sensor per time to see if it error 2. If single sensor work you should add more delay after sensor reading 3. Plot the data to see how it look like you might need digital filter to improve reading For step 3 if you use sensor as On Off by threshold you can see my example code. This code work the same way as button de-bouncing so in need to trig continuously for 10 time to prevent false alarm (This code for single sensor)

#define  ULTRASONIC_TRIG_PIN  8
#define  ULTRASONIC_ECHO_PIN 7

#define RELAY_PIN 4
/**/
#define ULTRASONIC_INTERVAL_MS 100//50
#define ULTRASONIC_TRIG_COUNT_THRESH 10
#define ULTRASONIC_DISTACE_THRESH_CM 50
/**/
#define RELAY_TRIG__INTERVAL_MS 5000
#define RELAY_TRIG_LOGIC HIGH



void setup()


{

  pinMode (RELAY_PIN, OUTPUT);
  pinMode (ULTRASONIC_TRIG_PIN, OUTPUT);
  pinMode (ULTRASONIC_ECHO_PIN, INPUT);
  Serial.begin(115200);
}
void loop()
{
  static unsigned long ultrasonic_timer = millis();
  static unsigned long relay_trig_timer = millis();
  static float us_distance_cm = 0;
  static int us_trig_count = 0;
  static byte relay_state = !RELAY_TRIG_LOGIC; // initial value as off;
  if (millis() - ultrasonic_timer >= ULTRASONIC_INTERVAL_MS) {
    ultrasonic_timer += ULTRASONIC_INTERVAL_MS;
    us_distance_cm = getDistanceCM();

    if (us_distance_cm <= ULTRASONIC_DISTACE_THRESH_CM) {
      if (us_trig_count >= ULTRASONIC_TRIG_COUNT_THRESH) {
        digitalWrite(RELAY_PIN, RELAY_TRIG_LOGIC);
        relay_state = RELAY_TRIG_LOGIC;
        relay_trig_timer = millis();
      }
      else {
        us_trig_count++;
      }

    }
    else {
      us_trig_count = 0;

    }
    Serial.print("distance = ");
    Serial.print(us_distance_cm);
    Serial.print("cm, relay = ");
    Serial.print(relay_state);
    Serial.print(", count = ");
    Serial.println(us_trig_count);
  }
  if (relay_state == RELAY_TRIG_LOGIC) {
    if (millis() - relay_trig_timer > RELAY_TRIG__INTERVAL_MS) {
      relay_state = !RELAY_TRIG_LOGIC;
      digitalWrite(RELAY_PIN, !RELAY_TRIG_LOGIC);
    }
  }


}

float getDistanceCM() {
  digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
  unsigned long duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH, 50000);
  float distance = float(duration * 343) / 20000.0;
  delay(5);
  return distance;
}
于 2020-05-10T01:06:49.750 回答