Gamepad Code And Servo Issues

Good evening all,
I'm working on a 1sheeld controlled robot using my phone accelerometers to control throttle and steering and the gamepad to control the servos. I can get the servos to run using the coloured gamepad buttons but when I connect the servos to the Uno outputs assigned to the gamepad arrows pin 5 makes the servo go straight up and 6 makes it go straight down without pressing any buttons. I'm sure it's an error in the code but I'm not sure where. Please can you assist with this. If there is anything else that can be improved in the coding please let me know.

> #define CUSTOM_SETTINGS
> #define INCLUDE_GAMEPAD_SHIELD
> #define INCLUDE_LED_SHIELD
> #define INCLUDE_ACCELEROMETER_SENSOR_SHIELD
> #define INCLUDE_SLIDER_SHIELD
> 
> 
> float x, y, z;
> 
> 
> /* Include 1Sheeld library. */
> #include <OneSheeld.h>
> #include <AFMotor.h>
> /* include Servo library */
> #include <Servo.h>
> /* A name for the LED on pin 7. */
> const int ledPin = 13;
> 
> 
> 
> /* Pin configuration of the Seeedstudio's motor shield. */
> int motorAPin1 = A0;
> int motorAPin2 = A1;
> int motorBPin1 = A2;
> int motorBPin2 = A3;
> //int motorASpeedPin = 9;
> //int motorBSpeedPin = 10;
> // Making servo objects
> 
> Servo shoulder;
> 
> Servo hand;
> 
> Servo grab;
> 
> Servo spare;
> 
> // Global variables for initializing the servo motors positions
> 
> int shoulderPos=90;
> 
> int handPos=90;
> 
> int grabPos=90;
> 
> int sparePos=(90);
> 
> /* Define a variable to hold the value of the slider. */
> int value;
> Servo servo;
> 
> void setup()
> {
>   /* Start communication. */
>   OneSheeld.begin();
> 
>   // Attach servo motors
> 	// Shoulder on 9, hand on 10, grab on 11 spare on 6, slider on 5.
> 
> 	shoulder.attach(9);
> 
> 	hand.attach(10);
> 
>   grab.attach(11);
> 
>   spare.attach(6);
> 
>   servo.attach(5); //servo pin
> 
>   	// The LED just works as an indicator that the 1Sheeld is working fine
> 	pinMode(ledPin, OUTPUT);
>   /* Set the LED pin as output. */
> 
> 
>   /* Seeedstudio's motor shield initialization. */
>   pinMode(motorAPin1, OUTPUT);    // IN1 of motor A
>   pinMode(motorAPin2, OUTPUT);    // IN2 of motor A
>   pinMode(motorBPin1, OUTPUT);    // IN3 of motor B
>   pinMode(motorBPin2, OUTPUT);    // IN4 of motor B
>  // pinMode(motorASpeedPin, OUTPUT); // Speed of motor A
>   //pinMode(motorBSpeedPin, OUTPUT); // Speed of Motor B
> //motorA.setSpeed(250);
> //motorB.setSpeed(250);
> }
> 
> void loop()
> 
> {/* Always get the value of the slider and output it as PWM to pin 5. */
>   value = Slider.getValue();
>   servo.write(value);
>   
>   // Controlling shoulder
> 	// Orange button moves it forward and green moves it backward
> 	if(GamePad.isGreenPressed()) {
> 		shoulderPos+=3;
> 	} else if(GamePad.isOrangePressed()) {
> 		shoulderPos-=3;
> 	}
> 	// Controlling hand
> 	// Red closes the hand and blue opens it
> 	if(GamePad.isRedPressed()) {
> 		handPos-=3;
> 	} else if(GamePad.isBluePressed()) {
> 		handPos+=3;
>   }
> 	 
>   //Controlling grab
>   // Up closes the Grab and down opens it
> 	if(GamePad.isUpPressed()) {
> 		grabPos-=3;
> 	} else if(GamePad.isDownPressed()) {
> 		grabPos+=3;
>     }
> 	// Controlling spare
> 	// Left closes the hand and right opens it
> 	if(GamePad.isLeftPressed()) {
> 		sparePos-=3;
> 	} else if(GamePad.isRightPressed()) {
> 		sparePos+=3;
> 	}
> 	// Move the servo motors to it's new positions
> 
> 	shoulder.write(shoulderPos);
> 
> 	hand.write(handPos);
> 
>   grab.write(grabPos);
> 
>   spare.write(sparePos);
>   // Delay before run again
> 	delay(20);
> 
> 
>  
> x=AccelerometerSensor.getX();
>   y=AccelerometerSensor.getY();
>   z=AccelerometerSensor.getZ();
> 
>   /* If up is pressed, move the car forward. */
> if (y < -5)
>   {
>    // analogWrite(motorASpeedPin, 255);
>     //analogWrite(motorBSpeedPin, 255);
>     digitalWrite(motorAPin1, LOW);
>     digitalWrite(motorAPin2, HIGH);
>     digitalWrite(motorBPin1, LOW);
>     digitalWrite(motorBPin2, HIGH);
>     digitalWrite(ledPin, HIGH);
> 
>   }
>   /* If down is pressed, move the car backwards. */
>   else if (y > 6)
>   {
>     //analogWrite(motorASpeedPin, 255);
>     //analogWrite(motorBSpeedPin, 255);
>     digitalWrite(motorAPin1, HIGH);
>     digitalWrite(motorAPin2, LOW);
>     digitalWrite(motorBPin1, HIGH);
>     digitalWrite(motorBPin2, LOW);
>     digitalWrite(ledPin, HIGH);
> 
> 
>   }
>   /* If right is pressed, turn the car to the right. */
>   else if (x < -6)
>   {
>     //analogWrite(motorASpeedPin, 255);
>     //analogWrite(motorBSpeedPin, 255);
>     digitalWrite(motorAPin1, LOW);
>     digitalWrite(motorAPin2, HIGH);
>     digitalWrite(motorBPin1, HIGH);
>     digitalWrite(motorBPin2, LOW);
>     digitalWrite(ledPin, HIGH);
> 
>   }
>   /* If left is pressed, turn the car to the left. */
>   else if (x > 6)
>   {
>     //analogWrite(motorASpeedPin, 255);
>     //analogWrite(motorBSpeedPin, 255);
>     digitalWrite(motorAPin1, HIGH);
>     digitalWrite(motorAPin2, LOW);
>     digitalWrite(motorBPin1, LOW);
>     digitalWrite(motorBPin2, HIGH);
>     digitalWrite(ledPin, HIGH);
> 
>   }
>   /* If nothing is pressed stop all motors. */
>   else
>   {
>     //analogWrite(motorASpeedPin, 0);
>     //analogWrite(motorBSpeedPin, 0);
>     digitalWrite(motorAPin1, LOW);
>     digitalWrite(motorAPin2, LOW);
>     digitalWrite(motorBPin1, LOW);
>     digitalWrite(motorBPin2, LOW);
>     digitalWrite(ledPin, LOW);
>   }
> 
> }

