Softpwm for DC motor control

Hello
I am trying to adapt a potentiometer to stepper motor system for use with a DC motor with quadrature encoder. The system is written with Arduino Leonardo in mind but the hardware does not physically have all the pins. I have attached a schematic of the board plus a picture of the pins.

The code that I am working with was written with a standard Leonardo and so does not work. I have been advised to find a soft pwm solution. All that I can find on the web concerning these types of libraries is for LED controls with a vague mention that it might be possible for DC motors.
Therefore, can anyone tell me using a soft pwm library could be an option for a DC motor and if so what would be the best library to intergrate with the code below?

#include <Encoder.h>
#define GAIN 20    //how agressively it tries to get to the desired position
#define PWMPIN A0 // Output pin to control PWM for motor controller
#define DIRPIN A1 // Output direction for motor controller
#define CHANNELA A2 // motor position sensor pin
#define CHANNELB A3 // motor position sensor pin
#define POTPIN A4    // select the input pin for the potentiometer
Encoder MyEnc(CHANNELA, CHANNELB);

void driveMotor(int speed){
  //drive motor at desired speed
  //-255 is max reverse, +255 is max forwards
  if(speed>=0) {
    digitalWrite(DIRPIN, 0);
    analogWrite(PWMPIN, speed);
  } else {
    digitalWrite(DIRPIN, 1);
    analogWrite(PWMPIN, -speed);
  }
}

int desiredPosition() {
  //Put the mathematics to convert pot position to encoder position here
  //This can be called from anywhere in the code with consistent results
  return map(analogRead(POTPIN), 0, 1023, 0, 9023);
}

void setup() {
  pinMode(PWMPIN, OUTPUT);
  pinMode(DIRPIN, OUTPUT);
  pinMode(CHANNELA, INPUT_PULLUP);
  pinMode(CHANNELB, INPUT_PULLUP);
  //drive motor all the way one way, then take this as the zero position
  driveMotor(-50); //drive backwards slowly
  delay(5000);    //maybe it takes 5 seconds to get there?
  driveMotor(0);  //stop
  delay(100);     //let it settle.
  MyEnc.write(0); //call this zero
  //now drive the motor somewhere close to where the pot currently is
  //this is to stop it slamming there at full speed.
  while(MyEnc.read() < desiredPosition()) {
    driveMotor(50);
  }
  driveMotor(0);
}

void loop() {
  int actualPosition = MyEnc.read();
  driveMotor(GAIN * (actualPosition - desiredPosition()));
}

Many thanks

I am trying to adapt a potentiometer to stepper motor system for use with a DC motor with quadrature encoder.

So where does the PWM come in and what have those pictures go to do with your problem or setup?

PWM pin goes to the motor controller. Attached is the wiring schematic of the system.
The pictures are to show what pins are available.
I hope this helps
Thank you

170228_LENSHOUND_SCHEMATIC.pdf (172 KB)

OK but your opening line was

am trying to adapt a potentiometer to stepper motor system for use with a DC motor with quadrature encoder.

Where is the stepping motor in that setup?
Is there not a proper PWM signal pin avaliable? Could you not swap that analog pin ( used as a digital pin ) with one of the signals on a real PWM pin?

You do know that you can't make a DC motor behave like a stepping motor?

There are 4 pins on the underside of the card that are used for the stepper motor. I have not used them and to be honest I don't know why. I am going to investigate.
The above code that was written for a DC motor. The stepper motor code is very different.