Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <XBOXUSB.h> //Include the necessary libraries.
- #include <Servo.h>
- #include <SPI.h>
- USB Usb;
- XBOXUSB Xbox(&Usb);
- Servo servo1; //Create instances of type Servo. servo1 is the steering servo and servo2 is the ESC.
- Servo servo2;
- int offset = 0; // put this with the other global variables (above the void setup() function).
- int steer = 127; // put this with the other global variables (above the void setup() function).
- void setup() {
- Serial.begin(115200);
- if (Usb.Init() == -1) {
- Serial.print(F("\r\nOSC did not start"));
- while(1); //halt
- }
- Serial.print(F("\r\nXBOX USB Library Started"));
- pinMode(5, OUTPUT);
- pinMode(6, OUTPUT);
- servo1.attach(5); //Steering servo on digital pin 5
- servo2.attach(6); //ESC on sigital pin 6
- }
- void loop()
- {
- Usb.Task();
- if (Xbox.Xbox360Connected) {
- steer = Xbox.getAnalogHat(LeftHatX); // read right joystick
- if(Xbox.getButtonClick(RIGHT)) { // trim right
- offset += 1;
- }
- if(Xbox.getButtonClick(LEFT)) { // trim left
- offset -= 1;
- }
- steer = steer + offset; // incorporate trim into steer command
- if(steer > 255) steer = 255; // enforce upper limit
- if(steer < 0) steer = 0; // enforce lower limit
- 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)
- servo2.write(map(Xbox.getAnalogHat(RightHatY), 0, 255, 180, 0)); // write throttle command
- }
- else
- {
- servo1.write(90);
- servo2.write(90);
- }
- if(Xbox.getButtonClick(XBOX)) {
- Xbox.disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement