Professores, boa tarde.

estou brincando com o hc-sr04 fazendo um "helping eyes" que eu achei no "instructables.com", a minha questão é que eu tenho percebido muitas leituras falsas no sensor, dá pra ver no monitor serial que ele faz leituras estranhas mesmo que não haja qualquer objeto na frente do sensor, o codigo parece legal, o cara que desenvolveu teve a doçura de fazer alguns samples do ping antes de validar como leitura... como ainda estou engatinhando trouxe aos senhores essa questão.

 então senhores caso tenham paciência, o que os senhores me sugerem fazer para diminuir as leituras falsas?

segue o código.
 
#include <NewPing.h>  // This library helps makes our readings more accurate
 
 
const int TRIGGER_PIN = 5;
const int ECHO_PIN  =  6;
const int ledPin =  9;
const int motor = 10;
const int buzzer =  11;
 
const int MAX_DISTANCE = 300; // Set maximum distance which can be measured to 300cm
const int SAMPLE_INTERVAL = 25; // Take a sample reading every 25milliseconds
const int PING_ITERATIONS = 3; // Take 3 readings before returning the average of the 3
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);//Create a new instance of the object
 
int systemState = LOW;  //Make sure everything is off
long previousMillis = 0;
long interval;  
 
void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);      
  pinMode(buzzer, OUTPUT);  
  pinMode(motor, OUTPUT);
  
}
void loop() {
  float d1 = getDistance();    // call the function which measures the distance
 
/* Map the distance measured to the number of milliseconds that LED, buzzer and motor
should stay on or off. VERY IMPORTANT! */
  interval = map(d1, 0, 250, 0, 2000);
  if(d1>0 && d1<=250) //Only do something if distance measured falls within this range
  {
      changeStateWithoutDelay(interval); //Call function to turn LED, Buzzer or motor on/off
  }
  else{
    digitalWrite(ledPin, LOW);
     digitalWrite(buzzer, LOW);
      digitalWrite(motor, LOW);
  }
 
  Serial.print(d1); Serial.print("cm    \t");
  //delay(100); //Uncomment this line to make the Serial monitor print out stuff more slowly
  Serial.print(interval); Serial.print("milliseconds    \t\n");
  Serial.println();
}
 
//Function which measures distance
float getDistance() {
  float t = sonar.ping_median(PING_ITERATIONS);                 // microseconds
  float d = sonar.convert_cm(t);          // centimeters
  return d;
}
 
// Function which turns LED,buzzer and motor on or off based on how far sensor is from the obstacle
void changeStateWithoutDelay(int interval)
{
  
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    // save the last time you turned everything off
    previousMillis = currentMillis;  
 
    // if the LED, buzzer and motor is off turn it on and vice-versa:
    if (systemState == LOW)
      systemState = HIGH;
    else
      systemState = LOW;
 
    // set the LED, buzzer and motor with the systemState of the variable:
    digitalWrite(ledPin, systemState);
    digitalWrite(buzzer, systemState);
    digitalWrite(motor, systemState);
  }
}

Exibições: 306

Responder esta

Respostas a este tópico

Faça varias medidas e depois calcule a média . 

Dessa forma, uma medida inválida, será desprezada. 

Calcule Mean average 

http://playground.arduino.cc/Main/Average

https://www.vocabulary.com/articles/chooseyourwords/mean-median-ave...

Legal professor, obrigado por responder. salvo engano esse código faz 3 medidas conforme:

"const int PING_ITERATIONS = 3; // Take 3 readings before returning the average of the 3"

vou aumentar para 8 porque eu gosto desse numero :) e testar. muitíssimo obrigado pela resposta.

Isso mesmo , parabéns aluno . KKKKK

professor.... funcionou. hehehe. ficou show. olha ai a fotinho 

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço