SHARE
TWEET

RC_Control

a guest Oct 13th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  Barrett Anderies
  3.  March 11, 2013
  4.  This is a modified version of the PS3BT.ino example sketch by Kristian Lauszus
  5.  For more information visit his blog: http://blog.tkjelectronics.dk/ or
  6.  send him an e-mail:  kristianl@tkjelectronics.com
  7.  */
  8.  
  9. #include <XBOXUSB.h>                                                   //Include the necessary libraries.
  10. #include <Servo.h>
  11. #include <SPI.h>
  12.  
  13. USB Usb;
  14. XBOXUSB Xbox(&Usb);
  15.  
  16. Servo servo1;                                                         //Create instances of type Servo. servo1 is the steering servo and servo2 is the ESC.
  17. Servo servo2;
  18.  
  19. int offset = 0; // put this with the other global variables (above the void setup() function).
  20. int steer = 127; // put this with the other global variables (above the void setup() function).
  21.  
  22. void setup() {
  23.   Serial.begin(115200);                                              
  24.   if (Usb.Init() == -1) {                                            
  25.     Serial.print(F("\r\nOSC did not start"));
  26.     while(1); //halt
  27.   }
  28.   Serial.print(F("\r\nXBOX USB Library Started"));        
  29.   pinMode(5, OUTPUT);
  30.   pinMode(6, OUTPUT);
  31.   servo1.attach(5);                                                  //Steering servo on digital pin 5
  32.   servo2.attach(6);                                                  //ESC on sigital pin 6
  33. }
  34. void loop()
  35. {
  36.   Usb.Task();
  37.  
  38.   if (Xbox.Xbox360Connected) {
  39.    
  40.     steer = Xbox.getAnalogHat(LeftHatX); // read right joystick
  41.    
  42.     if(Xbox.getButtonClick(RIGHT)) { // trim right
  43.       offset += 1;
  44.     }
  45.     if(Xbox.getButtonClick(LEFT)) { // trim left
  46.       offset -= 1;
  47.     }
  48.    
  49.     steer = steer + offset; // incorporate trim into steer command
  50.     if(steer > 255) steer = 255; // enforce upper limit
  51.     if(steer < 0) steer = 0; // enforce lower limit
  52.    
  53.     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)
  54.     servo2.write(map(Xbox.getAnalogHat(RightHatY), 0, 255, 180, 0)); // write throttle command
  55.    
  56.   }
  57.   else
  58.    {
  59.     servo1.write(90);
  60.     servo2.write(90);
  61.    }
  62.    
  63.     if(Xbox.getButtonClick(XBOX)) {
  64.       Xbox.disconnect();
  65.    }
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top