Advertisement
Guest User

Xbox_RC

a guest
Oct 13th, 2019
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <XBOXUSB.h>                                                   //Include the necessary libraries.
  2. #include <Servo.h>
  3. #include <SPI.h>
  4.  
  5. USB Usb;
  6. XBOXUSB Xbox(&Usb);
  7.  
  8. Servo servo1;                                                         //Create instances of type Servo. servo1 is the steering servo and servo2 is the ESC.
  9. Servo servo2;
  10.  
  11. int offset = 0; // put this with the other global variables (above the void setup() function).
  12. int steer = 127; // put this with the other global variables (above the void setup() function).
  13.  
  14. void setup() {
  15.   Serial.begin(115200);                                              
  16.   if (Usb.Init() == -1) {                                            
  17.     Serial.print(F("\r\nOSC did not start"));
  18.     while(1); //halt
  19.   }
  20.   Serial.print(F("\r\nXBOX USB Library Started"));        
  21.   pinMode(5, OUTPUT);
  22.   pinMode(6, OUTPUT);
  23.   servo1.attach(5);                                                  //Steering servo on digital pin 5
  24.   servo2.attach(6);                                                  //ESC on sigital pin 6
  25. }
  26. void loop()
  27. {
  28.   Usb.Task();
  29.  
  30.   if (Xbox.Xbox360Connected) {
  31.    
  32.     steer = Xbox.getAnalogHat(LeftHatX); // read right joystick
  33.    
  34.     if(Xbox.getButtonClick(RIGHT)) { // trim right
  35.       offset += 1;
  36.     }
  37.     if(Xbox.getButtonClick(LEFT)) { // trim left
  38.       offset -= 1;
  39.     }
  40.    
  41.     steer = steer + offset; // incorporate trim into steer command
  42.     if(steer > 255) steer = 255; // enforce upper limit
  43.     if(steer < 0) steer = 0; // enforce lower limit
  44.    
  45.     servo1.write(map(steer, 0, 255, 0, 180)); // write steer command (THE LAST TWO VARIABLES MAY HAVE TO BE CHANGED TO LIMIT RANGE OF MOTION OF SERVO)
  46.     servo2.write(map(Xbox.getAnalogHat(RightHatY), 0, 255, 180, 0)); // write throttle command
  47.    
  48.   }
  49.   else
  50.    {
  51.     servo1.write(90);
  52.     servo2.write(90);
  53.    }
  54.    
  55.     if(Xbox.getButtonClick(XBOX)) {
  56.       Xbox.disconnect();
  57.    }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement