Speed control of DC motor

Hi all,
I am using a pololu high power motor controller 18 vs 15 with arduino. Both are interfaced through serial interface.Hall effect sensor has been introduced in a system,which measures speed and displays it on serial monitor. Arduino example code has been used setmotorspeed() function increases and decreases speed properly . whwn i apply load across it the speed of motor decreases. I want to control its speed in such a manner so that if load is applied the speed of motor remains constant. any suggestions plz
Arduino example code for interface b/w motor controller and arduino

#include <NewSoftSerial.h>  
#define rxPin 3  // pin 3 connects to SMC TX  (not used in this example)
#define txPin 4  // pin 4 connects to SMC RX  
NewSoftSerial mySerial =  NewSoftSerial(rxPin, txPin);  
     
// required to allow motors to move  
// must be called when controller restarts and after any error  
void exitSafeStart()  
{  
  mySerial.print(0x83, BYTE);  
}  
   
// speed should be a number from -3200 to 3200  
void setMotorSpeed(int speed)  
{  
  if (speed < 0)  
  {  
    mySerial.print(0x86, BYTE);  // motor reverse command  
    speed = -speed;  // make speed positive  
  }  
  else 
  {  
    mySerial.print(0x85, BYTE);  // motor forward command  
  }  
  mySerial.print((unsigned char)(speed & 0x1F), BYTE);  
  mySerial.print((unsigned char)(speed >> 5), BYTE);  
}  
   
void setup()    
{
  // initialize software serial object with baud rate of 38.4 kbps
  mySerial.begin(38400);
 
  // the Simple Motor Controller must be running for at least 1 ms
  // before we try to send serial data, so we delay here for 5 ms
  delay(5);
   
  // if the Simple Motor Controller has automatic baud detection
  // enabled, we first need to send it the byte 0xAA (170 in decimal)
  // so that it can learn the baud rate
  mySerial.print(0xAA, BYTE);  // send baud-indicator byte
 
  // next we need to send the Exit Safe Start command, which
  // clears the safe-start violation and lets the motor run
  exitSafeStart();  // clear the safe-start violation and let the motor run
}  
   
void loop()  
{
  setMotorSpeed(3200);  // full-speed forward
  delay(1000);  
  setMotorSpeed(-3200);  // full-speed reverse  
  delay(1000);
}

Hello.

I suggest you implement a PID control system to control the motor speed. You can read more about it on Wikipedia. A section of our 3pi user’s guide implements a line-following PID control system that is a little bit more complex and different than what you need to do, but it might help guide you.

- Ryan