Speed control not working correctly with Potentiometer

Hey everyone I hope this is the right thread Anyway my setup is two DC motors Connected to an Adafruit H-Bridge then I have two pots connected to A4 and A5 which send their analogue values to control both speed and direction. This works with the code but somehow when the pot is close to centre the motors spin fast then as the pot value increases it becomes slower. This is not what I'm wanting obviously id prefer the speed to increase as it travels further from the centre point
Is anyone able to assist me in how this is happening?

#include <AFMotor.h>

AF_DCMotor motor1(4);
AF_DCMotor motor4(1);

const int y = A4;
const int x = A5;

int y1 = 0; //speed of y "pitch" axis
int y2 = 0; //Raw Anaogue in of y Pot
int x1 = 0; //Speed of x "yaw" axis
int x2 = 0; //Raw analogue in of x pot

void setup()
{
    pinMode(y2, INPUT);
    pinMode(x2, INPUT);
    // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop()
{
  //Motor Directions as Specified in AFMotor.h
   int a = FORWARD; 
   int b = BACKWARD;
   int c = RELEASE; //Motor brake

   {
   
  y2 = analogRead(y);
  y1 = map(y2, 0, 1023, 0, 255);
  
   //Point down
   if(y1 < 120)
   {
    motor4.setSpeed(y2);
    motor4.run(b);
   
   }
   //Point Up
    else if (y1 > 170)
    {
      motor4.setSpeed(y2);
      motor4.run(a);
 
    }
    //Brake
    else {
      motor4.setSpeed(0);
      motor4.run(c);

    }
    {
      x2 = analogRead(x);
      x1 = map(x2, 0, 1023, 0, 255);

         //Point down
   if(x1 < 120)
   {
    motor1.setSpeed(x2);
    motor1.run(b);
   
   }
   //Point Up
    else if (x1 > 170)
    {
      motor1.setSpeed(x2);
      motor1.run(a);
 
    }
    //Brake
    else {
      motor1.setSpeed(0);
      motor1.run(c);
    }
// print the results to the serial monitor:
  Serial.print("\t y In = ");
  Serial.print(y2);
  Serial.print("\t y Out = ");
  Serial.println(y1);
  Serial.print("\t x In = ");
  Serial.print(x2);
  Serial.print("\t x Out = ");
  Serial.print(x1);

  delay(2);

      
    }
      
    }
}

Hi
,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

So we can see how you have connected your pots.

Thanks.. Tom.. :slight_smile:

Hi,
If this is slow down?

   //Point down
   if(y1 < 120)
   {
    motor4.setSpeed(y2);
    motor4.run(b);
   
   }

If this is reverse, you are starting at PWM of 120 at your pot zero speed position, then decreasing 120 to 0, as you increase your reverse speed.

Tom... :slight_smile:

Hi,
Just noticed, you should be analogWrite y1, your mapped value not your raw analogRead value.

Tom.. :slight_smile:
Just about to have first coffee.

Ignore the power source obviously not using AA's but makes sense

TomGeorge:
Hi,
Just noticed, you should be analogWrite y1, your mapped value not your raw analogRead value.

Tom.. :slight_smile:
Just about to have first coffee.

so the mapped values put the mid point around 130-140

so with this should I re map then analogueWrite at a different range to read off?

Hi,
What is the midpoint from your analogRead, not all potentiometers are the same or exactly linear.

Tom... :slight_smile:
Your y2 goes from 0 to 1023, so midpoint should be 512 if perfect pots.

Hi,

//Point down
   if(y1 < 120)
   {
    motor4.setSpeed(y2);
    motor4.run(b);
   
   }

needs to be

//Point down
   if(y1 < 120)
   {
    motor4.setSpeed(120-y1);
    motor4.run(b);
  
   }

Hope it works..Tom.. :slight_smile:

The pots are ofcourse 10kB (lineair). Not type A or C (log).
Leo..

TomGeorge:
Hi,

//Point down

if(y1 < 120)
  {
    motor4.setSpeed(y2);
    motor4.run(b);
 
  }



needs to be


//Point down
  if(y1 < 120)
  {
    motor4.setSpeed(120-y1);
    motor4.run(b);
 
  }



Hope it works..Tom.. :)

works one way! so then for the other way would it be 120+y1 or 170+y1?

Wawa:
The pots are ofcourse 10kB (lineair). Not type A or C (log).
Leo..

Thanks Leo yeah thats true and if theres some way to make this a Quadratic curve rather than linear and then read these values but is there a mathamatical function we can input into the sketch to run and read.

tpruszin:
...if theres some way to make this a Quadratic curve rather than linear...

Maybe.
Try a 10k resistor from wiper to +5volt, and another 10k resistor from wiper to ground.
This voltage divider makes the center position of the pot broader.
Leo..

Wawa:
Maybe.
Try a 10k resistor from wiper to +5volt, and another 10k resistor from wiper to ground.
This voltage divider makes the center position of the pot broader.
Leo..

Just tried it still doesn't solve the problem thanks tho

Maybe you need lower values.
Try 4k7 or even down to 1k.

Try values of 120 and 136 in a program that just prints the values to the serial monitor before you try to control a motor with it.
Leo..

Hi,
Are your pots linear?
What do you get from analogRead for fully clockwise, fully counter clockwise and in the physical middle position of the pot?

Thanks.. Tom... :slight_smile:

Hey Tom,
Yeah they appear Linear from theValues Printed in Serial Print I get
Y min 24 mid 534 max 986
X min 14 mid 580 max 1022

Thomas

Hi,
What pots are you using, will they be the final one in your project?

If they are the small trimming type pots, them out and get decent ones like these.



The small trimpot type are not very consistent in their characteristics.

Tom... :slight_smile:

Hmm took it out of its housing (was in a pre assembled Gimbal that I tore out of a RC Controller for this project) looks to be a 5k I presume so now knowing this should I then get a different type of Pot e.g >10k resistance to be using to provide more accurate results?

Hi
Okay, if it came from a joystick, its a good size but the electrical characteristics sound lousy. lol
You should have left it in the gimbol, would have been good assembly for remote.
It will be linear.
You will have to trim your code for each pot , to allow for different centre zero values.

Tom.. :slight_smile:
(I'm off to bed, work in the morning.. :sleeping: )

Ah cool thanks for that its easy to put it back into the gimbal so I can use it to control the project easily so thats no matter I guess then ill just take it to the electronic store and see what they have similar size but higher resistance Thanks for all your help!