need help with backup sensor

im attempting to make a backup sensor to help with hooking up my truck and trailer because im tired of getting out the the truck 5 and 6 times to do so. the idea is to use 2 ping sensors on the bumper of the truck and display the distance to trailer on 4 7 segment lcd's on the dash. 2 groups of 2 7segment displays, one for each ping. then when it gets to a certain point, using a led or the 7 segment displays to tell me when the trailer is lined up correctly. i have some of the code worked out but im not sure how to go about taking the range from the ping sensors and displaying it on the 7 segment displays. any help or ideas would be greatly appreciated. ill attach the portion of code i have written so far as well as a rough layout.

BackUpSensorNotFinished.ino (2.62 KB)

backupsensor layout.pdf (22.5 KB)

What is the difference between this code:

  pinMode(pingLeft, OUTPUT);
  digitalWrite(pingLeft, LOW);
  delayMicroseconds(2);
  digitalWrite(pingLeft, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingLeft, LOW);
  
  pinMode(pingLeft,INPUT);
  durationLeft = pulseIn(pingLeft,HIGH);

and this code:

 pinMode(pingRight, OUTPUT);
  digitalWrite(pingRight, LOW);
  delayMicroseconds(2);
  digitalWrite(pingRight, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingRight, LOW);
  
  pinMode(pingRight,INPUT);
  durationRight = pulseIn(pingRight,HIGH);

The pin number and where the data is stored. This should be moved into a function:
durationLeft = pingTime(pingLeft);
durationRight = pingTime(pingRight);

  distanceInchesLeft = microsecondsToInches(durationLeft);

The distanceInchesLeft variable is long. Why? If the truck is more than 65536 inches (5,460 feet), I don't think you are in any danger of hitting it. Even an unsigned int is oversize. Even a signed int can hold a larger value than can be displayed on a 4 digit display.

Have you successfully displayed anything on the displays? There is nothing in your code to indicate that you have.

Here is some code I use to display data on a 4 digit, 7 segment display:

/*
 6-13-2011
 Spark Fun Electronics 2011
 Nathan Seidle
 
 This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
 
 4 digit 7 segment display:
 http://www.sparkfun.com/products/9483
 Datasheet: 
 http://www.sparkfun.com/datasheets/Components/LED/7-Segment/YSD-439AR6B-35.pdf

 This is an example of how to drive a 7 segment LED display from an ATmega without the use of current limiting resistors.
 This technique is very common but requires some knowledge of electronics - you do run the risk of dumping too 
 much current through the segments and burning out parts of the display. If you use the stock code you should be ok, but 
 be careful editing the brightness values.
 
 This code should work with all colors (red, blue, yellow, green) but the brightness will vary from one color to the next
 because the forward voltage drop of each color is different. This code was written and calibrated for the red color.

 This code will work with most Arduinos but you may want to re-route some of the pins.

 7 segments
 4 digits
 1 colon
 =
 12 pins required for full control 
 
 */

int digit1 = 11; //PWM Display pin 1
int digit2 = 10; //PWM Display pin 2
int digit3 = 9; //PWM Display pin 6
int digit4 = 6; //PWM Display pin 8

//Pin mapping from Arduino to the ATmega DIP28 if you need it
//http://www.arduino.cc/en/Hacking/PinMapping
int segA = A1; //Display pin 14
int segB = 3; //Display pin 16
int segC = 4; //Display pin 13
int segD = 5; //Display pin 3
int segE = A0; //Display pin 5
int segF = 7; //Display pin 11
int segG = 8; //Display pin 15

#define DIGIT_ON  LOW
#define DIGIT_OFF  HIGH

#define SEGMENT_ON  HIGH
#define SEGMENT_OFF LOW

void setup()
{
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);

  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);
  
  pinMode(13, OUTPUT);
  
  Serial.begin(57600);
}

void loop()
{
  for(int i=0; i<10000; i++)
  {
    displayNumber(i);
  }
}

