DC motor control with a joystick

Hello. I am working on a two-motor joystick controller (pan/tilt)

I have the design working with two potentiometer joystick. This will need to be switched over to a hall effect, but doing baby steps.

What I am needing help with is right now the joystick works by a variable speed as the joystick is used, like normal. I have two switches I need to adjust the speed as well.

So in switch1(A2), I want the motors to turn at one speed, but have the joystick control the direction (forward/reverse) but not the speed, I want to be able to define the speed when switch 1 is high, or Vcc. then have it operate a different speed when switch 2 (A3) is high, or Vcc.

I have enclosed a .docx file that shows the schematic and code.

Arduino joystick DC motor speed and rotation direction control.doc (129 KB)

Please repost your schematic and code as an image and in code tags respectively.

You should checkout the how-to-post sticky threads. .docx is a potential virus/trojan threat,
so savvy users will never open such a file from an untrusted source, and sume users
can't open such a file, nor would they bother.

// Arduino joystick DC motor speed and rotation direction control
 
#define joystick1    A0
#define pwm1         9
#define pwm2         10
#define joystick2    A1
#define pwm3         5
#define pwm4         6
//#define switchfast   A2
//#define switchslow   A3

int motor_control;
 
void setup() {
  pinMode(pwm1,   OUTPUT);
  pinMode(pwm2,   OUTPUT); 
  pinMode(pwm3,   OUTPUT);
  pinMode(pwm4,   OUTPUT); 
  
}
 
void loop() {
   // read the input on analog pin 0:
  // For joystick on/off, fast/slow
 // int sensorValue = analogRead(A2);
 // int sensorValue = analogRead(A3);
  
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  //float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
 // Serial.println(voltage);
  
 // if analogRead(switchfast, high) 
 //   motor_control 
  
  motor_control = analogRead(joystick1);
  motor_control >>= 1;
  if(motor_control > 255){
    digitalWrite(pwm2, 0);
    analogWrite(pwm1, (motor_control - 256));
  }
  else
    if(motor_control < 255){
      digitalWrite(pwm1, 0);
      analogWrite(pwm2, (255 - motor_control));
    }
    else{
      digitalWrite(pwm1, 0);
      digitalWrite(pwm2, 0);
    }
  
  motor_control = analogRead(joystick2);
  motor_control >>= 1;
  if(motor_control > 255){
    digitalWrite(pwm4, 0);
    analogWrite(pwm3, (motor_control - 256));
  }
  else
    if(motor_control < 255){
      digitalWrite(pwm3, 0);
      analogWrite(pwm4, (255 - motor_control));
    }
    else{
      digitalWrite(pwm3, 0);
      digitalWrite(pwm4, 0);
    }
}

Anyone?

Is the joystick output analog or digital? Can you post a link?

I am using a 10k potentiometer joystick

michaelsandborn:
What I am needing help with is right now the joystick works by a variable speed as the joystick is used, like normal. I have two switches I need to adjust the speed as well.

So in switch1(A2), I want the motors to turn at one speed, but have the joystick control the direction (forward/reverse) but not the speed, I want to be able to define the speed when switch 1 is high, or Vcc. then have it operate a different speed when switch 2 (A3) is high, or Vcc.

I can't make sense of your requirement.

The code in Reply #2 seems to control the direction and speed of the motors according to the position of the joystick - is that what actually happens?

Are you saying that you just want to use the joystick to control the direction and that you propose to control the speed with some button switches that are not now in the code in Reply #2?

If so, it seems to me you already have the code for direction control by joystick so what is it that you want help with?

...R

Thanks for the code.

One way to approach this is write some functions to separately control speed and direction, retaining
state as necessary to do this:

int current_drive_level = 0 ;
bool current_direction = false;

void set_direction (bool direction)
{
  current_direction = direction ;
  update_motor () ;
}

void set_speed (int speed)
{
  current_speed = speed ;
  update_motor() ;
}

void update_motor ()
{
  if (current_direction)
  {
    digitalWrite (pwm2, LOW) ;
    analogWrite (pwm1, current_speed) ;
  }
  else
  {
    digitalWrite (pwm1, LOW) ;
    analogWrite (pwm2, current_speed) ;
  }
}

Then you've got separate control, and you handle button presses by calling set_speed(),
and the joystick by calling set_direction().

Note the use of lots of small functions with well-chosen names - this is how you make code
readable, reusable, and flexible to future change.