Right now individually each of the ultrasonic sensors work, but when i try to run code with both of them, it is unable to do it.
Am I writing it wrong?
const int pingPin = 13;
const int inPin = 12;
const int pingPink = 11;
const int inPink = 10;
void setup() {
Serial.begin(9600);
}
void loop()
{
float duration, inches;
float velocity;
float durationk, inchesk;
pinMode(pingPin, OUTPUT);
pinMode(pingPink, OUTPUT);
digitalWrite(pingPin, LOW);
digitalWrite(pingPink, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
digitalWrite(pingPink, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
digitalWrite(pingPink, LOW);
pinMode(inPin, INPUT);
pinMode(inPink, INPUT);
duration = pulseIn(inPin, HIGH);
durationk = pulseIn(inPink, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
inchesk = microsecondsToInches(durationk);
int time = 0;
boolean timerOn = false;
boolean timerStart = true;
if(timerOn) {
time = time + 10;
}
if(timerStart && inches < 9) {
timerOn = true;
timerStart = false;
}
if(inchesk < 9) {
timerOn = false;
velocity = 6 / time / 12 * 1000;
Serial.print(velocity);
Serial.println(" feet/second");
}
Serial.println(inches);
Serial.println(inchesk);
Serial.println(time);
delay(10);
}
float microsecondsToInches(float 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.
return microseconds / 73.746 / 2;
}
long microsecondsToMillimeters(long microseconds)
{
// The speed of sound is 340.29 m/s or 29.3867 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 / 2.93867 / 2;
}
Thanks!