ultrasonic sensor for my project

hello i am a newbie here
i am having a hard time on how could i combine this two program codes i wrote
here's my code

First

[code][code]const int trigger=5;
const int echo=6;
const int motorPin=7;
long int duration, distanceInches, distanceCm;
int LimitCm = 100;

void setup()
{
  pinMode(motorPin, OUTPUT);
}
void loop()
{
  pinMode (trigger, OUTPUT);
  pinMode (echo, INPUT);
  digitalWrite(trigger, LOW);
  delayMicroseconds (2);
  digitalWrite(trigger,HIGH);
  delayMicroseconds(5);
  digitalWrite(trigger,LOW);
  
  pinMode(echo, INPUT);
  duration = pulseIn(echo,HIGH);
  
  distanceInches=microsecondsToInches(duration);
  distanceCm=microsecondsToCentimeters(duration);
  
  checkLimit();
  delay (100);
}
  void checkLimit()
  {
    if(distanceCm < LimitCm)
    {
      digitalWrite(motorPin,HIGH);
    }
    else
    {
      digitalWrite(motorPin,LOW);
    }
  }
  long microsecondsToInches (long microseconds)
  {
    return microseconds /74/2;
  }
  long microsecondsToCentimeters(long microseconds)
  {
    return microseconds /29/2;
  }

[/code][/code]

Second

const int trigger=5;
const int echo=6;
const int LedPin=7;
const int speaker=8;
const int LedAlert=4;
long int duration, distanceInches, distanceCm;
int LimitCm = 100;

void setup()
{
  pinMode(LedPin, OUTPUT);
  pinMode(LedAlert, OUTPUT);
  pinMode(speaker, OUTPUT);
}
void loop()
{
  digitalWrite(LedPin, HIGH);
  pinMode (trigger, OUTPUT);
  pinMode (echo, INPUT);
  digitalWrite(trigger, LOW);
  delayMicroseconds (2);
  digitalWrite(trigger,HIGH);
  delayMicroseconds(5);
  digitalWrite(trigger,LOW);
  
  pinMode(echo, INPUT);
  duration = pulseIn(echo,HIGH);
  
  distanceInches=microsecondsToInches(duration);
  distanceCm=microsecondsToCentimeters(duration);
  
  checkLimit();
  delay (100);
}
  void checkLimit()
  {
    if(distanceCm < LimitCm)
    {
      digitalWrite(LedAlert,HIGH);
      delayMicroseconds(2);
      digitalWrite(speaker,HIGH);
      delayMicroseconds(5);
      
   
    }
    else
    {
      digitalWrite(LedAlert,LOW);
      delayMicroseconds(2);
      digitalWrite(speaker,LOW);
      delayMicroseconds(5);

    }
  }
  long microsecondsToInches (long microseconds)
  {
    return microseconds /74/2;
  }
  long microsecondsToCentimeters(long microseconds)
  {
    return microseconds /29/2;
  }

I want to use two ultrasonic sensor
the first code uses vibrating motor as an alarm
while the second sensor uses a two LED and a buzzer/speaker a an alarm too..

this two codes works fine when i tested it with my sensor
but when i combined it in the loop and modifying the codes and pin designations it does not work fine..
SO i am looking for a help or tutorial on how could i make this two code be in one
thanks for those who would help..

Go back to the first post, click on "modify", highlight the first code block and click on the # icon on the editor's toolbar.
Repeat for the second code block, then click on "save"

sir could you please help me with this code..
i'll post my worked code combining the two..

const int trigger=5;
const int echo=6;
const int trig=8;
const int ech=9;
const int motorPin=7;
const int LedPin=3;
const int speaker=2;
const int LedAlert=4;
long int duration, distanceInches, distanceCm;
long int distance, distanceInch, distanceCms;
int LimitCm = 100;
int LimitCms = 100;

void setup()
{
  pinMode(motorPin, OUTPUT);
  pinMode(LedPin, OUTPUT);
  pinMode(LedAlert, OUTPUT);
  pinMode(speaker, OUTPUT);
}
void loop()
{
  {
  digitalWrite(LedPin, HIGH);
  }
  {
  pinMode (trigger, OUTPUT);
  pinMode (echo, INPUT);
  digitalWrite(trigger, LOW);
  delayMicroseconds (2);
  digitalWrite(trigger,HIGH);
  delayMicroseconds(5);
  digitalWrite(trigger,LOW);
  }
  {
  pinMode (trig, OUTPUT);
  pinMode (ech, INPUT);
  digitalWrite(trig, LOW);
  delayMicroseconds (2);
  digitalWrite(trig,HIGH);
  delayMicroseconds(5);
  digitalWrite(trig,LOW);
  }
  
  
  pinMode(echo, INPUT);
  duration = pulseIn(echo,HIGH);
  
  pinMode(ech,INPUT);
  distance=pulseIn(ech,HIGH);
  
  distanceInches=microsecondsToInches(duration);
  distanceCm=microsecondsToCentimeters(duration);
  
  distanceInch=microsecondsToInches(distance);
  distanceCms=microsecondsToCentimeters(distance);
  
  checkLimit();
  checkLimits();
  delay (100);
}
  void checkLimit()
  {
    if(distanceCm < LimitCm)
    {
      digitalWrite(motorPin,HIGH);
     
    }
    else
    {
      digitalWrite(motorPin,LOW);
  
    }
  }
  void checkLimits()
  {
    if(distanceCms < LimitCms)
    {
      digitalWrite(LedAlert,HIGH);
      delayMicroseconds(2);
      digitalWrite(speaker,HIGH);
      delayMicroseconds(5);
    }
    else
    {
      digitalWrite(LedAlert,LOW);
      delayMicroseconds(2);
      digitalWrite(speaker,LOW);
      delayMicroseconds(5);
    }
  }

  long microsecondsToInches (long microseconds)
  {
    return microseconds /74/2;
  }
  long microsecondsToCentimeters(long microseconds)
  {
    return microseconds /29/2;
  }
 
 the two led lights and the vibrating motor vibrates continuously.. 
 that's my problem and my speaker doesn't work
