Measuring distance of an object with a supersonic sensor

Hi guys.
I have a supersonic sensor, which I want to use to measure the distance between it and an object, and turn on an LED based on the distance. But the LEDs aren’t doing their job.

int trigPin = 13;
int echoPin = 11;
int RedLED = 6;
int YellowLED = 5;
int GreenLED = 7;
float pingTime;
float SoundSpeed = 1310;
float targetDistance;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);       //Setting up pin modes

  pinMode(RedLED, OUTPUT);
  pinMode(YellowLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(100);              // Sending a pulse to the sensor to start and transmit and recieve
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigPin, LOW);


  pingTime = pulseIn(echoPin, HIGH);
  pingTime = pingTime / 1000000.0;
  pingTime = pingTime / 3600.0;

  targetDistance = SoundSpeed * pingTime;             //A few conversions for a better user interface
  targetDistance = targetDistance / 2;
  targetDistance = targetDistance * 100000;

  Serial.print("The current distance to target is ");
  Serial.print(targetDistance);
  Serial.println(" centimters");         //Print the distance
  Serial.println(" ");
  delay(2000);

  if (targetDistance <= 10) {
    digitalWrite(RedLED, HIGH); //If the target distance it equal to or less than 10 centimeters, flash the red LED
    delay(500);
    digitalWrite(RedLED, LOW);
    delay(500);
  }

  if (targetDistance <= 15 && targetDistance > 10) {
    digitalWrite(YellowLED, HIGH);          //If the target distance it equal to or less than 15 centimeters and larger than 10 centimeters, flash the yellow LED
    delay(500);
    digitalWrite(YellowLED, LOW);
    delay(500);
  }

  if (targetDistance > 15) {
    digitalWrite(GreenLED, HIGH);            //If the target distance it larger than 15 centimeters, flash the green LED
    delay(500);
    digitalWrite(GreenLED, LOW);
    delay(500);
  }
}

However, at less than 10 centimetres, it flashes yellow, between 10 and 15 it flashes green, larger than 15 and it flashes red.

Also, when measuring distances larger than about 20 centimetres, it adds a ‘3310 centimetres’, which is weird.

Anybody know how to fix these problems? Also, why is the sensor adding a crazy measurement?

Why doing all those redundant calculations?

  unsigned long duration = pulseIn(echoPin, HIGH);
  float distance = (duration/2) / 29.1;

Floats are single precision so you’re bound to lose accuracy the way you do your calculations. Start with this for your numbers.

Define your pin numbers as byte rather than int. You’re wasting memory here. You don’t have to put the speed of sound in a variable as it’s constant enough for normal atmospheric conditions.

Also your if statements can be simplified big time:

if (targetDistance <= 10) {
  // blink red
}
else if (targetDistance <= 15) {
  // blink yellow
}
else {
  // blink green
}