HC SR 04 and LCD keypad shield with Arduino

Sir , I am using the following code for distance measurement using HCSR 04 ultrasonic sensor, interfaced with Arduino Uno + LCD key pad shield,

I want to make an alarm if distance is less than say 25 cm

The alarm circuit can be driven by 5V

please give an idea to modify code , any of the Arduino pin can be configerd as "High" if distance is<=25cm

My code is as follows:-

int TrigPin = 5;
int EchoPin = 6;
void setup()
{
Serial.begin(9600);
pinMode(TrigPin,OUTPUT);
pinMode(EchoPin,INPUT);
}
void loop()
{
int distance,duration;
int cd;
digitalWrite(TrigPin,HIGH);//TrigPin prepare high of more than 10us
delayMicroseconds(11);
digitalWrite(TrigPin,LOW);

duration = pulseIn(EchoPin, HIGH);//EchoPin received high start counting until the receiver to the low,return to the count value
duration =duration/29/2;//Calculating the distance cm

// The speed of s ound is 340 m/s or 29 microseconds per centimeter.
// cd=duration/10;
Serial.write(byte(0X01));
Serial.write(byte(0X80));
Serial.print("Distance=");
Serial.print(duration);
Serial.print("cm");
delay(1000);
}

Change duration to long instead of int otherwise it might overflow and declare a pin as output check the distance and set it HIGH if your reached your limit
What is the weird stuff you send out on Serial at the end? Is that needed for the shield?
Check syntax 0X not the proper convention to declare constant hexadecimal values

See comments in code

And indent your code correctly for readability and use code tags to post code please

const byte trigPin = 5;
const byte echoPin = 6;
const byte alertPin = 13;

void setup()
{
    Serial.begin(9600);  // why do you want 9600 here? Can't that be faster? Where are you connected? 
    pinMode(trigPin,OUTPUT);
    pinMode(echoPin,INPUT);
    pinMode(alertPin, OUTPUT);
}

void loop()
{
    long distance,duration;  // don't declare as int or duration might/will overflow

    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin,HIGH);//TrigPin prepare high of more than 10us
    delayMicroseconds(11);
    digitalWrite(trigPin,LOW);

    duration = pulseIn(echoPin, HIGH);// echoPin received high start counting until the receiver to the low,return to the count value
    distance = duration/29/2;//Calculating the distance cm. Careful on integer rounding 
    // The speed of sound is 340 m/s or 29 microseconds per centimeter.

    digitalWrite(alertPin, (distance <25)?HIGH:LOW); // turn on or off the alert based on distance 

    Serial.write(byte(0x01)); // what was that capital X?? Why are you sending that 1 out?
    Serial.write(byte(0x80)); // 0x01 is "Start of Heading" and 0x80 is not standard ASCII would be euro sign in ISO Latin 
    Serial.print("Distance="); 
    Serial.print(distance); // use the right variables 
    Serial.print("cm");
    delay(1000); // that's a bad idea you can bump into something within 1 second depending on speed
}

Sir, I will check and replay back .
Thanking you very much

Sir, It is working fine as I expected, Thank you very much to solve my problem

VINOD

I am attaching photograph of my arrangement. Red LED become "on" as distance is less than 25 cm

Images attached