Go Down

Topic: programa para hacer funcionar 2 sensores ultrasonicos srf04 con arduino (Read 995 times) previous topic - next topic

marviiin

hice un programa en el cual incluyo dos sensores ultrasonicos, pero a la hora de correrlo solo me funciona uno, y los probe por separado y si funcionan los dos, espero y me puedan ayudar a corregir el programa, decirme en que esta mal,o decirme como se hace, o si me pudieran pasar ejemplos de otros programas que funcionen con dos o mas sensores se los agradeceria.
les adjunto el programa en archivo y en texto.
int led1=13;
int led2=8;
int pingPin = 7;
int pingPin2 = 6;
int value = 0; // Valor del pulsador
int value2 =0;

void setup() {
  // 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,duration2, cm,cm2;

  // 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:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, 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.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);


pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, 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.
  pinMode(pingPin2, INPUT);
  duration2 = pulseIn(pingPin2, HIGH);

  // convert the time into a distance

  cm = microsecondsToCentimeters(duration);
  cm2 = microsecondsToCentimeters(duration2);
  value = duration  / 29 / 2;
  value2 = duration2  / 29 / 2;


pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);

Serial.print(cm);
  Serial.print("cm");
  Serial.println();
delay(100);


Serial.print(cm2);
  Serial.print("cm2");
  Serial.println();
delay(100);


  if
( cm <=20 )
 
{digitalWrite (led1,HIGH);}
else
{digitalWrite (led2,LOW);}


if
(  value2 <= 20)
{digitalWrite (led2,HIGH);
}
else
{digitalWrite (led2,LOW);}
 


}






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;
}


Go Up