HC-SR04 making a alarm

Hello i wanna make an alarm with my hcsr-04.
My problem is i dont know how i can programm it.

Here my sketch:

#include <NewPing.h>

#define TRIG_PIN 5
#define ECHO_PIN 11
#define MAX_DIST 400

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DIST); // NewPing initialisieren

void setup() {
Serial.begin(9600);
}

void loop() {
delay(50);
unsigned int distanceCM = sonar.ping_cm();
Serial.print(distanceCM);
Serial.println(“cm”);
}

Now i just can read how many cm my object is away.
But i want that if the number is under 5cm an led turns on.

can someone help me?

if (distanceCM < 5)
{
  //code here to turn LED on
}
else
{
  //code here to turn LED off
}

Read this tutorial to learn “Conditional Statements“ (If … then … else…).

#define TRIG_PIN 5
#define ECHO_PIN 11
#define BUILTIN_LED 2
 
void setup() {
  Serial.begin(9600);

  pinMode(ECHO_PIN, INPUT);
  pinMode(TRIG_PIN, OUTPUT);

  digitalWrite(BUILTIN_LED, HIGH);
  pinMode(BUILTIN_LED, OUTPUT);
}
 
void loop() {

 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(5);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
 
  duration = pulseIn(ECHO_PIN, HIGH);
  cm = (duration/2) / 29.1;     
  
  Serial.print(cm);
  Serial.println(" cm");

 if (cm <= 5)
{
    digitalWrite(BUILTIN_LED, LOW); 
    Serial.println("LED ON");
}
else
{
    digitalWrite(BUILTIN_LED, HIGH); 
    Serial.println("LED OFF");
}

Hope this could help…