Advertisement
KRITSADA

Arduino PS2X Control servo

Dec 12th, 2016
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>  //For driving the ESCs and Servos
  2. #include <PS2X_lib.h> // Bill Porter's PS2X Library
  3. PS2X ps2x;  //The PS2 Controller Class
  4. Servo UpRServo;  //Create servo object representing up right ESC
  5. int RollLeftVal = 0; //Value read off the PS2 Left Stick left/right
  6.   //Variables to carry the actual raw data for the ESCs
  7. int upRraw = 0;
  8. void setup(){
  9.   ps2x.config_gamepad(A0,A2,13,A1, true, false);
  10.   //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?)
  11.   //We have disabled the pressure sensitivity and rumble in this instance and
  12.   //we know the controller type so we have not bothered with the error checks
  13.   UpRServo.attach(6);// attaches the Up Right Servo to pin 6
  14. }
  15.  
  16. void loop(){
  17.   ps2x.read_gamepad(); //This needs to be called at least once a second
  18.                         // to get data from the controller.
  19. //Analogue Stick readings
  20.   RollLeftVal = ps2x.Analog(PSS_LX);
  21. //Variables to carry the actual raw data for the ESCs
  22.   upRraw = (127-RollLeftVal);  //This will be up to a value of 192
  23. //Scale the values to be suitable for ESCs and Servos
  24.   upRraw=map(upRraw,-127,127,0,179);
  25. //Write it to the Servos or ESCs
  26.   UpRServo.write(upRraw);
  27.   delay(15);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement