Hi Everyone,
I have just started my very first project using Arduino. Quite excited!
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
If you need additional info or have any questions, please let me know.