Need help with code

Hello . Since I started in robotics, I need some help with the code for my robot. I need to track it has passed the edge of the maze is painted black, he must navigate the white and does not go beyond the edge of the track. I use the robot with the collector 4 engines based on the Arduino Mega and 2 track sensor but something in my code does not work, he just goes forward and all. help me please.Code without errors but the robot goes forward only And sorry for my weird English.

#include <AFMotor.h> //Link library for working with shield
#define LEFT_SENSOR_PIN  33
#define RIGHT_SENSOR_PIN 35
#define STATE_FORWARD    0
#define STATE_RIGHT      1
#define STATE_LEFT       2

int state = STATE_FORWARD ;

 // Plug motors to the terminal blocks M1, M2, M3, M4
 AF_DCMotor motor1(1); //задний левый
 AF_DCMotor motor2(2); //задний правый 
 AF_DCMotor motor3(3); //передний правый 
 AF_DCMotor motor4(4); //передний левый
 
 void runForward() 
{
  state = STATE_FORWARD;
 
 motor1.run(FORWARD); // Ask moving forward
 motor2.run(FORWARD);
 motor3.run(FORWARD);
 motor4.run(FORWARD);
 motor1.setSpeed(150); // Set the speed
 motor2.setSpeed(150);
 motor3.setSpeed(150);
 motor4.setSpeed(150);
  }
   void runRIGHT() 
{
  state = STATE_RIGHT;
 
 motor1.run(FORWARD); // Ask moving forward
 motor4.run(FORWARD);
 motor1.setSpeed(150); // Set the speed
 motor4.setSpeed(150);
  }
  void runLEFT() 
{
  state = STATE_LEFT;
 
 motor3.run(FORWARD); // Ask moving forward
 motor2.run(FORWARD);
 motor3.setSpeed(150); // Set the speed
 motor2.setSpeed(150);
  }
 void setup(){
 // Set the maximum speed of the motor (similar work PWM)
 motor1.setSpeed(255);
 motor1.run(RELEASE);
 motor2.setSpeed(255);
 motor2.run(RELEASE);
 motor3.setSpeed(255);
 motor3.run(RELEASE);
 motor4.setSpeed(255);
 motor4.run(RELEASE);
 
 }
  
 void loop(){
    boolean left = digitalRead(LEFT_SENSOR_PIN);
    boolean right = digitalRead(RIGHT_SENSOR_PIN);
   
  int targetState;
    if (left == right) {
       
        targetState = STATE_FORWARD;
        } else if (left) {
          targetState = STATE_RIGHT;
        }
        else
        {
          targetState = STATE_LEFT;
        }
        
if (state == targetState) {
        
        return;
    }
   switch (targetState) {
        case STATE_FORWARD:
            runForward();
            break;
 
        case STATE_RIGHT:
            runRIGHT();
            break;
 
        case STATE_LEFT:
            runLEFT();
            break;
    }
 
  delay(50);
    
}

It would make sense (to me) to add some simple prints, or LEDs to let your sketch tell you what it is seeing and doing.

adding pinMode to set up so the arduino knows that 33 and 35 are inputs might help

I see lots of code that turns the motors on , but none that turns them off

#include <AFMotor.h> //Link library for working with shield
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

void setup(){
// Set the maximum speed of the motor (similar work PWM)
motor1.setSpeed(255);
motor1.run(RELEASE);
motor2.setSpeed(255);
motor2.run(RELEASE);
motor3.setSpeed(255);
motor3.run(RELEASE);
motor4.setSpeed(255);
motor4.run(RELEASE);

}

void loop(){

if (digitalRead(33) == HIGH && digitalRead(33) == HIGH ) {

motor1.run(FORWARD); // Ask moving forward
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
motor1.setSpeed(250); // Set the speed
motor2.setSpeed(250);
motor3.setSpeed(250);
motor4.setSpeed(250);
delay(20);

motor1.run(RELEASE); // Ask moving forward
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
if (digitalRead(33)== LOW) {
motor1.run(FORWARD); // Ask moving forward
motor4.run(FORWARD);
motor1.setSpeed(250); // Set the speed
motor4.setSpeed(250);
delay(20);

motor1.run(RELEASE); // Ask moving forward
motor4.run(RELEASE);

}
if (digitalRead(35)== LOW)
{motor3.run(FORWARD); // Ask moving forward
motor2.run(FORWARD);
motor3.setSpeed(250); // Set the speed
motor2.setSpeed(250);

delay(20);
motor2.run(RELEASE); // Ask moving forward
motor3.run(RELEASE);
}
return;

}

Now he's just going forward and turning right but does not turn to the left

Now how to do that he went forward left and right not just right and left ??? Any ideas??

if (digitalRead(33) == HIGH && digitalRead(33) == HIGH )

spot the error?

In this condition will be 1 at pin 33 and 35 thisis track white color. Left and right sensor must be 1 for muving robot forward.

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" icon above the posting area. It is the first icon, with the symbol: </>

How to use this forum

gpop1 you was right he move only forward.