Controlling a Servo with a PS3 controller

Hello,

I am in the middle of a robotics project, and we are controlling the robot using a PS3 controller using the USB host shield and PS3 library found here: http://www.circuitsathome.com/mcu/programming/ps3-and-wiimote-game-controllers-on-the-arduino-host-shield-part-1

I am trying to move two servos incrementally using the D-pad. At the moment the best I can get (I've tried many different variations on the code) is a single press of the UP button will move the servo all the way round, once it has been moved, the servo is unresponsive, until the reset button is pressed.

      if(PS3.getButtonPress(UP) == HIGH) {ser = 1;}
       if(PS3.getButtonPress(DOWN) == HIGH) {ser = 2;}
       if(PS3.getButtonPress(LEFT) == HIGH) {ser = 3;}
       if(PS3.getButtonPress(RIGHT) == HIGH) {ser = 4;}

switch (ser) {
  case 1:
  tilt.write(++tilt_pos);
  break;
  
  case 2:
  tilt.write(--tilt_pos);
  break;
  
  case 3:
  pan.write(++pan_pos);
  break;
  
  case 4:
  pan.write(--pan_pos);
}

Can someone help me please?

Can someone help me please?

Perhaps over at http://snippets-r-us.com. Here, we need to see all of your code.

if(PS3.getButtonPress(UP) == HIGH) {ser = 1;} if(PS3.getButtonPress(DOWN) == HIGH) {ser = 2;} if(PS3.getButtonPress(LEFT) == HIGH) {ser = 3;} if(PS3.getButtonPress(RIGHT) == HIGH) {ser = 4;}

You should have an "else { ser = 0; } Also instead of everything "if", make the first one "if", the others "else if" and the last one just else.

You really need to post the full code, otherwise were just going to guess.

andy_p_: Can someone help me please?

It seems that you're trying to do the pan and tilt motion with the servos.

Why don't you use the analog stick on the PS3 controller, which has 2 built-in potentiometers?

Also, it should be only

if(PS3.getButtonPress(UP)) {ser = 1;}
//etc...

In fact, you shouldn't even be using the getButtonPress function here. This detects pressing the button for a while. getButtonClick is what you should be using.

Why don't you use the analog stick on the PS3 controller, which has 2 built-in potentiometers?

Its possible andy_p_ just wants the servo to move and then stay in place until told to move again. However I do agree he should use the analog sticks, but then the servos wont stay in place on there own. They will reset to center, unless he incorporates a button to lock the servos in place.