Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <XBOXRECV.h>
- // Satisfy the IDE, which needs to see the include statment in the ino too.
- #ifdef dobogusinclude
- #include <spi4teensy3.h>
- #include <SPI.h>
- #endif
- USB Usb;
- XBOXRECV Xbox(&Usb);
- int motorPin = 3;
- int motorPin1 = 5;
- int reversePin = 4;
- int reversePin1 = 6;
- void setup() {
- pinMode(motorPin, OUTPUT);
- pinMode(reversePin, OUTPUT);
- pinMode(motorPin1, OUTPUT);
- pinMode(reversePin1, OUTPUT);
- Serial.begin(115200);
- #if !defined(__MIPSEL__)
- while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
- #endif
- if (Usb.Init() == -1) {
- Serial.print(F("\r\nOSC did not start"));
- while (1); //halt
- }
- Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
- }
- void motor_forward(int motorPin, int reversePin, bool speed){
- digitalWrite(reversePin, LOW);
- analogWrite(motorPin, 255-127*speed);
- }
- void motor_back(int motorPin, int reversePin, bool speed){
- digitalWrite(reversePin, HIGH);
- analogWrite(motorPin, 0 - 127*speed);
- }
- void motor_stop(int motorPin, int reversePin){
- digitalWrite(reversePin, LOW);
- analogWrite(motorPin, 0);
- }
- bool is_stick_X;
- bool forward;
- bool right;
- bool hold_A;
- bool is_stick;
- void loop() {
- Usb.Task();
- if(Xbox.XboxReceiverConnected){
- is_stick_X = false;
- forward = true;
- right = false;
- hold_A = false;
- is_stick = false;
- for(uint8_t i = 0; i < 4; i++){
- if(Xbox.Xbox360Connected[i]){
- if(Xbox.getAnalogHat(LeftHatY, i)>20000 || Xbox.getAnalogHat(LeftHatY, i)<-20000||
- Xbox.getAnalogHat(LeftHatX, i)>20000 || Xbox.getAnalogHat(LeftHatX, i)<-20000){
- is_stick = true;
- if(Xbox.getAnalogHat(LeftHatY, i)>20000){
- forward = true;
- is_stick_X = false;
- }
- if(Xbox.getAnalogHat(LeftHatY, i)<-20000){
- forward = false;
- is_stick_X = false;
- }
- if(Xbox.getAnalogHat(LeftHatX, i)>20000){
- right = true;
- is_stick_X = true;
- }
- if(Xbox.getAnalogHat(LeftHatX, i)<-20000){
- right = false;
- is_stick_X = true;
- }
- }
- if(Xbox.getButtonPress(A, i)){
- hold_A = true;
- }
- }
- }
- if(is_stick){
- if(!is_stick_X){
- if(forward){
- motor_forward(motorPin, reversePin, hold_A);
- motor_forward(motorPin1, reversePin1, hold_A);
- }else{
- motor_back(motorPin, reversePin, hold_A);
- motor_back(motorPin1, reversePin1, hold_A);
- }
- }else{
- if(right){
- motor_back(motorPin, reversePin, hold_A);
- motor_forward(motorPin1, reversePin1, hold_A);
- }else{
- motor_forward(motorPin, reversePin, hold_A);
- motor_back(motorPin1, reversePin1, hold_A);
- }
- }
- }else{
- motor_stop(motorPin, reversePin);
- motor_stop(motorPin1, reversePin1);
- }
- }
- }
Add Comment
Please, Sign In to add comment