Code review request for RC car

I'm doing an obstacle avoiding car with an arduino uno r3 board, hc-sr04 ultrasonic sensor, dual h-bridge l298 and sg90 microservo.

My code doesn't work, can anyone tell me what i'm doing wrong?

#define MOTOR1_CTL1  8  // I1  
#define MOTOR1_CTL2  9  // I2  
#define MOTOR1_PWM   11 // EA  
  
#define MOTOR2_CTL1  6  // I3  
#define MOTOR2_CTL2  7  // I4  
#define MOTOR2_PWM   10 // EB  
  
#define MOTOR_DIR_FORWARD  0  
#define MOTOR_DIR_BACKWARD   1  

#include <Servo.h>
 
Servo myservo;  
 
int pos = 0; 
int frontal;
int dreta;
int esquerra;
  
void setup()  
{  
   
   pinMode(MOTOR1_CTL1,OUTPUT);  
   pinMode(MOTOR1_CTL2,OUTPUT);  
   pinMode(MOTOR1_PWM,OUTPUT);  
     
  // Setup pins for motor 2  
   pinMode(MOTOR2_CTL1,OUTPUT);  
   pinMode(MOTOR2_CTL2,OUTPUT);  
   pinMode(MOTOR2_PWM,OUTPUT);  

   myservo.attach(3);  
   
   motorStart(1, 1);     //canvivi de direccio 1 endavant - 0 endarrera   
  setSpeed(1, 0);     // velocitat 0-255
  motorStart(2, 1);     //canvivi de direccio 1 endavant - 0 endarrera   
  setSpeed(2, 0);     // velocitat 0-255
  
   myservo.write(80);  // situem el servo al centre 
}  
  
void setSpeed(char motor_num, char motor_speed)  
{  
   if (motor_num == 1)  
   {  
      analogWrite(MOTOR1_PWM, motor_speed);  
   }     
   else  
   {  
      analogWrite(MOTOR2_PWM, motor_speed);  
   }  
}  
  
void motorStart(char motor_num, byte direccio)  
{  
    
   char pin_ctl1;  
   char pin_ctl2;  
     
   if (motor_num == 1)  
   {  
      pin_ctl1 = MOTOR1_CTL1;  
      pin_ctl2 = MOTOR1_CTL2;  
   }     
   else  
   {  
      pin_ctl1 = MOTOR2_CTL1;  
      pin_ctl2 = MOTOR2_CTL2;       
   }  
    
   switch (direccio)  
   {  
     case MOTOR_DIR_FORWARD:  
     {  
       digitalWrite(pin_ctl1,LOW);  
       digitalWrite(pin_ctl2,HIGH);            
     }  
     break;   
            
     case MOTOR_DIR_BACKWARD:  
     {  
        digitalWrite(pin_ctl1,HIGH);  
        digitalWrite(pin_ctl2,LOW);            
     }  
     break;           
   }  
}  
  
void motorStop(char motor_num)  
{  
   setSpeed(motor_num, 0);  
   if (motor_num == 1)  
   {  
     digitalWrite(MOTOR1_CTL1,HIGH);  
     digitalWrite(MOTOR1_CTL2,HIGH);       
   }  
   else  
   {  
     digitalWrite(MOTOR2_CTL1,HIGH);  
     digitalWrite(MOTOR2_CTL2,HIGH);       
   }  
}  
 

void loop()  
{ 
  //motors endavant
  motorStart(1, 1);     //canvivi de direccio 1 endavant - 0 endarrera   
  setSpeed(1, 200);     // velocitat 0-255
  motorStart(2, 1);     //canvivi de direccio 1 endavant - 0 endarrera   
  setSpeed(2, 200);     // velocitat 0-255
  
  //llegim sensor
  frontal =analogRead(3);
  if(frontal < 20)
  {
      motorStart(1, 1);     //canvivi de direccio 1 endavant - 0 endarrera   
      setSpeed(1, 0);     // velocitat 0-255
      motorStart(2, 1);     //canvivi de direccio 1 endavant - 0 endarrera   
      setSpeed(2, 0);     // velocitat 0-255
      
      myservo.write(135);
      esquerra =analogRead(3);
      myservo.write(35);
      dreta =analogRead(3);
      if( dreta > esquerra)
      {
         motorStart(1, 0);
         setSpeed(1, 200);
         motorStart(2, 1);
         setSpeed(2, 200);
         
      }
      else
      {
        motorStart(1, 1);
        setSpeed(1, 200);
        motorStart(1, 0);
        setSpeed(1, 200);
      }
  }
  
  
  
  
  
  
 
  else
  {
    motorStart(1, 1);
    setSpeed(1, 200);
    motorStart(2, 0);
    setSpeed(2, 200);
    delay(500);
  }
}

Thank you guys!

You have not said what is wrong but from Servo - Arduino Reference

on boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

and in your program you have
#define MOTOR2_PWM   10 // EB  and analogWrite(MOTOR2_PWM, motor_speed);  

Try moving the affected PWM output to another pin.

You're right, the problem is that when I use the myservo.attach(pin) function then the motor 2 don't run. But if I disconnect the enable B then the motor operates at maximum speed (255). So I can't regulate it.

I tried using different pins but it still doesn't work. Maybe the problem is the library or something. I don't know.

Please do not hijack other peoples discussion

  • splitted
  • changed topic.

I tried using different pins but it still doesn't work.

There's that lame "it doesn't work" crap again. What pin did you change to? What ACTUALLY happens?

Maybe the problem is the library or something.

I doubt that the problem is in any library. I'm willing to bet that the "or something" will be your wiring, your power supply, or your code.