How do I turn on a buzzer and keep it on when a certain trigger is met

I am currently working on a security alarm that uses a buzzer, ultrasonic sensor, LCD screen and a Keypad. I am trying to trigger the buzzer whenever the ultrasonic sensor reads a value under 11cm. Currently the buzzer only goes off while it reads a value under 11cm and I want it to go off and stay off when it reads one value under 11cm and turn off when the right code is entered in the keypad, and I do not know how to do this. Also my LCD screen is displaying strange characters whenever I enter a number on the keypad, I double checked the wiring and even used different pins for it. I am fairly new to arduino and do not have too much knowledge on it. I am using a mega 2560 board. Any help would be much appreciated!

#include <LiquidCrystal.h>
#include <NewPing.h>
#include <Keypad.h> 
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>

#define trig 11
#define echo 12
#define maxdist 400
#define buzzer 52 

const byte ROWS = 4;
const byte COLS = 4;

  char keys[ROWS][COLS] = 

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 

const String password = "5572";
String Input_Password;

LiquidCrystal lcd(50, 48, 46, 44, 42, 40);  

int cm;
NewPing usr(trig, echo, maxdist); 

void setup() {
  // put your setup code here, to run once:

lcd.begin(16, 2); 
pinMode (trig, OUTPUT);
pinMode (echo, INPUT); 
pinMode(buzzer, OUTPUT);
lcd.print("Enter Pin:"); 


void loop() {
  // put your main code here, to run repeatedly:
char key = keypad.getKey();

if (key)

    if(key == '*') {
        Input_Password = "";// clear input password
        lcd.print("Enter Pin:");
    else if(key == '#') {
        if(password == Input_Password) {
        else {
        Input_Password = ""; // clear input password
    else {
        Input_Password += key; // append new character to input password string

cm = usr.ping_cm(); 
delay (100);
if (cm > 11)
  //digitalWrite (buzzer, HIGH);
  digitalWrite (buzzer, HIGH);
else if (cm < 11) 
  //digitalWrite (buzzer, LOW);
  digitalWrite (buzzer, LOW); 


Did you write and test any sketches for the individual parts of your project, for example, the display that is giving you problems?

I have not, mostly because when I want to display something on the LCD, it is fine, but when I input something after I run the code it glitches out, and I do not know how to write code to test that.

First, let's simplify some things.
IF you already know that cm is NOT greater than 11, then the only other possibility is it is less than 11, so the extra "if" serves no purpose. Just include the "else" in your code.

If you want to remember that the value was less than 11, then you need to code something that remembers it for you. That is where "boolean" is used. Set it initially to false at the start of your code. Then in your test where you discover that cm is less than 11, set your boolean to true.
Now you can use the boolean anywhere in your code that you need to know if cm was ever less than 11.
Good luck,

You could echo it back in serial debug statements.