potentio -- speed and direction DC motor

Hi Everyone,

I have just started my very first project using Arduino. Quite excited! :slight_smile:
I would like to have a single axis joystick control the speed and direction of a DC motor.
Considering I have never built a project on my own, and I don't have a lot of experience writing C, a forum like this comes in very handy.

Currently I am trying to figure out the better way to have the potentio control direction and speed.

I scrapped an XY joystick pad, containing 2 potentiometers.
I have hooked one of them up to the arduino (as this represents the single axis joystick I'll need to get later on).
Pin 1 and three of the potentiometer are hooked to 5v and ground on the arduino.
Pin 2 (middle pin) is hooked to Analog input AO.
I will use PWM to drive the H-bridge driving the DC motor.

The serial monitor in the Arduino software reveals that the middle point of the potentio never really is the same, after each movements it falls back to a slightly different position. And the readout isn't very stable either. so chances are I may better look for another type of component. (center point averages out in a range of 450-600.).
--Any alternate ideas I should check out?

So I coded a treshold L (below pointx) and treshold R (below pointy) in setup.
And in Loop I have coded the following three if statements

void loop ()
{
int potValue = analogRead(A0);
Serial.println(potValue); //schrijven naar de seriele monitor

if (potValue < thresleft) {
analogWrite(motorpinL,(thresleft-potValue/2));
// slaag er niet in te inverteren.
}

if (potValue > thresright) {
analogWrite(motorpinR,(potValue-thresright)/2);
}

if (potValue < thresright & potValue < thresleft) {
analogWrite(motorpinR,0);
}

Btw : I am using leds for testing. So I am using two pins (motorpinL and R) on which I am sending out the PWM signal.
In the final code 1 pin will be used to drive the motor and the other pin will just state a logic high or low to indicate direction to the H-brigde.

Currently the biggest problem seems to be

  • finding decent code the 'invert' the readout of the potentio below the middle point, as this doesn't seem to work quite well.

Quite possible there is a whole other and easier way to tackle this one :slight_smile:

If you need additional info or have any questions, please let me know.

It's common for potentiometers to give some variation. Your code should allow for that by ignoring small differences.

These two pieces of code are not equivalent

(thresleft-potValue/2)
(potValue-thresright)/2

You haven't posted your entire code so I have no idea what motorPinR is.

Do you not need to set both sides of the h-bridge for both directions - perhaps one side set to 0 and the other side receiving the PWM signal?

And please post you code within code tags as requested at the top of the forum.

...R

Hello Robin, thanks for replying.

this is the entire code

//potentio stuurt de PWM uitgang van de arduino aan.

int pot = 0;            // waarde van de potentiometer
int potValue = 0;       //
int motorpinL = 11;      // uitgangspinnetje
int motorpinR = 10;      // uitgangspinnetje 

int brightness = 0;     // uitsturing led
int thresleft = 300;    // treshold left movement
int thresright = 800;   // treshold right movement 

void setup ()
{
  Serial.begin(9600);    // communicatie met de seriele poort
  pinMode (pot, INPUT);        
  pinMode (motorpinL, OUTPUT);
  pinMode (motorpinR, OUTPUT);
}

void loop ()
{
  int potValue = analogRead(A0);  
  Serial.println(potValue);        //schrijven naar de seriele monitor
  
  if (potValue < thresleft) {
    analogWrite(motorpinL,(thresleft-potValue/2));
    // slaag er niet in te inverteren.
  }

  if (potValue > thresright) {
    analogWrite(motorpinR,(potValue-thresright)/2); 
  }
  
   if (potValue < thresright & potValue < thresleft) {
    analogWrite(motorpinR,0); 
  }

}

As you'll notive I have put up a centre area in which the output remains at zero. This is big and I wish to improve performance here.
As you also have noticed both pieces of code aren't equivalent, but this is needed as pushing the joystich further of center on either side should allow a higher output level. But considering the big offset of the center area there still are measurements that go beyond the put tolerance and will cause negative values.

Also at this moment there isn't a good balance between the left and right side, but for now I wish to focus on getting the general idea right.

Sorry. I never noticed the stuff below your code in your first post.

I think I would approach the problem a little differently. I would first of all test whether the pot is in the Left or Right section and call the appropriate function rather than try to do it all together. Something like this pseudo code. I know this doesn't have an idle space around the threshold - but it should be easy to add that, perhaps by manipulating potVal before it is used.

When I'm writing my own code I like to do it in simple steps so there is no confusion about what I'm doing and it also makes it easy to add a Serial.println() to show any of the intermediate values.

potVal = analogRead()

if (potVal > thres) {
   motion = 'L';
}
else {
   motion = 'R';
}

speed = abs(potVal - thresh);

if (motion == 'L') {
   analogWrite(motorpinL,speed);
}
else {
   analogWrite(motorpinR,speed);
}

...R

Hi Robin,

Thanks again for replying. Just found some extra time to try out your code.
It makes sense, I understand what you mean with the pseudo code.
I'll let you know where I end up.

Hi again,

It tried your code, and it works like a charm. adjusted it a little (dividid by two again to convert to the 256 PWM output
Ofcourse there is still a small issue with the potentio not returning to zero at the exact center, but it's behaviour has improved.
Now I'm controlling LEDS, but eventually the output will drive a H-bridge, so Once I hook it up along with the DC Motor; I'll see how it behaves and what additional finetuning is needed.

If anyone has an alternate way to code this? Please drop your ideas, always welcome.

Now I'll add the code to program the additional steering.
Don't know If i've mentioned before, but I'm planning on building my own railcam system.

So I'll need a one axis joystick that controls a DC motor (left and right speed sensitive)
Which is working at the moment.

And I'll add a pan and tilt movenment, which is also PWM output that must control two servo's.
So there's not a whole lot to it anymore I guess. But considering I'm new at code, there'll be some bumps along I cannot predict. :slight_smile:

I'll update the post asap.
Oh yeah, If this comes along. I'll need to add RF tx:/rx and think it'll be cool to add a component video feedback op LCD.
But that's miles away,.. one step at a time for now.

Thanks Robin, sure helped me a lot!

If you want to extend the project to other actions you may be interested in this demo code that I wrote

http://forum.arduino.cc/index.php?topic=223286.0

...R