Arduino limit switch robot problem

Hello I am making a robot for a project and so far I have
-two gear motors
-tb6612fng driver
-arduino uno
-one limit switch

So far I can get it to go forward but I can’t get it to stop when it hits a wall, so I used a if statement
like this:

 if (buttonState = HIGH)
  {
     move(1, 255, 1); //motor 1, full speed, left
     move(2, 255, 2); //motor 2, full speed, right
     delay(5000);
     stop();
  }
   else {
    // turn LED off:
    stop(); 
  }

and my whole code is:

int STBY = 10; //standby

//Motor A
int PWMA = 3; //Speed control 
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction

//Motor B
int PWMB = 5; //Speed control
int BIN1 = 11; //Direction
int BIN2 = 12; //Direction


const int button = 2;

int buttonState = 0;


void setup(){
  pinMode(STBY, OUTPUT);

  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);

  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  
  pinMode(button, INPUT);
  
}

void loop(){
  

  
buttonState = digitalRead(button);

  if (buttonState = HIGH)
  {
     move(1, 255, 1); //motor 1, full speed, left
     move(2, 255, 2); //motor 2, full speed, right
     delay(5000);
     stop();
  }
   else {
    // turn LED off:
    stop(); 
  }
 

  
  stop();
  
}


void move(int motor, int speed, int direction){
//Move specific motor at speed and direction
//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise

  digitalWrite(STBY, HIGH); //disable standby

  boolean inPin1 = LOW;
  boolean inPin2 = HIGH;

  if(direction == 1){
    inPin1 = HIGH;
    inPin2 = LOW;
  }

  if(motor == 1){
    digitalWrite(AIN1, inPin1);
    digitalWrite(AIN2, inPin2);
    analogWrite(PWMA, speed);
  }else{
    digitalWrite(BIN1, inPin1);
    digitalWrite(BIN2, inPin2);
    analogWrite(PWMB, speed);
  }
}

void stop(){
//enable standby  
  digitalWrite(STBY, LOW); 
}
if (buttonState = HIGH)

Your homework assignment is to research the difference between = and ==.

THANKS!

now I get an error saying else without previous if here problem:

  if (buttonState == LOW);
  {
     move(1, 255, 1); //motor 1, full speed, left
     move(2, 255, 2); //motor 2, full speed, right
     delay(10);     
  }
  else
  {  
    move(1, 255, 1); //motor 1, full speed, left
    move(2, 255, 1); //motor 2, full speed, left
    delay(1000);
  }
  if (buttonState == LOW);

Lose the semi-colon.

Compare your first lines:

if (buttonState == LOW);
{

with these ones from the reference page…

if (pinFiveInput < 500)
{

One important difference!

Thanks jimbo, but now when it hits something it stops and the loop repeats here new code:

int STBY = 10; //standby

//Motor A
int PWMA = 3; //Speed control 
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction

//Motor B
int PWMB = 5; //Speed control
int BIN1 = 11; //Direction
int BIN2 = 12; //Direction


const int button = 2;

int buttonState = 0;


void setup(){
  pinMode(STBY, OUTPUT);

  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);

  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  
  pinMode(button, INPUT);
  
}

void loop(){
  

  
buttonState = digitalRead(button);

  if (buttonState == LOW)
  {
     move(1, 255, 1); //motor 1, full speed, left
     move(2, 255, 2); //motor 2, full speed, right
     delay(10);     
  }
  else
  { 
    move(1, 255, 2); 
    move(2, 255, 1); 
    delay(500);
    stop();
    move(1, 255, 1); //motor 1, full speed, left
    move(2, 255, 1); //motor 2, full speed, left
    delay(1000);
    stop();
  }
    
   
  }




void move(int motor, int speed, int direction){

//Move specific motor at speed and direction
//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise

  digitalWrite(STBY, HIGH); //disable standby

  boolean inPin1 = LOW;
  boolean inPin2 = HIGH;

  if(direction == 1){
    inPin1 = HIGH;
    inPin2 = LOW;
  }

  if(motor == 1){
    digitalWrite(AIN1, inPin1);
    digitalWrite(AIN2, inPin2);
    analogWrite(PWMA, speed);
  }else{
    digitalWrite(BIN1, inPin1);
    digitalWrite(BIN2, inPin2);
    analogWrite(PWMB, speed);
  }
}

void stop(){
//enable standby  
  digitalWrite(STBY, LOW); 
}

but now when it hits something it stops and the loop repeats

Not quite sure what you mean there... but yep loop() runs all the time, that's what it's for.

I'm not going to wade through that code to figure it out, but when the bumper switch gets pressed, are you trying to tell it to back out of there? I'd be inclined to back out from an obstacle with a bit of a turn for a short while, maybe a second or so; maybe a random amount of turn and random time?) Then move forward again, hoping this time to miss the obstacle...