MiniSumo Program

Hey guys! I’ve doing a Minisumo Bot for a competition in México and I’m not very experienced in programming in any language so I’ll like you guys to help me a little with my program because it doesn’t work well, the robot only turn around and when another thing pass in front of it it doesn’t move forward to it!

Here’s the code:

//LIGHT
int luzfront = 3;
int valluzfront = 0;

//IR
int IRizq = 0;
int valizq = 0;
int cmizq;
int IRder = 1;
int valder = 0;
int cmder;

//ULTRASONIC
const int pingPin = 2;

//MOTORS
int motor1A = 3;
int motor1B = 4;
int motor1_ENABLE = 5;
int motor2_ENABLE = 6;
int motor2A = 7;
int motor2B = 8;

void setup (){
  Serial.begin(9600);
  
  pinMode(motor1A, OUTPUT);
  pinMode(motor1B, OUTPUT);
  pinMode(motor2A, OUTPUT);
  pinMode(motor2B, OUTPUT);
  pinMode(motor1_ENABLE, OUTPUT);
  pinMode(motor2_ENABLE, OUTPUT);
  
  delay(5000);

}

void loop(){
  
  long duration, cm;
  
  {
    valluzfront = analogRead(luzfront);
    delay(10);
  
  
  while(valluzfront > 100)
  {
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);
   pinMode(pingPin, INPUT);
   duration = pulseIn(pingPin, HIGH);
   cm = microsecondsToCentimeters(duration);
   delay(10);
   
   valizq = analogRead(IRizq);
   cmizq = map(valizq,65,645,40,3);
   delay (10);
   
   valder = analogRead(IRder);
   cmder = map(valizq,65,645,40,3);
   delay (10);
  
   if(cm >0 && cm <30)
   {
     forward();
   }
   else
   {
     if(valizq < 30)
     {
       left();
     }
     else
     {
       if(valder < 30)
       {
         right();
       }
     }
   }
  }
     
  }
  
  if (valluzfront < 100)
  {
    back();
  }
}

  

  
  
  
void forward()
   {
    digitalWrite(motor1_ENABLE, 250);
    digitalWrite(motor2_ENABLE, 250);
    digitalWrite(motor1A, HIGH);
    digitalWrite(motor1B, LOW);
    digitalWrite(motor2A, HIGH);
    digitalWrite(motor2B, LOW);
  }
  
void left() 
  {
    digitalWrite(motor1_ENABLE, 20);
    digitalWrite(motor2_ENABLE, 20);
    digitalWrite(motor1A, HIGH);
    digitalWrite(motor1B, LOW);
    digitalWrite(motor2A, LOW);
    digitalWrite(motor2B, HIGH);
    }
    
void right()
{
    digitalWrite(motor1_ENABLE, 20);
    digitalWrite(motor2_ENABLE, 20);
    digitalWrite(motor1A, LOW);
    digitalWrite(motor1B, HIGH);
    digitalWrite(motor2A, HIGH);
    digitalWrite(motor2B, LOW);
}

void back()
  {
    digitalWrite(motor1_ENABLE, 250);
    digitalWrite(motor2_ENABLE, 250);
    digitalWrite(motor1A, LOW);
    digitalWrite(motor1B, HIGH);
    digitalWrite(motor2A, LOW);
    digitalWrite(motor2B, HIGH);
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

Any ideas to improve it?

The sketch seems to be doing various things with sensors and motors. Do all of those functions work correctly?