hoping that you could help me sir/mam
thanks in advance

Please read reply #2

AWOL:
Please read reply #2

sorry sir for the mistake i made..
and i didn't know if i am in the correct thread to ask questions regrading with my project..
but hopefully you could help me getting rid of my problem
thanks in advance

-Ok, factor the code for the Ping sensors into a single function, taking the pin number as a parameter.
This comes up on the forum every other week, so it shouldn't be too difficult to find.
Once you start reducing the volume of code, it is easier to see the stuff that really matters.

AWOL:
-Ok, factor the code for the Ping sensors into a single function.
This comes up on the forum every other week, so it shouldn't be too difficult to find.
Once you start reducing the volume of code, it is easier to see the stuff that really matters.

thanks sir..but i still have questions in my mind
i am just new with arduino and have a little background in c programming
i wish someone could make a sample code for me or a full instruction on how to do this because i really need this for my final requirement=(

again thanks a lot sir!!
i'll post questions and problems when i make my code..

  pinMode (trigger, OUTPUT);  // move to setup ()
  pinMode (echo, INPUT);                  // move to setup ()

unsigned long getRange (const int triggerPin, const int echoPin)
{
  digitalWrite(triggerPin, LOW);
  delayMicroseconds (2);
  digitalWrite(triggerPin,HIGH);
  delayMicroseconds(5);
  digitalWrite(triggerPin,LOW);
  
  return pulseIn(echoPin,HIGH);
}

thanks sir..
did i also need to put the other codes into set up..
the one that has no statements //
or i should leaved it with the loop()

Why don't you try stuff out, and come back when you've got specific questions?

sir thanks for the idea and after some searching in the internet
my problem was now solved..

here's my final sketch

const int trigger=5;
const int echo=6;
const int trig=8;
const int ech=9;
const int motorPin=7;
const int LedPin=3;
const int speaker=2;
const int LedAlert=4;
long int duration, distanceInches, distanceCm;
long int distance, distanceInch, distanceCms;
int LimitCm = 100;
int LimitCms = 100;

void setup()
{
  pinMode(motorPin, OUTPUT);
  pinMode(LedPin, OUTPUT);
  pinMode(LedAlert, OUTPUT);
  pinMode(speaker, OUTPUT);
}
void loop()
{
  {
  digitalWrite(LedPin, HIGH);
  }
  {
  pinMode (trigger, OUTPUT);
  pinMode (echo, INPUT);
  digitalWrite(trigger, LOW);
  delayMicroseconds (2);
  digitalWrite(trigger,HIGH);
  delayMicroseconds(5);
  digitalWrite(trigger,LOW);
   
  pinMode(echo, INPUT);
  duration = pulseIn(echo,HIGH);
  
  distanceInches=microsecondsToInches(duration);
  distanceCm=microsecondsToCentimeters(duration);
  
   checkLimit();
   delay (100);
  }
  {
  pinMode (trig, OUTPUT);
  pinMode (ech, INPUT);
  digitalWrite(trig, LOW);
  delayMicroseconds (2);
  digitalWrite(trig,HIGH);
  delayMicroseconds(5);
  digitalWrite(trig,LOW);
  
 
  pinMode(ech,INPUT);
  distance=pulseIn(ech,HIGH);
  
  distanceInch=microsecondsToInches(distance);
  distanceCms=microsecondsToCentimeters(distance);
  
  checkLimits();
  delay (100);
}
}
  void checkLimit()
  {
    if(distanceCm < LimitCm)
    {
      digitalWrite(motorPin,HIGH);
     
    }
    else
    {
      digitalWrite(motorPin,LOW);
  
    }
  }
  void checkLimits()
  {
    if(distanceCms < LimitCms)
    {
      digitalWrite(LedAlert,HIGH);
      delayMicroseconds(2);
      digitalWrite(speaker,HIGH);
      delayMicroseconds(5);
    }
    else
    {
      digitalWrite(LedAlert,LOW);
      delayMicroseconds(2);
      digitalWrite(speaker,LOW);
      delayMicroseconds(5);
    }
  }

  long microsecondsToInches (long microseconds)
  {
    return microseconds /74/2;
  }
  long microsecondsToCentimeters(long microseconds)
  {
    return microseconds /29/2;
  }
  {
  digitalWrite(LedPin, HIGH);
  }

What are these { and } for?

How does this:

  pinMode (trigger, OUTPUT);
  pinMode (echo, INPUT);
  digitalWrite(trigger, LOW);
  delayMicroseconds (2);
  digitalWrite(trigger,HIGH);
  delayMicroseconds(5);
  digitalWrite(trigger,LOW);
   
  pinMode(echo, INPUT);
  duration = pulseIn(echo,HIGH);

differ from:

  pinMode (trig, OUTPUT);
  pinMode (ech, INPUT);
  digitalWrite(trig, LOW);
  delayMicroseconds (2);
  digitalWrite(trig,HIGH);
  delayMicroseconds(5);
  digitalWrite(trig,LOW);
  
 
  pinMode(ech,INPUT);
  distance=pulseIn(ech,HIGH);

You were advised to create a function. You ignored that advice. Why?