I don't see where you are using pin 7. Your grab and spare servos are using pins 11 and 6, respectively.

I also don't see where you are limiting the values for the servo angles to the range 0 to 180.

How are your servos powered?

Apologies, it should have been 5 & 6. I've edited the original post now. I'm running a sea
separate power supply and testing them one at a time. The others seem to work fine.

No worries. Good answer on suppIying the servos separately. I would suggest using the serial console to print some debug statements to track your button pushes and servo angle values. That's the only help I can give right now.

Thanks ToddL1962, I've swapped some of the code around and the servos on 9,10 & 11 are working, the other 2 are still acting up no matter what pins I use.
I'm new to this, can you give me some pointers on setting the servo limits and maybe a servo control using the 1sheeld gamepad please.

#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_SHIELD
#define INCLUDE_LED_SHIELD
#define INCLUDE_ACCELEROMETER_SENSOR_SHIELD
#define INCLUDE_SLIDER_SHIELD


float x, y, z;


/* Include 1Sheeld library. */
#include <OneSheeld.h>
#include <AFMotor.h>
/* include Servo library */
#include <Servo.h>
/* A name for the LED on pin 7. */
const int ledPin = 13;



/* Pin configuration of the Seeedstudio's motor shield. */
int motorAPin1 = A0;
int motorAPin2 = A1;
int motorBPin1 = A2;
int motorBPin2 = A3;
//int motorASpeedPin = 9;
//int motorBSpeedPin = 10;
// Making servo objects

