KRITSADA icon

Arduino PS2X Control servo

KRITSADA | PRO | 12/12/16 08:12:38 AM UTC | 0 ⭐ | 680 👁️ | Never ⏰ | []
C++ |

1.22 KB

|

None

|

0 👍

/

0 👎

#include <Servo.h>  //For driving the ESCs and Servos
#include <PS2X_lib.h> // Bill Porter's PS2X Library
PS2X ps2x;  //The PS2 Controller Class
Servo UpRServo;  //Create servo object representing up right ESC
int RollLeftVal = 0; //Value read off the PS2 Left Stick left/right
  //Variables to carry the actual raw data for the ESCs
int upRraw = 0;
void setup(){
  ps2x.config_gamepad(A0,A2,13,A1, true, false);
  //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?)
  //We have disabled the pressure sensitivity and rumble in this instance and
  //we know the controller type so we have not bothered with the error checks
  UpRServo.attach(6);// attaches the Up Right Servo to pin 6
}
 
void loop(){
  ps2x.read_gamepad(); //This needs to be called at least once a second
                        // to get data from the controller.
//Analogue Stick readings
  RollLeftVal = ps2x.Analog(PSS_LX);
//Variables to carry the actual raw data for the ESCs
  upRraw = (127-RollLeftVal);  //This will be up to a value of 192
//Scale the values to be suitable for ESCs and Servos
  upRraw=map(upRraw,-127,127,0,179);
//Write it to the Servos or ESCs 
  UpRServo.write(upRraw);
  delay(15);
}

Comments