Shutting off DC motor with ultra sonic sensor NEED HELP

Hi all,

I am trying to figure out the code for my set up. I have a keypad, lcd, DC motor, and ultrasonic sensor all hooked up to a mega arduino board. After entering a code into the keypad, the lcd displays asking you to make a selection. After choosing the selection, the motor needs to spin and remain spinning until the ultra sonic sensor detects that there is an obstruction, where at that point the motor will shut off. Everything works except the fact that the motor needs to keep spinning until the obstruction is detected. I’ve written another code where it’s just the motor and the sensor. Everything works correctly and the motor shuts off when distance is less than a set number. But as soon as I put it in the bigger code utilizing the keypad and the lcd, it doesn’t work. It just runs the motor for 1 sec and then goes straight to the ending the program. PLEASE NEED HELP

Code below

#include <Keypad.h>
#include <Password.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Servo.h>

String newPasswordString; //hold the new password
char newPassword[6]; //charater string of newPasswordString

//initialize password to 1234
//you can use password.set(newPassword) to overwrite it
Password password = Password( “1234” );

byte maxPasswordLength = 4;
byte currentPasswordLength = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Four columns

//Define the keymap
char keys[ROWS][COLS] = {
{’#’,‘0’,’*’},
{‘9’,‘8’,‘7’},
{‘6’,‘5’,‘4’},
{‘3’,‘2’,‘1’},
};

//Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to row pinouts

// Connect keypad COL0, COL1, COL2 and COL3 to these Arduino pins.
byte colPins[COLS] = {8, 7, 6}; //connect to column pinouts

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

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int flag=0;
int flag1=1;
int motor=38;
int trigPin=10;
int echoPin=11;
long duration;
int distance;

void setup(){
flag=0; //to indicate whether the password is right
lcd.begin(16,2);
pinMode(motor,OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop(){

lcd.setCursor(0,0);
lcd.print(“welcome!”);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Enter your”);
lcd.setCursor(0,1);
lcd.print(“Policy Number:”);
delay(2000);
lcd.clear();

while (currentPasswordLength<maxPasswordLength){
char key = keypad.getKey();

if (key != NO_KEY){ //when there is an input from the keypad
delay(60);

lcd.print(key);
currentPasswordLength++; //it indicates the length of the input
password.append(key); //add the input key to the input password
}
if (currentPasswordLength == maxPasswordLength) { //if the input password match the actual password length
if (password.evaluate()){ //password.evaluate compates the password and the input

lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Choose Your”);
lcd.setCursor(0,1);
lcd.print(“Symptoms:”);
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("* Flu");
lcd.setCursor(0,1);
lcd.print("# Headache");
flag=1; //change the flag to 1, which means the password was right

}

else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Invalid Policy #”);
lcd.setCursor(0,1);
lcd.print(“Enter Again”);
currentPasswordLength=0;
flag=0;
password.reset();
delay(1000);
lcd.clear();
}
}

while (flag==1){
int n=0;
char k=keypad.getKey();
if (k != NO_KEY){
lcd.clear();
lcd.print(k);
delay(60);

if (k == ‘*’){
lcd.clear();

lcd.setCursor(0,0);
lcd.print(“dispensing…”);

while (distance>5){
analogWrite(motor,255);
check();
delay(50);
}

analogWrite(motor,0);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Thank You!”);
currentPasswordLength=0;
flag=0;
password.reset();
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“welcome!”);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Enter your”);
lcd.setCursor(0,1);
lcd.print(“Policy Number:”);
delay(2000);
lcd.clear();
}
else if (k == ‘#’){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“dispensing…”);
delay(1000);

analogWrite(motor,255);
while (n <1){
digitalWrite(trigPin, LOW);
delayMicroseconds(50);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(50);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
int distance= duration*0.034/2;
delay(500);

if ( distance < 5) {

analogWrite(motor,0);

n=n+1;
}
}

lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Thank You!”);
currentPasswordLength=0;
flag=0;
password.reset();
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“welcome!”);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Enter your”);
lcd.setCursor(0,1);
lcd.print(“Policy Number:”);
delay(2000);
lcd.clear();

}
else {
lcd.print(" Invalid!");
currentPasswordLength=0;
flag=0;
password.reset();
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“welcome!”);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Enter your”);
lcd.setCursor(0,1);
lcd.print(“Policy Number:”);
delay(2000);
lcd.clear();
}
}
}
}
}

void check(){

int distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(8);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;

}