measuring and controlling speed and position of Dc motor

hi all
I am new to arduino i want to measure speed of dc motor by using light sensor and then control the speed and position and position by potentiometer. I am using pololu high speed motor controller with arduino which is able to control a 24 V motor Arduino’s pin 4 is attached to serial interface of controller and ground is connected to the ground. Now for the begining the code is running fine as I burned the code the motor started rotating backward and forward.

#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
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

// 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

// 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
setMotorSpeed(-3200); // full-speed reverse

This code is running perfectly fine what i need is
1.To insert a potentiometer section in this code so that its speed and direction of rotation should be controlled by potentiometer.I know about the connections of potentiometer that is one to the digital pwm pin second to power supply of 5v and third to ground.
2. Light sensor portion to the code to measure speed of dc motor

I will be waiting for your assistance

Actually I am still confused on LED portion ..the aim is to measure speed and position of DC motor and display it on LCD display with some kind of sensor and honestly speaking I am unable to choose a perfect sensor for measuring these two parameters and compatible to adruino. About the motor, it has got a white strip on its shaft and the speed and position is supposed to measured by some throwing light on that strip and output will be given as an input to LCD to show its speed ist problem is selection of such kind of sensor and second problem is coding associated with it but the choice of perfect sensor is major issue.

it is a simple 24 V DC motor-generator set I m sorry to say that the model is obsolete so i cant share any link for the motor but it is running properly by motor controller and the code given above its a white strip on the shaft which has to be followed for the speed and position and sensor output is taken to LCD display

its not a secret actually i am doin it first tyme so i might not explained u properly what i m trying 2 do I have left "postion" portion for now I just want to measure speed of motor in rpm and display it on real tyme LCD 16*2

ok i start from begining

A motor is rotating the motor is controlled by adriono through Pololu controller,the code is given above.There is a white strip on the shaft of motor. Now I need a light sensor (or any other cheap sensor ) which by throwing light on that strip measures the counts and gives me rpm continuosly as i increase or decrease speed on the screen of lcd

I need coding to fit in the above coding and connections if u or anybody helps me I would be sincerely grateful

tnks alot

I have seen the code given in frequency counter library and the sensor u proposed but in the code i have not seen any line that takes input from sensor or reading sensor pin i think this code needs modification if this sensor is attached to it and frequency is supposed to be measured through this sensor.