Servo shoulder;

Servo hand;

Servo grab;

Servo spare;

// Global variables for initializing the servo motors positions

int shoulderPos=90;

int handPos=110;

int grabPos=80;

int sparePos=(90);

/* Define a variable to hold the value of the slider. */
int value;
Servo servo;

void setup()
{
  /* Start communication. */
  OneSheeld.begin();

  // Attach servo motors
	// Shoulder on 9, hand on 10, grab on 11 spare on 6, slider on 5.

	shoulder.attach(9);

	hand.attach(10);

  grab.attach(11);

  spare.attach(7);

  servo.attach(8); //servo pin

  	// The LED just works as an indicator that the 1Sheeld is working fine
	pinMode(ledPin, OUTPUT);
  /* Set the LED pin as output. */


  /* Seeedstudio's motor shield initialization. */
  pinMode(motorAPin1, OUTPUT);    // IN1 of motor A
  pinMode(motorAPin2, OUTPUT);    // IN2 of motor A
  pinMode(motorBPin1, OUTPUT);    // IN3 of motor B
  pinMode(motorBPin2, OUTPUT);    // IN4 of motor B
 // pinMode(motorASpeedPin, OUTPUT); // Speed of motor A
  //pinMode(motorBSpeedPin, OUTPUT); // Speed of Motor B
//motorA.setSpeed(250);
//motorB.setSpeed(250);
}

void loop()

{/* Always get the value of the slider and output it as PWM to pin 5. */
  value = Slider.getValue();
  servo.write(value);
  
  
	 
  //Controlling grab
  // Up closes the Grab and down opens it
	if(GamePad.isBluePressed()) {
		grabPos-=3;
	} else if(GamePad.isRedPressed()) {
		grabPos+=3;
    }
	// Controlling spare
	// Left closes the hand and right opens it
	if(GamePad.isLeftPressed()) {
		sparePos-=3;
	} else if(GamePad.isRightPressed()) {
		sparePos+=3;


	}

// Controlling shoulder
	// Orange button moves it forward and green moves it backward
	if(GamePad.isOrangePressed()) {
		shoulderPos+=3;
	} else if(GamePad.isGreenPressed()) {
		shoulderPos-=3;
	}
	// Controlling hand
	// Red closes the hand and blue opens it
	if(GamePad.isUpPressed()) {
		handPos-=3;
	} else if(GamePad.isDownPressed()) {
		handPos+=3;
  }  
	// Move the servo motors to it's new positions

	shoulder.write(shoulderPos);

	hand.write(handPos);

  grab.write(grabPos);

  spare.write(sparePos);
  // Delay before run again
	delay(20);


 
x=AccelerometerSensor.getX();
  y=AccelerometerSensor.getY();
  z=AccelerometerSensor.getZ();

  /* If up is pressed, move the car forward. */
if (x < -5)
  {
   // analogWrite(motorASpeedPin, 255);
    //analogWrite(motorBSpeedPin, 255);
    digitalWrite(motorAPin1, LOW);
    digitalWrite(motorAPin2, HIGH);
    digitalWrite(motorBPin1, LOW);
    digitalWrite(motorBPin2, HIGH);
    digitalWrite(ledPin, HIGH);

  }
  /* If down is pressed, move the car backwards. */
  else if (x > 6)
  {
    //analogWrite(motorASpeedPin, 255);
    //analogWrite(motorBSpeedPin, 255);
    digitalWrite(motorAPin1, HIGH);
    digitalWrite(motorAPin2, LOW);
    digitalWrite(motorBPin1, HIGH);
    digitalWrite(motorBPin2, LOW);
    digitalWrite(ledPin, HIGH);


  }
  /* If right is pressed, turn the car to the right. */
  else if (y < -6)
  {
    //analogWrite(motorASpeedPin, 255);
    //analogWrite(motorBSpeedPin, 255);
    digitalWrite(motorAPin1, LOW);
    digitalWrite(motorAPin2, HIGH);
    digitalWrite(motorBPin1, HIGH);
    digitalWrite(motorBPin2, LOW);
    digitalWrite(ledPin, HIGH);

  }
  /* If left is pressed, turn the car to the left. */
  else if (y > 6)
  {
    //analogWrite(motorASpeedPin, 255);
    //analogWrite(motorBSpeedPin, 255);
    digitalWrite(motorAPin1, HIGH);
    digitalWrite(motorAPin2, LOW);
    digitalWrite(motorBPin1, LOW);
    digitalWrite(motorBPin2, HIGH);
    digitalWrite(ledPin, HIGH);

  }
  /* If nothing is pressed stop all motors. */
  else
  {
    //analogWrite(motorASpeedPin, 0);
    //analogWrite(motorBSpeedPin, 0);
    digitalWrite(motorAPin1, LOW);
    digitalWrite(motorAPin2, LOW);
    digitalWrite(motorBPin1, LOW);
    digitalWrite(motorBPin2, LOW);
    digitalWrite(ledPin, LOW);
  }

}

