How i reduce speed of dc motor

Sir , i want to reduce the speed of 2 dc motor when obstacles comes .by L298N motor driver, ultrasonic sensor and arduino.sir how i coding for it

Using the algorithm you have developed for sensing & speed control, look into PWM control of DC motors.
The use of PWM is widely discussed in most Arduiono tutorials and fora.

Google is quite useful in searching for ‘how to’.

Here is one example: