DigitalRead stuck at HIGH in if statement

In my program I have an ultrasonic sensor when active in a certain distance go into a while loop.

In the while loop there is a switch that needs to be flipped on in-order to reach an IF digitalRead statement

My problem is that I cannot get out of the IF statement because I can't change the DigitalRead value once it is inside the if statement will not change.

everything else seems to be working correctly

You've posted this in "Programming questions".
Have you forgotten something?

And here…

/*Equiment used 
 * dual-H bridge sensor + 2 motors
 * ultrasonic sensor
 * bullet sensor (set up like a 3 terminal switch) (one terminal is not used)
 * link for radar set up http://howtomechatronics.com/projects/arduino-radar-project/
 */





#define trigPin 2  //Ultrasonic sensor
#define echoPin 6  //Ultrasonic sensor
  
 
int buttonState = 0;         

void setup() {
  Serial.begin  (9600);
  pinMode(trigPin, OUTPUT);  
  pinMode(echoPin, INPUT); 
  pinMode(9, OUTPUT); //PWM pin
  pinMode(8, OUTPUT); //H Bridge
  pinMode(7, OUTPUT); //H Bridge
  pinMode(5, OUTPUT); //H Bridge
  pinMode(4, OUTPUT); //H Bridge
  pinMode(3, OUTPUT); // PWM pin
  pinMode(10, INPUT); //Switch

}

void loop() {
 int duration, distance; 

  
  
 digitalWrite(trigPin, HIGH);
 delay(0); 
 digitalWrite(trigPin, LOW);
 duration= pulseIn(echoPin, HIGH); 
 distance= (duration/2)/29.1; //convert to centimeters
  //http://www.cplusplus.com/reference/cstdio/sprintf/
 
 
 
     while (distance <= 100) {

        Serial.println(distance );
        Serial.println(" mm ");
       
       int buttonState = 0; 
       buttonState = digitalRead(10);  //read the switch 
  
            digitalWrite (buttonState , LOW);

        if(buttonState == HIGH){                     // <<<<<<<<<< trouble maker need help please >>>>>>
        Serial.println("Bullet in chamber     ");
        Serial.println(buttonState);
       
            
          digitalWrite(8, HIGH);  //turn motors on
          digitalWrite(7,LOW);    
          digitalWrite(5, HIGH);
          digitalWrite(4, LOW);  
          analogWrite(9 , 220);  //PWM SIGNAL
          analogWrite(3 , 220);  

                                   
                     digitalWrite(trigPin, HIGH); // to reread the distance 
                     delay(0); 
                     digitalWrite(trigPin, LOW);
                     
        }; //end if 1                              <<<<<<<<<<<<<<<<<<<<<<<<<<<<<end of the trouble 
                                                   // cannot change the buttonState value from high to LOW
                                                  

        if(buttonState == LOW){
                    digitalWrite(8 , LOW);  //turn off motors 
                    digitalWrite(7 ,LOW);
                    digitalWrite(5 , LOW);
                    digitalWrite(4 , LOW);
                    Serial.println("Bullet NOT in chamber" );
                    Serial.println(buttonState);
          }; //end if 2

                                break;
                 
           };//END distance while
           
            if(distance >= 100) {
     
                    Serial.println(" object is to far away    ");
                     digitalWrite(8 , LOW);
                    digitalWrite(7 ,LOW);
                    digitalWrite(5 , LOW);
                    digitalWrite(4 , LOW);
                              
            };
            
            
                             
}//end main

Thank you.

This is my first post.

So you read a pin:
buttonState = digitalRead(10); //read the switch

Then you write pin 0 or 1 with a LOW:

digitalWrite (buttonState , LOW);

Doesn't that then interfere with the following Serial action?

Here is a sample code I was using for reference.

const int buttonPin = 10;     // the number of the pushbutton pin
      
// variables will change:

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
   
  Serial.begin  (9600);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
                                                           // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
                                                          // check if the pushbutton is pressed.
                                                          // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
                                                           
    Serial.println(buttonState);
  } else {
    
    
  }
}

Yes, that code doesn’t use the state of an input to decide which of two pins that you wouldn’t normally want to write to, to write to.

Does that code write to buttonState ?
Yours does.

The advantage of using Pin or State in the name is that the name is supposed to remind you what the variable is for. State is the second argument to digitalWrite(). The pin number is the first argument. It does not make sense to use State in the name of the first argument.