Hi there
Im really new with all this, Im battling to get a reading from my pingpin2, I have swapped my sensors around they both work on pingpin1 but cant get both of then to work together.
This is my code below any advice would be much appreciated
Thanks in advance.
#include <Servo.h>
Servo myservo; // create servo object named myservo to control a servo/ Right servo
Servo myservo1; //create servo object named myservo1 to control a servo/ Left servo
int pos = 0;
const int pingPin = 7;
const int pingPin1 = 8;
const int pingPin2 = 13;
void setup() {
// initialize serial communication:
Serial.begin(9600);
myservo.attach(11); // Right servo
myservo1.attach(10); //Left servo
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
long duration1, inches1, cm1;
// 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);
pinMode(pingPin1, OUTPUT);
digitalWrite(pingPin1, LOW);
delayMicroseconds(2);
digitalWrite(pingPin1, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin1, LOW);
pinMode(pingPin1, INPUT);
duration = pulseIn(pingPin1, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
pinMode(pingPin2, OUTPUT);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin2, LOW);
pinMode(pingPin2, INPUT);
duration1 = pulseIn(pingPin2, HIGH);
inches1 = microsecondsToInches(duration1);
cm1 = microsecondsToCentimeters(duration1);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
Serial.print(inches1);
Serial.print("in1, ");
Serial.print(cm1);
Serial.print("cm1");
Serial.println();
delay(500);
}
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;
}