0

I am trying to get values for two ping sensors, but am not getting any returns, can someone help me edit my code?

Code

void setup()
{
  Serial.begin(9600); 
  pingRange(2);
  pingRange(3);
}

void loop(){
}

  long pingRange (int pingPin)

{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  return microsecondsToCentimeters(pulseIn(pingPin, HIGH));
}

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

There's one part of your code conspicuous by its absence:

pingRange(2);
pingRange(3);

You're calling the functions, but you're not doing anything with the returned value. I think you meant to print that value to the serial interface...

Serial.println(pingRange(2));
Serial.println(pingRange(3));
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.