How to control the speed of the motors (DC speed motors with L298N driverboard)

Hello everyone,

I'm doing a project for school for my final exams, i build a smart robot car with the smart robot car kit v.1 from eleego. My project is based on the obstacle avoidance, and i already had a programm given with the kit for it.
I tested it and apparently with my car, my motors are turning too fast so that the sensor for the obstacle avoidance can't stop the car before it hits the obstacle.
I wanted to know how to configure a lower speed for the DC motors.
Here is my code below, please tell me what i need to change or if i need something else.

#include <Servo.h> //servo library
Servo myservo; // create servo object to control servo
int Echo = A4;  
int Trig = A5; 
int in1 = 9;
int in2 = 8;
int in3 = 7;
int in4 = 6;
int ENA = 11;
int ENB = 5;
int ABS = 130;
int rightDistance = 0,leftDistance = 0,middleDistance = 0 ;
void _mForward()
{
 analogWrite(ENA,ABS);
 analogWrite(ENB,ABS);
 digitalWrite(in1,LOW);
 digitalWrite(in2,HIGH);
 digitalWrite(in3,LOW);
 digitalWrite(in4,HIGH);
 Serial.println("go forward!");
}

void _mBack()
{
 analogWrite(ENA,ABS);
 analogWrite(ENB,ABS);
 digitalWrite(in1,HIGH);
 digitalWrite(in2,LOW);
 digitalWrite(in3,HIGH);
 digitalWrite(in4,LOW);
 Serial.println("go back!");
}

void _mleft()
{
 analogWrite(ENA,ABS);
 analogWrite(ENB,ABS);
 digitalWrite(in1,LOW);
 digitalWrite(in2,HIGH);
 digitalWrite(in3,HIGH);
 digitalWrite(in4,LOW);
 Serial.println("go left!");
}

void _mright()
{
 analogWrite(ENA,ABS);
 analogWrite(ENB,ABS);
 digitalWrite(in1,HIGH);
 digitalWrite(in2,LOW);
 digitalWrite(in3,LOW);
 digitalWrite(in4,HIGH);
 Serial.println("go right!");
} 
void _mStop()
{
  digitalWrite(ENA,LOW);
  digitalWrite(ENB,LOW);
  Serial.println("Stop!");
} 
 /*Ultrasonic distance measurement Sub function*/
int Distance_test()   
{
  digitalWrite(Trig, LOW);   
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);  
  delayMicroseconds(20);
  digitalWrite(Trig, LOW);   
  float Fdistance = pulseIn(Echo, HIGH);  
  Fdistance= Fdistance/58;       
  return (int)Fdistance;
}  

void setup() 
{ 
  myservo.attach(3);// attach servo on pin 3 to servo object
  Serial.begin(9600);     
  pinMode(Echo, INPUT);    
  pinMode(Trig, OUTPUT);  
  pinMode(in1,OUTPUT);
  pinMode(in2,OUTPUT);
  pinMode(in3,OUTPUT);
  pinMode(in4,OUTPUT);
  pinMode(ENA,OUTPUT);
  pinMode(ENB,OUTPUT);
  _mStop();
} 

void loop() 
{ 
    myservo.write(90);//setservo position according to scaled value
    delay(500); 
    middleDistance = Distance_test();
    #ifdef send
    Serial.print("middleDistance=");
    Serial.println(middleDistance);
    #endif

    if(middleDistance<=20)
    {     
      _mStop();
      delay(500);                         
      myservo.write(5);          
      delay(1000);      
      rightDistance = Distance_test();

      #ifdef send
      Serial.print("rightDistance=");
      Serial.println(rightDistance);
      #endif

      delay(500);
       myservo.write(90);              
      delay(1000);                                                  
      myservo.write(180);              
      delay(1000); 
      leftDistance = Distance_test();

      #ifdef send
      Serial.print("leftDistance=");
      Serial.println(leftDistance);
      #endif

      delay(500);
      myservo.write(90);              
      delay(1000);
      if(rightDistance>leftDistance)  
      {
        _mright();
        delay(180);
       }
       else if(rightDistance<leftDistance)
       {
        _mleft();
        delay(180);
       }
       else if((rightDistance<=20)||(leftDistance<=20))
       {
        _mBack();
        delay(180);
       }
       else
       {
        _mForward();
       }
    }  
    else
        _mForward();                     
}

That code could use some helpful comments e.g.

int ABS = 130;  // controls the speed of the motors 0 = stop, 255 = full speed

Does that give you any useful ideas?

Steve

I'm stunned that using code that comes with a kit, then getting others to fix it for you, counts as "doing a project for school for my final exams".

(I'm wondering what the heck you were doing in the 6 weeks since you posted the same code and question here... but I guess that's none of my business. Can't help wondering, though. I suppose next will be a cry of "it's urgent").

I thought first i should change the value of ABS but i tested it with apparently to low values, now it works.
The lowest speed for the 4 motors to work is 87 and then lower only 2 motors are working and lower 0.
Thank u for ur help. I bet i need to change some values in my turning right and left fonction now.

elvon_blunden:
I'm stunned that using code that comes with a kit, then getting others to fix it for you, counts as "doing a project for school for my final exams".

(I'm wondering what the heck you were doing in the 6 weeks since you posted the same code and question here... but I guess that's none of my business. Can't help wondering, though. I suppose next will be a cry of "it's urgent").

The robot car that i build with a kit is for an experience for a project, i'm maybe asking it the wrong way but i'm only a new user of arduino cards and i didn't know anything about it 6 weeks ago. I'm just asking on forums, that what forum are used for right?
The way i do it is not ur problem and i don't care about "the way i should do it". I'm just doing it the way i can. Sure it would be "better" doing my own things but i didn't had the time and the knowledge for it.
Thanks for the comments even if it doesn't help much...
I took time responding cause u seemed to wonder "so much".

The lowest speed for the 4 motors to work is 87

The L298 chip suffers from a large voltage drop of 1.8V minimum and almost 5V when any substantial current is drawn, as shown here from the 298 datasheet:

That would / could explain why the motors aren't running below 87, which is in theory about 1/3 (ie 87/255) of your supply voltage, but then from that you must subtract somewhere between 2 and 5V to see what's actually getting to the motors. There's too little left to turn the motors, especially at start-up from standstill, where the current draw is higher and the voltage drop is at its worst.

That gives you a very small range of PWMs, if 130 is too fast to stop in time but 87 isn't moving at all.

Crystone:
Thanks for the comments even if it doesn't help much...

I apologise; I was having a bad moment, and shouldn't have responded like that. The underlying snideness (if there's such a word?) of this forum must be rubbing off on me. Sorry.