//Display brightness
//Each digit is on for a certain amount of microseconds
//Then it is off until we have reached a total of 20ms for the function call
//Let's assume each digit is on for 1000us
//If each digit is on for 1ms, there are 4 digits, so the display is off for 16ms.
//That's a ratio of 1ms to 16ms or 6.25% on time (PWM).
//Let's define a variable called brightness that varies from:
//5000 blindingly bright (15.7mA current draw per digit)
//2000 shockingly bright (11.4mA current draw per digit)
//1000 pretty bright (5.9mA)
//500 normal (3mA)
//200 dim but readable (1.4mA)
//50 dim but readable (0.56mA)
//5 dim but readable (0.31mA)
//1 dim but readable in dark (0.28mA)

#define DISPLAY_BRIGHTNESS  500

void displayNumber(int toDisplay)
{
  long beginTime = millis();

  for(int digit = 4 ; digit > 0 ; digit--)
  {
    //Turn on a digit for a short amount of time
    switch(digit)
    {
      case 1:
        analogWrite(digit1, DIGIT_ON);
        break;
      case 2:
        digitalWrite(digit2, DIGIT_ON);
        break;
      case 3:
        digitalWrite(digit3, DIGIT_ON);
        break;
      case 4:
        digitalWrite(digit4, DIGIT_ON);
        break;
    }

    //Turn on the right segments for this digit
    lightNumber(toDisplay % 10);
    toDisplay /= 10;

    delayMicroseconds(DISPLAY_BRIGHTNESS); //Display this digit for a fraction of a second (between 1us and 5000us, 500 is pretty good)

    //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);
  }

  while((millis() - beginTime) < 10); //Wait for 20ms to pass before we paint the display again
}

//Given a number, turns on those segments
//If number == 10, then turn off number
void lightNumber(int numberToDisplay)
{
  switch (numberToDisplay){
    case 0:
      digitalWrite(segA, SEGMENT_ON);
      digitalWrite(segB, SEGMENT_ON);
      digitalWrite(segC, SEGMENT_ON);
      digitalWrite(segD, SEGMENT_ON);
      digitalWrite(segE, SEGMENT_ON);
      digitalWrite(segF, SEGMENT_ON);
      digitalWrite(segG, SEGMENT_OFF);
      break;

  case 1:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 2:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 3:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 4:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 5:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 6:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 7:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 8:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 9:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 10:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;
  }
}

The displays use up a lot of pins. Unless you have a Mega, you will run out of pins trying to connect two of them.

i have been able to display something although it was fairly useless, it did tell me that i have everything wired up properly. the 2 7 segment displays that i have set up so far are both being fed by 74hc164 shift registers. im rather new to the whole programming thing and im not entirely sure what im doing, so thanks for the comments about the code. any idea as to how i might go about taking the info returned form the ping sensors and displaying it in inches on 2 7 segment displays?

any idea as to how i might go about taking the info returned form the ping sensors and displaying it in inches on 2 7 segment displays?

The code I posted earlier can be used to display any 4 digit number on one seven segment display, wired directly. If you are using a shift register, that makes things more difficult. There are examples on the playground about how to use shift registers. You'll need to figure out what data to shift out to make a 1, a 2, a 3, etc. appear.

i was able to write some code to display which ever number i want and in far less code than the example you posted but thanks for the example, i was able to learn quite a bit about it. my major hurdle now is trying to figure out how to take the input from the ping sensors and translating it into which number to display on which display. like taking 48 inches and displaying '4' on the left 7 segment and '8' on the right one, as well as the displays constantly updating with the current range. i guess the biggest problem is not knowing how to tie the 2 parts together

my major hurdle now is trying to figure out how to take the input from the ping sensors and translating it into which number to display on which display. like taking 48 inches and displaying '4' on the left 7 segment and '8' on the right one

48 / 10 = 4
48 % 10 = 8

OK, so, now you know what to display on each display.

as well as the displays constantly updating with the current range

Call whatever function you develop to display a digit on a display after each call to read the distance.

distLeft = pingDist(pingLeft);
int leftTen = distLeft / 10;
int leftOne = distLeft % 10;
ShowLeftTenDigit(leftTen);
ShowLeftOneDigit(leftOne);

Repeat for the right sensor.