Este sensor não funciona se o shield Wiznet estiver plugado, mesmo que nenhuma biblioteca seja ativada.
sketch de teste
Requer Shield com keypad e display 1602
Exemplo testado com Arduino 1.05
// Jefferson Ryan - Automalabs.com.br // Baseado em script de: luckylarry.co.uk // Este sketech também demonstra o uso de Running Average. #include <LiquidCrystal.h> //Esta linha é específica para o display do shield com keypad //se seu display for diferente você pode precisar mudar LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // variables to take x number of readings and then average them // to remove the jitter/noise from the SRF05 sonar readings const int qtdeLeituras = 10; // number of readings to take/ items in the array int leituras[qtdeLeituras]; // stores the distance readings in an array int indice = 0; // arrayIndex of the current item in the array int total = 0; // stores the cumlative total int distanciaMedia = 0; // stores the average value // setup pins and variables for SRF05 sonar device int echoPin = 2; // SRF05 echo pin (digital 2) int triggerPin = 3; // SRF05 trigger pin (digital 3) unsigned long tempoPulso = 0; // stores the pulse in Micro Seconds unsigned long distancia = 0; // variable for storing the distance (cm) void setup() { lcd.begin(16, 2); lcd.clear(); pinMode(triggerPin, OUTPUT); // set init pin 3 as output pinMode(echoPin, INPUT); // set echo pin 2 as input // create array loop to iterate over every item in the array for (int estaLeitura = 0; estaLeitura < qtdeLeituras; estaLeitura++) { leituras[estaLeitura] = 0; } } void loop() { digitalWrite(triggerPin, HIGH); // send 10 microsecond pulse delayMicroseconds(10); // wait 10 microseconds before turning off digitalWrite(triggerPin, LOW); // stop sending the pulse tempoPulso = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low distancia = tempoPulso/58; // Distance = pulse time / 58 to convert to cm. total= total - leituras[indice]; // subtract the last distance leituras[indice] = distancia; // add distance reading to array total= total + leituras[indice]; // add the reading to the total indice = indice + 1; // go to the next item in the array // Ao chegar ao fim da matriz, voltamos a preencher do inicio if (indice >= qtdeLeituras) { indice = 0; } distanciaMedia = total / qtdeLeituras; // calculate the average distance String myString = String(distanciaMedia, DEC); myString +=" cm"; char charBuf[10]; //apago o conteúdo do array. Sem isso as leituras ficam completamente malucas, //porque "toCharArray" não faz isso. memset(charBuf, 0, sizeof(charBuf)); myString.toCharArray(charBuf, 10); lcd.clear(); lcd.setCursor(1, 0); lcd.print(charBuf); // Serial.println(averageDistance, DEC); /*Se o delay for muito baixo (10, por exemplo), alguns exemplares sensores irão apresentar alta instabilidade a curtas distâncias */ delay(50); }
Leave a Comment