Boa noite pessoal,
estou com um problema aqui eu estou precisando ligar 2 sensores ultrasonicos pra medir a distancia de dois moveis e calcular suas velocidades.
com um sensor só eu consegui fazer tranquilamente porem com dois eu nao consegui fazer, eu tentei declarar outro sensor porem quanto utilizo a serial para ver as distancias as distancias estão exatamente iguais, o Arduíno aparentemente so reconhece a distancia do primeiro sensor e coloca ela no segundo sensor.
alguém uma ideia de como eu posso fazer isso ?
Abraço!!
Tags:
posta o codigo
Olá Gustavo.
Basta você ligar os pinos de Trigger dos sensores no mesmo pino do Arduino, e os pinos de Echo em pinos diferentes.
No código você manda o comando de ping, seleciona o pino correspondente ao sensor, e efetua a leitura das medidas.
Segue exemplo abaixo.:
/* Ping))) Sensor
This sketch reads a PING))) ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.
The circuit:
* +V connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* SIG connection of the PING))) attached to digital pin 7
http://www.arduino.cc/en/Tutorial/Ping
created 3 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
*/
// this constant won't change. It's the pin number
// of the sensor's output:
const int pingTrigger = 8;
const int Sensor_1 = 7;
const int Sensor_2 = 6;
const int Sensor_3 = 5;
void setup() {
pinMode(pingTrigger, OUTPUT);
pinMode(Sensor_1, INPUT);
pinMode(Sensor_2, INPUT);
pinMode(Sensor_3, INPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// SENSOR 1
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(pingTrigger, LOW);
delayMicroseconds(2);
digitalWrite(pingTrigger, HIGH);
delayMicroseconds(5);
digitalWrite(pingTrigger, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(Sensor_1, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.println("Sensor 1");
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
// SENSOR 2
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(pingTrigger, LOW);
delayMicroseconds(2);
digitalWrite(pingTrigger, HIGH);
delayMicroseconds(5);
digitalWrite(pingTrigger, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(Sensor_2, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.println("Sensor 2");
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
// SENSOR 3
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(pingTrigger, LOW);
delayMicroseconds(2);
digitalWrite(pingTrigger, HIGH);
delayMicroseconds(5);
digitalWrite(pingTrigger, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(Sensor_3, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.println("Sensor 3");
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Espero ter ajudado.
T++. ^^
boa tarde !
sua dica me ajudou muito!
porem, eu estou tendo dificultadas em calcular as velocidades dos carros com os valores que eu leio dos sensores. a parte de apresentar os valores no lcd esta funcionando muito bem.
segue o codigo :
const int pingTrigger = 8;
const int Sensor_1 = 7;
const int Sensor_2 = 6;
#include <LiquidCrystal.h> //Carrega a biblioteca LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
pinMode(pingTrigger, OUTPUT);
pinMode(Sensor_1, INPUT);
pinMode(Sensor_2, INPUT);
lcd.begin(16,2); //Inicializa LCD
lcd.clear(); //Limpa o LCD
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
long duration, cm;
//distancia dos carros
double x, velocidade, velocidade2, tempo2, tempoencontro,pontodeencontro, carro1,carro2, tempo; // define todas as variaveis como double
// SENSOR 1
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(pingTrigger, LOW);
delayMicroseconds(2);
digitalWrite(pingTrigger, HIGH);
delayMicroseconds(5);
digitalWrite(pingTrigger, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(Sensor_1, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.println("Sensor 1");
Serial.print(carro1);
Serial.print("cm");
Serial.println();
delay(100);
// apresenta a distancia dos carros na serial
Serial.print("Carro 1:");
Serial.print(cm);
// apresenta no LCD a distancia dos carros
lcd.setCursor(0,0);
lcd.print("Distancia 1:");
lcd.setCursor(10,0);
lcd.print(cm);
lcd.setCursor(14,0);
lcd.print("CM");
carro1 = cm;
// SENSOR 2
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(pingTrigger, LOW);
delayMicroseconds(2);
digitalWrite(pingTrigger, HIGH);
delayMicroseconds(5);
digitalWrite(pingTrigger, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(Sensor_2, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print("Carro 2 ");
Serial.println(cm);
// carro 2
lcd.setCursor(0,1);
lcd.print("Distancia 2:");
lcd.setCursor(10,1);
lcd.print(cm);
lcd.setCursor(14,1);
lcd.print("CM");
carro2 = cm;
if(cm >= 91){
//calcula e mostra na serial a velocidade dos carros
tempo2 = tempo /1000; // transforma de millisegundos para segundos
velocidade = carro1 / tempo2; // calcula a velocidade do carro 1
velocidade2 = carro2 / tempo2 ;// calcula a velocidade do carro 2
Serial.println(tempo2);
Serial.print(" velocidade carro 1: ");
Serial.print(velocidade);
Serial.print(" velocidade carro 2:");
Serial.print(velocidade2);
// CALCULA O PONTO DE ENCONTRO DOS CARROS E O APRESENTA NA LCD
tempoencontro = 1 /(velocidade2 + velocidade);
pontodeencontro = 0.04 + velocidade * tempoencontro; // calculo do local de encontro subustituindo os calores na primeira equação
Serial.print("encontro ocorreu");
Serial.print(pontodeencontro);
lcd.setCursor(0,0);
lcd.print("encontro foi em :");
lcd.setCursor(0,1);
lcd.print(pontodeencontro);
lcd.setCursor(5,1);
lcd.print("CM");
delay (1000000);
}
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Até mais,
abraço!
então as distancias vc ta conseguindo ler corretamente!? e vc colocou que, tempo2 = tempo /1000, mas onde vc mede a variavel tempo? não achei no codigo
Bom dia, como faço para ligar todos os pinos Trigger no mesmo pino do Arduino?
É igual está na figura da resposta acima, só ligar todos em paralelo em um único pino, quando vc usa o comando do trigger ele aciona todos os sensores aí é escolher qual vc quer ler.
oi agr como faço para fazer dois motores girarem igual de carro
Dois automóveis ?
Qual a distância entre os sensores e os automóveis ?
Qual a velocidade maxima desses automóveis ?
Lembre-se que esses sensores tem limitações de distância e tempo de medição.
http://labdegaragem.com/forum/topics/sensor-hc-sr04-ultrasom-como-f...
Bem-vindo a
Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)
© 2024 Criado por Marcelo Rodrigues. Ativado por