How to select speed before set run direction...

I would like you to give me an idea about how to add to this sketch the functionality of previously choosing the speed of the motors using an AT command with pwm (using the same bluetooth app I have) so that you can select for example: 25%, 40%, 50%, 75% and 100%

This is the program that I currently have:

char t;
 
void setup() {
pinMode(13,OUTPUT);   //left motors forward
pinMode(12,OUTPUT);   //left motors reverse
pinMode(11,OUTPUT);   //right motors forward
pinMode(10,OUTPUT);   //right motors reverse
 
Serial.begin(9600);
 
}
 
void loop() {
if(Serial.available()){
  t = Serial.read();
  Serial.println(t);
}
 
if(t == '1'){            //move forward(all motors rotate in forward direction)
  digitalWrite(13,HIGH);
  digitalWrite(12,LOW);
  digitalWrite(11,HIGH);
  digitalWrite(10,LOW);
}
 
else if(t == '2'){      //move reverse (all motors rotate in reverse direction)
  digitalWrite(13,LOW);
  digitalWrite(12,HIGH);
  digitalWrite(11,LOW);
  digitalWrite(10,HIGH);
}
 
else if(t == '3'){      //turn right (left side motors rotate in forward direction, right side motors doesn't rotate)
  digitalWrite(13,LOW);
  digitalWrite(12,LOW);
  digitalWrite(11,HIGH);
  digitalWrite(10,LOW);
}
 
else if(t == '4'){      //turn left (right side motors rotate in forward direction, left side motors doesn't rotate)
  digitalWrite(13,HIGH);
  digitalWrite(12,LOW);
  digitalWrite(11,LOW);
  digitalWrite(10,LOW);
}
 
else if(t == '5'){      //STOP (all motors stop)
  digitalWrite(13,LOW);
  digitalWrite(12,LOW);
  digitalWrite(11,LOW);
  digitalWrite(10,LOW);
}
delay(100);
}

Thanks in advance for any clue

A quick and dirty using your existing code would be to map your speed to a variable in the delay statement.

Example:

If full speed is 100ms delay then half speed might be 200ms.

double speed = 1 // 1, .75, .5, .4, .25
uint32_t speed_delay = (1 / speed)
delay (speed_delay)

Hello DKWattson,

Thanks a lot for your answer! I´ll try to figure out how to write down the code for it...this should be my first attemp for coding

BR

Your first attempt at coding? You've never written code before?

Hello, yes, this is my first attemp...

I have built a small car with 4 motors. It is a kind of motorized dolly to record videos and photographs. Which I control by bluetooth.
Now I need to be able to define three or four motor speeds from the same app by sending commands to the arduino. In the application I have several programmable buttons that I can use to send a command character.

Thanks in advance for your help

Let me know how you're doing. My day job is in motion control and robotics/automation so I have some interest in your project.