Motor Control code (not working)

Howdy!

I am working on a personal project where I want to be able to control both the direction and speed of two DC motors using a two axis joystick. My code synthesizes, loads, and runs, but does not turn the motors. In the serial monitor the correct values are displayed, and the values update when I move the joystick, but I still cannot get the motors to turn.

Here is my code:

#include <Servo.h>

int pan_enable = 9;
int panA = 8;
int panB = 11;
int tilt_enable = 10;
int tiltA = 12;
int tiltB = 13;
int zoom_out = 1;
int focus_out = 2;

double pan, x, xp;
double tilt, y, yp;
double zoom;
double focus;

int xoe, xo1, xo2, yoe, yo1, yo2, zo, fo;

Servo zoom_servo;
Servo focus_servo;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  
  zoom_servo.attach(zoom_out);
  focus_servo.attach(focus_out);
  
  //initialize the pin configuration
  pinMode(pan_enable, OUTPUT);
  pinMode(panA, OUTPUT);
  pinMode(panB, OUTPUT);
  pinMode(tilt_enable, OUTPUT);
  pinMode(tiltA, OUTPUT);
  pinMode(tiltB, OUTPUT);
  //pinMode(zoom_out, OUTPUT);
  //pinMode(focus_out, OUTPUT);
  
}

void loop() {
  
  double pan = analogRead(A0); //read joystick for pan
  double tilt = analogRead(A1); //read joystick for tilt
  double zoom = analogRead(A2); //read pot for zoom
  double focus = analogRead(A3); //read pot for focus
 

 if(tilt > 512){ // if joystick is pushed forward
    //y = tilt - 512;
    yo1 = 1;
    yo2 = 0;
    yoe = map(tilt, 512, 1023, 0, 254);
  }
  else if(tilt < 504){ // if joystick is pulled back
    //y = 504 - tilt;
    yo1 = 0;
    yo2 = 1;
    yoe = map(tilt, 504, 0, 0, 254);
 }
 else if(504 < tilt < 512){ // if joystick is in the center
    yoe = 0;
    yo1 = 0;
    yo2 =  0;
  }

  if(pan > 516){ // if joystick is pushed right
    //x = pan - 516;
    xo1 = 1;
    xo2 = 0;
    xoe = map(pan, 516, 1023, 0, 254);
  }
  else if(pan < 508){ // if joystick is pushed left
    //x = 508 - pan;
    xo1 = 0;
    xo2 = 1;
    xoe = map(pan, 508, 0, 0, 254);
  }
  else if(508 < pan < 516){ // if joystick is in the center
    xoe = 0;
    xo1 = 0;
    xo2 = 0;
  }
  
  //xoe = map(x, 0, 507, 0, 254);
  //yoe = map(y, 0, 511, 0, 254);
  
  zo = map(zoom, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  zoom_servo.write(zo);                  // sets the servo position according to the scaled value 

  fo = map(focus, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  focus_servo.write(fo);                  // sets the servo position according to the scaled value 

  digitalWrite(panA, xo1);
  digitalWrite(panB, xo2);
  digitalWrite(tiltA, yo1);
  digitalWrite(tiltB, yo2);

  analogWrite(pan_enable, xoe);
  analogWrite(tilt_enable, yoe);
  
  
  Serial.print("Tilt:    ");
  Serial.print(tilt);
  Serial.print("    ");
  Serial.print(yp);
  Serial.print("%    ");
  Serial.print(yo1);
  Serial.print("    ");
  Serial.print(yo2);
  Serial.print("    ");
  Serial.println(yoe);
  
  Serial.print("Pan:    ");
  Serial.print(pan);
  Serial.print("    ");
  Serial.print(xp);
  Serial.print("%    ");
  Serial.print(xo1);
  Serial.print("    ");
  Serial.print(xo2);
  Serial.print("    ");
  Serial.println(xoe);
  
  
  // delay (800); // only for testing
  delay (100); // allow the changed values to take effect
}

NOTE: I have not tested the part of the code for the servos, because I have not implemented them in the physical design yet.

Should I change the parts of the code that deal with the joystick position to where they are an external function that is called within the main loop? From what I can tell, the code is generating the pwm and direction values correctly, but it is not actually getting the values all the way to the motor shield that is on top of the Arduino.

So far, I have tried implementing just the forward direction for the tilt (pushing the joystick forward) with all of the other code commented out (the joystick pulled back, and the joystick pushed to either side), with no luck. I have also searched for similar control code on Arduino’s site and third party sites, but can only find examples of controlling a single motor speed and only in one direction.

I am using an Arduino Uno, with an external motor shield from SeeedStudio. The motor shield takes 6 inputs (x1, x2, xpwm, y1, y2, ypwm), and includes its own power supply (so that I am not trying to source any currents out of the Arduino)

  analogWrite(pan_enable, xoe);
  analogWrite(tilt_enable, yoe);

Why are you writing PWM values to the enable pins? The enable pins should control direction.

The motor shield that I am using takes 6 inputs. The two pins that are labeled 'enable' in my code are used to control the speed of the motors (or at least its supposed to). The other four pins are split between the two motors, which control the direction of the motor (ex. P1=HIGH P2=LOW => CCW)

The other issue with applying the pwm to the other pins is that some of them are not pwm pins (specifically pin 8 and 12). I can't change the pins, or the way that they are assigned, because the motor shield requires them to be that way.

Can you clarify whether the problem is about motor control or SERVO control? I see lots of code dealing with servos in that sketch and I'm not clear whether your problem is with that, or whether you're also trying to drive DC motors.

If you're mixing servos with PWM motor control then you need to read the documentation for the servo library carefully because it disables PWM output on some pins and some motor drive shields use the affected pins.

I suggest that if you want any further help you start by writing a sketch that does nothing but initialise whatever motor/servo device you've having trouble with and does some simple hard-coded sequence of actions with it.

PeterH,
Sorry for the confusion. The problem was with the motor control, not the servo control. After seeing your post, I tried removing the servo portions of the code, and that solved the problem. I did not know that the servo library disables certain pwm pins. Thank you for your help and insight.
-H

After seeing your post, I tried removing the servo portions of the code, and that solved the problem.

If you can use other pins (by bending shield pins and soldering jumper wires, if necessary), you could control the motor shield using two other PWM pins. 9 and 10 are disabled first, and those seem to be the two most popular pins for shield makers to hardwire to. Some designers think you will never need to use servos, apparently.