Here is the Arduino Program that will listen to the Serial Port. Based on the command received on the Serial Port it will either Set the Direction of the Motor or Set the PWM Duty Cycle of the Motor Driver, thereby controlling the speed of the motor. The Program uses the Frequency Count Library to measure the speed of the Motor by an optical disk encoder and a slotted optical sensor. To make the Motor rotate in a Clockwise direction send the command "cw#" over the Serial terminal. To make it rotate in an anti-clockwise direction send "ccw#". To set motor speed to send in the following format e.g. "s128#" i.e. to set PWM at 50% and so on. The numeric values in the string "s!!!#", where !!! can be 0 to 255, i.e 8 bit Integer.

// Declare Freq Count Pin
// Unusable Pins by using FreqCount Library 3, 9, 10, 11
#include "FreqCount.h";

// Declare RPM(Pulses)-in Pin / Encoder Pin
#define MotorRPM 5;

// Declare Motor Driver Pins
#define MotorPlus 2
#define MotorMinus 4
#define MotorPWM 6

// Declare global speed variable;
int rpm; // Measured Spped
int pwmDuty; // Desired Speed as Duty of PWM

// Declare variable to store command
String command;
// e.g. s120# will set pwm to 120
// e.g. dcw# will set direction to cw
// e.g. dccw# will set ccw direction of motor
// e.g. m# will report current speed

void setup() {
  // start serial port at 9600 bps:
  //Serial.begin(115200);
  Serial.begin(500000);
  FreqCount.begin(500);
  // Begin frequency counting. GateInterval is the time in milliseconds 
  // for each measurement. Using 1000 provides direct frequency output without mulitplying or dividing by a scale factor.

}

void loop() {
  // Send Motor Speed

  // Receive PWM Duty
  handleSerial();
}

int measureRPM()
{
  //Measure RPM of Motor by calculating no of pulses in a fixed time interval
  if (FreqCount.available()) {
    unsigned long count = FreqCount.read(); // Pulses in 500 milliSecond
    rpm = (count/8) * 60 * 2;
    // RPM = (No of counted Pulses / No of Cuts in the Encoder) * Time Interval Scaling factor
    // Determine the time interval scaling factor from FreqCount.begin();
  }
  return rpm;
}

void setMotorSpeed()
{
  // Set the Motor Speed by AnalogWrite
  analogWrite(MotorPWM, pwmDuty);
}

void setMotorDirectionCW()
{
  // Set Motor Direction ClockWise
  digitalWrite(MotorPlus, HIGH);
  digitalWrite(MotorMinus, LOW);
}

void setMotorDirectionCCW()
{
  // Set Motor Direction Counter-ClockWise
  digitalWrite(MotorPlus, LOW);
  digitalWrite(MotorMinus, HIGH);
}

void handleSerial() {
  while (Serial.available() > 0) {
    command = Serial.readStringUntil('#');
    char commandType = command.charAt(0);
    if (commandType == 's') {
      // Set Motor Speed (write pwm duty)
      String pwmDuty_ = command.substring(1);
      pwmDuty = pwmDuty_.toInt();
      setMotorSpeed();
      //Serial.print("The Motor PWM Duty Cycle is set to ");
      Serial.println(pwmDuty);
    }
    else if (commandType == 'c') {
      String motorDirection = command;
      if (motorDirection == "cw") {
        setMotorDirectionCW();
        Serial.print("Motor Direction is set to Clock-Wise \n");
      }
      else if (motorDirection == "ccw") {
        setMotorDirectionCCW();
        Serial.print("Motor Direction is set to Counter-Clock-Wise \n");
      }
    }
    else if (commandType == 'm') {
      // Measure motor speed and report
      int currentSpeed = measureRPM();
      //Serial.print("Current Speed of the motor is ");
      Serial.println(currentSpeed);
    }
    else {
      Serial.print("Unknown Command Received ");
      Serial.println(command);
    }
  }
}