Speed control of DC Geared motor with encoder using arduino mega

I am doing a project in which I am using High torque High precision Encoder DC geared Motor (12V 300RPM). I have to code the DC motor to run at a specific speed using Arduino Mega 2560. This is my first time experience with Arduino and have very little knowledge of coding. I have been able to run my motor at its full speed using the basic example in encoder library.

/* Encoder Library - Basic Example
 * This example code is in the public domain.

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(5, 6);
//   avoid using pins with LEDs attached

void setup() {
  Serial.println("Basic Encoder Test:");

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;

The issue is I am unable to control the speed of the motor. I want my motor to run at a specified speed in either forward or backward direction until I change the speed, direction or stop the motor. I have a RMCS-2301 motor driver but I did not use it while running my motor at full speed. Thus, I am not vey sure whether I am required to use it for this purpose. Eventually I have to control 4 motors but for now I want to get it right for one motor and then proceed further. I would really appreciate any help regarding how to control the speed of my motor.

Motor: https://robokits.co.in/motors/encoder-dc-servo/high-torque-high-precision-encoder-dc-geared-motor-12v-300rpm

Motor Driver: https://robokits.download/downloads/RMCS-2301.pdf

Thanks in advance.

For this you need a control loop. That is usually implemented with a PID controller library.

You can either do your control in terms of position or speed. For constant speed you can use
position control with the setpoint constantly increasing, or use speed control directly with a
constant setpoint.

The complication is that an encoder provides distance information, not speed information.

You can calculate speed from encoder readings, but with a small time delay (time delays are
the enemy of control loop stability). This means in practice your speed control loop will have to
have somewhat lower gain - but its probably not an issue.

MarkT's recommendation is a very good one. A method with a control loop will do be able to do this.

A P.I.D. algorithm should get this done for you.

CattleDog has a nice external timer code that counts pulses over a preset amount of time, which gets estimates of RPM in a pretty good way.

There are other ways for getting estimates of RPM ... such as using interrupts, which might do a similar job ..... but pulses causing a bunch of interrupts might lead to the processing getting tied up too much, and causing more unwanted delay. Unaccounted delays can often be something we don't want.