By Eduardo Contreras – Computer, Power, and Powertrain
The following code is a beta version of converting a dc motor into a servo:
For connections, see Figure 1 below:
Note: The idea for this is for both shafts from the motor and potentiometer to be connected together so that the potentiometer will turn whenever the motor moves in any direction and will keep that position saved in a variable, here called cur_pot.
Before letting the user command the motor to move, it will need to reset its position to a predetermined position. In this case, it will move until the Arduino reads the potentiometer at a certain value, here being 160. After it is in position, the user will be able to tell the Arduino to move the motor.
When the user wants to turn the motor, the user will either type ‘r’, which will turn right, or ‘l’, which will turn left. This is done by incrementing, to turn right, or decrementing, to turn left, a second variable, new_pot, and have the Arduino compare this value to the current reading from the potentiometer. Depending on the value, the motor will either run forward or backwards, trying to reach the second variable number. After it reaches the value, the motor will stop. If the position changes from an outside force, the code will try to go back to the previous value. Afterwards, the user can type another instruction to turn either left or right.
The following video demonstrates how the code works: