How can i get a DC motor to go Slower ?

hy guys i'd made a autonom object avoiding car, and i notice a problem she is respondig fine to the object around, and steer and everything but the problem is that it's going to fast so i had to put the stop() function at 60cm to the car shoud stop in the object :( i wonder it's there some possebility to make the motor go slower(with code)? Thanks

Sory this is the code:

//sensors
const int pingPin9 = 9;
const int pingPin8 = 8;
const int pingPin3 = 3;
const int pingPin2 = 2;
// motor pins
int E1 = 7; 
int M1 = 6; 
int E2 = 4; 
int M2 = 5; 

void setup() 
  { 
    Serial.begin(9600);
  pinMode(M1, OUTPUT); 
  pinMode(M2, OUTPUT); 
  }
  
  void loop()
{
  long dist2 = ping(pingPin2);
  delay(150);
  long dist8 = ping(pingPin8);
  delay(150);
  long dist3 = ping(pingPin3);
  delay(150);
  long dist9 = ping(pingPin9);
  delay(150);
  
Serial.print("Spate: "); Serial.println(dist2);
Serial.print("Stanga: "); Serial.println(dist8);
Serial.print("Fata: "); Serial.println(dist3);
Serial.print("Dreapta: "); Serial.println(dist9);

//conditi de conducere ! 
if (dist8 < 40) Dreapta();
if (dist9 < 40) Stanga(); 


}

  long ping(int pinNumToReadFrom)
{
  long duration, cm;
  pinMode(pinNumToReadFrom, OUTPUT);
  digitalWrite(pinNumToReadFrom, LOW);
  delayMicroseconds(2);
  digitalWrite(pinNumToReadFrom, HIGH);
  delayMicroseconds(5);
  digitalWrite(pinNumToReadFrom, LOW);
  pinMode(pinNumToReadFrom, INPUT);
  duration = pulseIn(pinNumToReadFrom, HIGH);
  cm = microsecondsToCentimeters(duration);
  return cm;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
 
 void MergeFata()
{ 
digitalWrite(M1,HIGH); 
digitalWrite(M2,LOW);
analogWrite(E1, 0); //PLL Speed Control 
analogWrite(E2, 128); //PLL Speed Control 
} 
void MergeSpate()
{ 
digitalWrite(M1,HIGH); 
digitalWrite(M2,HIGH);
analogWrite(E1, 0); //PLL Speed Control 
analogWrite(E2, 128); //PLL Speed Control 
} 
void Dreapta()
{ 
digitalWrite(M1,HIGH); 
digitalWrite(M2,HIGH);
analogWrite(E1, 128); //PLL Speed Control 
analogWrite(E2, 0); //PLL Speed Control 
} 
void Stanga()
{
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 0);
}
void MergeFataDreapta()
{
digitalWrite(M1,HIGH);
digitalWrite(M2,LOW);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void MergeFataStanga()
{
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void MergeSpateDreapta()
{
digitalWrite(M1,HIGH);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void MergeSpateStanga()
{
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E1, 128);
analogWrite(E2, 128);
}
void Stop()
{
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
analogWrite(E1, 0);
analogWrite(E2, 0);
}

The motor is a small DC motor 6V, and is connected to a shield L298N,

Could you give me an example(code) how can i get the motor slower with PWM ?

the speed is being controlled by the analogWrite statements as below for example:

digitalWrite(M1,HIGH); 
digitalWrite(M2,HIGH);
analogWrite(E1, 0); //PLL Speed Control 
analogWrite(E2, 128); //PLL Speed Control

if you want it to go slower try changing the 128's to 92 or even 64.

i tryed to change from 128 to a lower value but when i change it the motor wont move at all. :frowning:

I wrote a webpage about this, and your not the first to bring up this subject. I've done it before, so have others. You need to have your motors in a PWM pin on the Arduino. You might want to check out my website first so you don't blow your board to quick. https://sites.google.com/site/arduinosoapy29/motor-speed-controller What board are you using? How many AMPs are your motors rated for? How many volts? Etc. We need to know it all!! lol :D (Oh, and the way I control my motors, idk how to reverse them :stuck_out_tongue_closed_eyes:)