How to decrease speed of motors in L298n motor driver in a line following robot

Hello there, I am currently trying to make a line follower using two standard IR sensors, a L298n H-Bridge motor driver, and an arduino uno. What I want to do is that since the robot is going way too fast I want to decrease it's overall speed that it turns and goes straight. This is the code that I am using:

/*------ Arduino Line Follower Code----- */
/*-------definning Inputs------*/
#define LS 9      // left sensor
#define RS 10     // right sensor

/*-------definning Outputs------*/
#define LM1 3       // left motor
#define LM2 4       // left motor
#define RM1 8       // right motor
#define RM2 7       // right motor
#define MtrspeedA 8 
#define MtrspeedB 3

void setup()
{
  pinMode(LS, INPUT);
  pinMode(RS, INPUT);
  pinMode(LM1, OUTPUT);
  pinMode(LM2, OUTPUT);
  pinMode(RM1, OUTPUT);
  pinMode(RM2, OUTPUT);


}

void loop()
{
  if(!(digitalRead(LS)) && !(digitalRead(RS)))     // Move Forward
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
  }
  
  if(digitalRead(LS) && !(digitalRead(RS)))     // Turn right
  {
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
  }
  
  if(!(digitalRead(LS)) && digitalRead(RS))     // turn left
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
  }
  
  if(digitalRead(LS) && digitalRead(RS))     // stop
  {
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
  }
}

Thank you for any help

Use PWM pins and analogWrite() to control the speed.

Most Arduino motor driver tutorials use this method, for example.

Ok so I edited the code to this:

/*------ Arduino Line Follower Code----- */
/*-------definning Inputs------*/
#define LS 9      // left sensor
#define RS 10     // right sensor

/*-------definning Outputs------*/
#define LM1 3       // left motor
#define LM2 4       // left motor
#define RM1 8       // right motor
#define RM2 7       // right motor
#define MtrspeedA 8 
#define MtrspeedB 3

void setup()
{
  pinMode(LS, INPUT);
  pinMode(RS, INPUT);
  pinMode(LM1, OUTPUT);
  pinMode(LM2, OUTPUT);
  pinMode(RM1, OUTPUT);
  pinMode(RM2, OUTPUT);
}

void loop()
{
  if(!(digitalRead(LS)) && !(digitalRead(RS)))     // Move Forward
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, 100);
    analogWrite(MtrspeedB, 100);

  }
  
  if(digitalRead(LS) && !(digitalRead(RS)))     // Turn right
  {
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, 100);
    analogWrite(MtrspeedB, 100);
  }
  
  if(!(digitalRead(LS)) && digitalRead(RS))     // turn left
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, 100);
    analogWrite(MtrspeedB, 100);
  }
  
  if(digitalRead(LS) && digitalRead(RS))     // stop
  {
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
    analogWrite(MtrspeedA, 0);
    analogWrite(MtrspeedB, 0);
  }
}

But now all it does is go straight and the line following ability doesn't work and it still does not slow down, Please help, thank you

You seem to be using pins 3 and 8 for two different purposes. That hardly ever works.

Also please post a circuit diagram so we know, among other things, which Arduino pins are connected to which L298N pins. Otherwise we're just trying to guess what's happening.

Steve

Thank you it works now