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);
}
}
}
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
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.
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!