Here is how to limit servo angle for one servo. Of course, it is the same for the others.

	if(GamePad.isBluePressed()) {
		grabPos-=3;
		if (grabPos < 0) grabPos = 0;
	} else if(GamePad.isRedPressed()) {
		grabPos+=3;
		if (grabPos > 180) grabPos = 180;
    }

I can't really debug from here. Did you add Serial.println() calls in order to debug using the serial console?

Thanks ToddL1962, I'll try out the servo limits next time. Good point about the "serial print" I'll have to add it in as nothing showed in the serial monitor. Thanks again.

Just two hopefully quick questions,

I'm trying to use the serial read and serial print commands to tell me the servo positions before I try to set the limits. I'm using hand.read(); &
Serial.println(hand.read()); after each servo operation but it only outputs ???? to the serial monitor. How do I rectify this. Once I get the servo position information from this, is it just a case of changing the code to something like this.

if(GamePad.isUpPressed()) {
		handPos-=3;
    if (handPos < 45) handPos = 45;
	} else if(GamePad.isDownPressed()) {
		handPos+=3;
    if (handPos > 135) handPos = 135;
        hand.read();
    Serial.println(hand.read());

Hopefully this will stop the servo moving outside 45 and 135 degrees.

hand.read() returns an int of the servo angle so that should be fine.

Did you do a Serial.begin() with the baud rate in setup()? Do you have the serial monitor set to the same baud rate?

Post the entire sketch of your latest changes.

Good evening,
This is were I am at the moment, I haven't had a chance to change the servo end stops yet. I've checked the baud rate and they are both set at 9600.

> > ```
> > #define CUSTOM_SETTINGS
> > #define INCLUDE_GAMEPAD_SHIELD
> > #define INCLUDE_LED_SHIELD
> > #define INCLUDE_ACCELEROMETER_SENSOR_SHIELD
> > #define INCLUDE_SLIDER_SHIELD
> > 
> > 
> > float x, y, z;
> > 
> > 
> > /* Include 1Sheeld library. */
> > #include <OneSheeld.h>
> > #include <AFMotor.h>
> > /* include Servo library */
> > #include <Servo.h>
> > /* A name for the LED on pin 7. */
> > const int ledPin = 13;
> > 
> > 
> > 
> > /* Pin configuration of the Seeedstudio's motor shield. */
> > int motorAPin1 = A0;
> > int motorAPin2 = A1;
> > int motorBPin1 = A2;
> > int motorBPin2 = A3;
> > //int motorASpeedPin = 9;
> > //int motorBSpeedPin = 10;
> > // Making servo objects
> > 
> > Servo shoulder;
> > 
> > Servo hand;
> > 
> > Servo grab;
> > 
> > Servo spare;
> > 
> > // Global variables for initializing the servo motors positions
> > 
> > int shoulderPos=90;
> > 
> > int handPos=110;
> > 
> > int grabPos=80;
> > 
> > int sparePos=(90);
> > 
> > /* Define a variable to hold the value of the slider. */
> > int value;
> > Servo servo;
> > 
> > void setup()
> > {
> >     Serial.begin(9600); // open the serial port at 9600 bps:
> >   /* Start communication. */
> >   OneSheeld.begin();
> > 
> >   // Attach servo motors
> > 	// Shoulder on 9, hand on 10, grab on 11 spare on 6, slider on 5.
> > 
> > 	shoulder.attach(9);
> > 
> > 	hand.attach(10);
> > 
> >   grab.attach(11);
> > 
> >   spare.attach(8);
> > 
> >   servo.attach(7); //servo pin
> > 
> >   	// The LED just works as an indicator that the 1Sheeld is working fine
> > 	pinMode(ledPin, OUTPUT);
> >   /* Set the LED pin as output. */
> > 
> > 
> >   /* Seeedstudio's motor shield initialization. */
> >   pinMode(motorAPin1, OUTPUT);    // IN1 of motor A
> >   pinMode(motorAPin2, OUTPUT);    // IN2 of motor A
> >   pinMode(motorBPin1, OUTPUT);    // IN3 of motor B
> >   pinMode(motorBPin2, OUTPUT);    // IN4 of motor B
> >  // pinMode(motorASpeedPin, OUTPUT); // Speed of motor A
> >   //pinMode(motorBSpeedPin, OUTPUT); // Speed of Motor B
> > //motorA.setSpeed(250);
> > //motorB.setSpeed(250);
> > }
> > 
> > void loop()
> > 
> > {/* Always get the value of the slider and output it as PWM to pin 5. */
> >   value = Slider.getValue();
> >   servo.write(value);
> >   
> >   
> > 	 
> >   //Controlling grab
> >   // Up closes the Grab and down opens it
> > 	if(GamePad.isBluePressed()) {
> > 		grabPos-=3;
> >     if (grabPos < 0) grabPos = 0;
> > 	} else if(GamePad.isRedPressed()) {
> > 		grabPos+=3;
> >     if (grabPos > 180) grabPos = 180;
> >     grab.read();
> >     Serial.println(grab.read());
> > 
> >     }
> > 	// Controlling spare
> > 	// Left closes the hand and right opens it
> > 	if(GamePad.isLeftPressed()) {
> > 		sparePos-=3;
> >     if (sparePos < 0) grabPos = 0;
> > 	} else if(GamePad.isRightPressed()) {
> > 		sparePos+=3;
> > if (sparePos > 180) sparePos = 180;    
> >     spare.read();
> >     Serial.println(spare.read());
> > 
> > 	}
> > 
> > // Controlling shoulder
> > 	// Orange button moves it forward and green moves it backward
> > 	if(GamePad.isOrangePressed()) {
> > 		shoulderPos+=3;
> >     if (shoulderPos > 180) shoulderPos = 180;
> > 	} else if(GamePad.isGreenPressed()) {
> > 		shoulderPos-=3;
> >     if (shoulderPos < 0) shoulderPos = 0;
> >         shoulder.read();
> >     Serial.println(shoulder.read());
> > 	}
> > 	// Controlling hand
> > 	// Red closes the hand and blue opens it
> > 	if(GamePad.isUpPressed()) {
> > 		handPos-=3;
> >     if (handPos < 0) handPos = 0;
> > 	} else if(GamePad.isDownPressed()) {
> > 		handPos+=3;
> >     if (handPos > 180) handPos = 180;
> >         hand.read();
> >     Serial.println(hand.read());
> >   }  
> > 	// Move the servo motors to it's new positions
> > 
> > 	shoulder.write(shoulderPos);
> > 
> > 	hand.write(handPos);
> > 
> >   grab.write(grabPos);
> > 
> >   spare.write(sparePos);
> >   // Delay before run again
> > 	delay(20);
> > 
> > 
> >  
> > x=AccelerometerSensor.getX();
> >   y=AccelerometerSensor.getY();
> >   z=AccelerometerSensor.getZ();
> > 
> >   /* If up is pressed, move the car forward. */
> > if (x < -5)
> >   {
> >    // analogWrite(motorASpeedPin, 255);
> >     //analogWrite(motorBSpeedPin, 255);
> >     digitalWrite(motorAPin1, LOW);
> >     digitalWrite(motorAPin2, HIGH);
> >     digitalWrite(motorBPin1, LOW);
> >     digitalWrite(motorBPin2, HIGH);
> >     digitalWrite(ledPin, HIGH);
> > 
> >   }
> >   /* If down is pressed, move the car backwards. */
> >   else if (x > 6)
> >   {
> >     //analogWrite(motorASpeedPin, 255);
> >     //analogWrite(motorBSpeedPin, 255);
> >     digitalWrite(motorAPin1, HIGH);
> >     digitalWrite(motorAPin2, LOW);
> >     digitalWrite(motorBPin1, HIGH);
> >     digitalWrite(motorBPin2, LOW);
> >     digitalWrite(ledPin, HIGH);
> > 
> > 
> >   }
> >   /* If right is pressed, turn the car to the right. */
> >   else if (y < -6)
> >   {
> >     //analogWrite(motorASpeedPin, 255);
> >     //analogWrite(motorBSpeedPin, 255);
> >     digitalWrite(motorAPin1, LOW);
> >     digitalWrite(motorAPin2, HIGH);
> >     digitalWrite(motorBPin1, HIGH);
> >     digitalWrite(motorBPin2, LOW);
> >     digitalWrite(ledPin, HIGH);
> > 
> >   }
> >   /* If left is pressed, turn the car to the left. */
> >   else if (y > 6)
> >   {
> >     //analogWrite(motorASpeedPin, 255);
> >     //analogWrite(motorBSpeedPin, 255);
> >     digitalWrite(motorAPin1, HIGH);
> >     digitalWrite(motorAPin2, LOW);
> >     digitalWrite(motorBPin1, LOW);
> >     digitalWrite(motorBPin2, HIGH);
> >     digitalWrite(ledPin, HIGH);
> > 
> >   }
> >   /* If nothing is pressed stop all motors. */
> >   else
> >   {
> >     //analogWrite(motorASpeedPin, 0);
> >     //analogWrite(motorBSpeedPin, 0);
> >     digitalWrite(motorAPin1, LOW);
> >     digitalWrite(motorAPin2, LOW);
> >     digitalWrite(motorBPin1, LOW);
> >     digitalWrite(motorBPin2, LOW);
> >     digitalWrite(ledPin, LOW);
> >   }
> > 
> > }

````Preformatted text`

I indented the following code snippet to better visualize what is happening. Notice that you will only print the shoulder position if you press the green button. Indeed, you are printing the shoulder position before the servo has been moved to the next position. Also the line 'shoulder.read();' does nothing to contribute to the functionality of the program because you never assign it to a value.

  if (GamePad.isOrangePressed()) {
    shoulderPos += 3;
    if (shoulderPos > 180) shoulderPos = 180;
  } else if (GamePad.isGreenPressed()) {
    shoulderPos -= 3;
    if (shoulderPos < 0) shoulderPos = 0;
    shoulder.read();
    Serial.println(shoulder.read());
  }

Here is how I would code the debug statements. You will have feedback when the buttons are pressed and also see exactly what the servo angles are. If that is verified and you still have issues with the servos you probably have a power or wiring issue.

  //Controlling grab
  // Up closes the Grab and down opens it
  if (GamePad.isBluePressed()) {
    Serial.println("Blue Pressed");
    grabPos -= 3;
    if (grabPos < 0) grabPos = 0;
    Serial.print("grabPos = ");
    Serial.println(grabPos);
  } else if (GamePad.isRedPressed()) {
    Serial.println("Red Pressed");
    grabPos += 3;
    if (grabPos > 180) grabPos = 180;
    Serial.print("grabPos = ");
    Serial.println(grabPos);
  }

  // Controlling spare
  // Left closes the hand and right opens it
  if (GamePad.isLeftPressed()) {
    Serial.println("Left Pressed");
    sparePos -= 3;
    if (sparePos < 0) grabPos = 0;
    Serial.print("sparePos = ");
    Serial.println(sparePos);
  } else if (GamePad.isRightPressed()) {
    Serial.println("Right Pressed");
    sparePos += 3;
    if (sparePos > 180) sparePos = 180;
    Serial.print("sparePos = ");
    Serial.println(sparePos);
  }

  // Controlling shoulder
  // Orange button moves it forward and green moves it backward
  if (GamePad.isOrangePressed()) {
    Serial.println("Orange Pressed");
    shoulderPos += 3;
    if (shoulderPos > 180) shoulderPos = 180;
    Serial.print("shoulderPos = ");
    Serial.println(shoulderPos);
  } else if (GamePad.isGreenPressed()) {
    Serial.println("Green Pressed");
    shoulderPos -= 3;
    if (shoulderPos < 0) shoulderPos = 0;
    Serial.print("shoulderPos = ");
    Serial.println(shoulderPos);
  }

  // Controlling hand
  // Red closes the hand and blue opens it
  if (GamePad.isUpPressed()) {
    Serial.println("Up Pressed");
    handPos -= 3;
    if (handPos < 0) handPos = 0;
    Serial.print("handPos = ");
    Serial.println(handPos);
  } else if (GamePad.isDownPressed()) {
    Serial.println("Down Pressed");
    handPos += 3;
    if (handPos > 180) handPos = 180;
    Serial.print("handPos = ");
    Serial.println(handPos);
  }

Thank you, that seems to have cracked it.
One final question, I'm trying to get a variable speed control using the 1sheeld accelerometer. How do I modify the motor code for this?

#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_SHIELD
#define INCLUDE_LED_SHIELD
#define INCLUDE_ACCELEROMETER_SENSOR_SHIELD
#define INCLUDE_SLIDER_SHIELD


float x, y, z;


/* Include 1Sheeld library. */
#include <OneSheeld.h>
#include <AFMotor.h>
/* include Servo library */
#include <Servo.h>
/* A name for the LED on pin 7. */
const int ledPin = 13;



/* Pin configuration of the Seeedstudio's motor shield. */
int motorAPin1 = A0;
int motorAPin2 = A1;
int motorBPin1 = A2;
int motorBPin2 = A3;
int motorASpeedPin = 3;
int motorBSpeedPin = 5;
// Making servo objects

Servo shoulder;

Servo hand;

Servo grab;

Servo spare;

// Global variables for initializing the servo motors positions

int shoulderPos=90;

int handPos=110;

int grabPos=80;

int sparePos=(90);

/* Define a variable to hold the value of the slider. */
int value;
Servo servo;

void setup()
{
    Serial.begin(9600); // open the serial port at 9600 bps:
  /* Start communication. */
  OneSheeld.begin();

  // Attach servo motors
	// Shoulder on 9, hand on 10, grab on 11 spare on 6, slider on 5.

	shoulder.attach(9);

	hand.attach(10);

  grab.attach(11);

  spare.attach(8);

  servo.attach(7); //servo pin

  	// The LED just works as an indicator that the 1Sheeld is working fine
	pinMode(ledPin, OUTPUT);
  /* Set the LED pin as output. */


  /* Seeedstudio's motor shield initialization. */
  pinMode(motorAPin1, OUTPUT);    // IN1 of motor A
  pinMode(motorAPin2, OUTPUT);    // IN2 of motor A
  pinMode(motorBPin1, OUTPUT);    // IN3 of motor B
  pinMode(motorBPin2, OUTPUT);    // IN4 of motor B
 pinMode(motorASpeedPin, OUTPUT); // Speed of motor A
 pinMode(motorBSpeedPin, OUTPUT); // Speed of Motor B

}

void loop()

{/* Always get the value of the slider and output it as PWM to pin 5. */
  value = Slider.getValue();
  servo.write(value);
  
  
	 
  //Controlling grab
  // Up closes the Grab and down opens it
	if(GamePad.isBluePressed()) {
		grabPos-=3;
    if (grabPos < 0) grabPos = 0;
	} else if(GamePad.isRedPressed()) {
		grabPos+=3;
    if (grabPos > 180) grabPos = 180;
    grab.read();
    Serial.println(grab.read());

    }
	// Controlling spare
	// Left closes the hand and right opens it
	if(GamePad.isLeftPressed()) {
		sparePos-=3;
    if (sparePos < 0) grabPos = 0;
	} else if(GamePad.isRightPressed()) {
		sparePos+=3;
if (sparePos > 180) sparePos = 180;    
    spare.read();
    Serial.println(spare.read());

	}

// Controlling shoulder
	// Orange button moves it forward and green moves it backward
	if(GamePad.isOrangePressed()) {
		shoulderPos+=3;
    if (shoulderPos > 180) shoulderPos = 180;
	} else if(GamePad.isGreenPressed()) {
		shoulderPos-=3;
    if (shoulderPos < 0) shoulderPos = 0;
        shoulder.read();
    Serial.println(shoulder.read());
	}
	// Controlling hand
	// Red closes the hand and blue opens it
	if(GamePad.isUpPressed()) {
		handPos-=3;
    if (handPos < 0) handPos = 0;
	} else if(GamePad.isDownPressed()) {
		handPos+=3;
    if (handPos > 180) handPos = 180;
        hand.read();
    Serial.println(hand.read());
  }  
	// Move the servo motors to it's new positions

	shoulder.write(shoulderPos);

	hand.write(handPos);

  grab.write(grabPos);

  spare.write(sparePos);
  // Delay before run again
	delay(20);




 
x=AccelerometerSensor.getX();
  y=AccelerometerSensor.getY();
  z=AccelerometerSensor.getZ();

  /* If up is pressed, move the car forward. */
if (x < -4)
  {
    analogWrite(motorASpeedPin, 122);
   analogWrite(motorBSpeedPin, 122);
    digitalWrite(motorAPin1, LOW);
    digitalWrite(motorAPin2, HIGH);
    digitalWrite(motorBPin1, LOW);
    digitalWrite(motorBPin2, HIGH);
    digitalWrite(ledPin, HIGH);

  }

  else if (x < -8)
{
    analogWrite(motorASpeedPin, 255);
   analogWrite(motorBSpeedPin, 255);
    digitalWrite(motorAPin1, LOW);
    digitalWrite(motorAPin2, HIGH);
    digitalWrite(motorBPin1, LOW);
    digitalWrite(motorBPin2, HIGH);
    digitalWrite(ledPin, HIGH);

  }

  /* If down is pressed, move the car backwards. */
else if (x > 4)
  {
   analogWrite(motorASpeedPin, 122);
   analogWrite(motorBSpeedPin, 122);
    digitalWrite(motorAPin1, HIGH);
    digitalWrite(motorAPin2, LOW);
    digitalWrite(motorBPin1, HIGH);
    digitalWrite(motorBPin2, LOW);
    digitalWrite(ledPin, HIGH);
  }
  else if (x > 8)
  {
   analogWrite(motorASpeedPin, 255);
   analogWrite(motorBSpeedPin, 255);
    digitalWrite(motorAPin1, HIGH);
    digitalWrite(motorAPin2, LOW);
    digitalWrite(motorBPin1, HIGH);
    digitalWrite(motorBPin2, LOW);
    digitalWrite(ledPin, HIGH);


  }
  /* If right is pressed, turn the car to the right. */
  else if (y < -4)
  {
    analogWrite(motorASpeedPin, 122);
    analogWrite(motorBSpeedPin, 122);
    digitalWrite(motorAPin1, LOW);
    digitalWrite(motorAPin2, HIGH);
    digitalWrite(motorBPin1, HIGH);
    digitalWrite(motorBPin2, LOW);
    digitalWrite(ledPin, HIGH);

  }

   else if (y < -8)
  {
    analogWrite(motorASpeedPin, 255);
    analogWrite(motorBSpeedPin, 255);
    digitalWrite(motorAPin1, LOW);
    digitalWrite(motorAPin2, HIGH);
    digitalWrite(motorBPin1, HIGH);
    digitalWrite(motorBPin2, LOW);
    digitalWrite(ledPin, HIGH);

  }
  /* If left is pressed, turn the car to the left. */
  else if (y > 4)
  {
    analogWrite(motorASpeedPin, 122);
    analogWrite(motorBSpeedPin, 122);
    digitalWrite(motorAPin1, HIGH);
    digitalWrite(motorAPin2, LOW);
    digitalWrite(motorBPin1, LOW);
    digitalWrite(motorBPin2, HIGH);
    digitalWrite(ledPin, HIGH);

  }
  else if (y > 8)
  {
    analogWrite(motorASpeedPin, 255);
    analogWrite(motorBSpeedPin, 255);
    digitalWrite(motorAPin1, HIGH);
    digitalWrite(motorAPin2, LOW);
    digitalWrite(motorBPin1, LOW);
    digitalWrite(motorBPin2, HIGH);
    digitalWrite(ledPin, HIGH);

  }
  /* If nothing is 
  /* If nothing is pressed stop all motors. */
  else
  {
    //analogWrite(motorASpeedPin, 0);
    //analogWrite(motorBSpeedPin, 0);
    digitalWrite(motorAPin1, LOW);
    digitalWrite(motorAPin2, LOW);
    digitalWrite(motorBPin1, LOW);
    digitalWrite(motorBPin2, LOW);
    digitalWrite(ledPin, LOW);
  }

}