I'm a novice at writing code, so please be gentle with me.
I have a project that I need to do, and before I start spending hours going round in circles, I thought I'd ask for help.
I've been looking all over and can only seem to find examples for dc motor speed control, which all look too complicated for what I require.
So far, I can control the 12v DC motor in both directions using a h bridge. The bit that is confusing me, the motor has an encoder that generates a 0v to 5v squarewave.
What my aim is, is to first move the motor to its endstop then move it to it's other endstop and counter the number of pulses from the encoder then move the motor to it's mid position and store the positions.
After this has been done my aim is to control the motor position via a potentiometer.
If you think DC motor speed control is too complicated then you're not ready for this project. DC motor position control really requires DC motor speed control (or at least keeping the motor at a pretty low speed whether electronically or through gearing) in order to work. It sounds simple, but in all but really trivial cases, it really isn't.
It may be easier to do what you require with a stepper motor.
Even if you count the square waves from the encoder how will you know that you have reached the end stop when you don't know where you started ? The encoder is only any use if you know where you are when you start counting and how many pulses there are per unit of length
I just have to ask, how strong are the gears? Will they strip if an end stop is hit? Will the end stop be able to stop the motor given the gear reduction? Can you time the encoder so it will be between counts when it hits the end stop? The reason for that question is to eliminate the possibility of having one more count going one way that going the other way.
Ok. In a nutshell, the difficulty of doing this really depends on how quickly the motor (with load attached) will stop when you tell it to. A motor/load combination that is heavily geared down and has plenty of torque to control the inertia will be easy to work with. It also depends on how much overshoot you are happy with.
Here's a quick test. Connect the motor in the system you have to a power supply and a switch. Let it run at normal speed then flip the switch off. If the load stops where you want it to stop within your allowable position error, then you will probably be able to get this working with an arduino quickly.
It won't. What you have is not an encoder: it's a revolution sensor!
You can probably do what you want with what you have. Set up the arduino with an interrupt service routine and in that routine count up if you're going one direction and down if you're going the other way. That will tell you where the motor shaft is. That will probably work well enough for what you're trying to do.