Semior001

Xbox 2 движка + xbox

Jul 28th, 2016
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <XBOXRECV.h>
  2.  
  3. // Satisfy the IDE, which needs to see the include statment in the ino too.
  4. #ifdef dobogusinclude
  5. #include <spi4teensy3.h>
  6. #include <SPI.h>
  7. #endif
  8.  
  9. USB Usb;
  10. XBOXRECV Xbox(&Usb);
  11.  
  12. int motorPin = 3;
  13. int motorPin1 = 5;
  14. int reversePin = 4;
  15. int reversePin1 = 6;
  16.  
  17. void setup() {
  18.   pinMode(motorPin, OUTPUT);
  19.   pinMode(reversePin, OUTPUT);
  20.   pinMode(motorPin1, OUTPUT);
  21.   pinMode(reversePin1, OUTPUT);
  22.   Serial.begin(115200);
  23.   #if !defined(__MIPSEL__)
  24.     while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  25.   #endif
  26.   if (Usb.Init() == -1) {
  27.     Serial.print(F("\r\nOSC did not start"));
  28.     while (1); //halt
  29.   }
  30.   Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
  31. }
  32.  
  33. void motor_forward(int motorPin, int reversePin, bool speed){
  34.   digitalWrite(reversePin, LOW);
  35.   analogWrite(motorPin, 255-127*speed);
  36. }
  37.  
  38. void motor_back(int motorPin, int reversePin, bool speed){
  39.   digitalWrite(reversePin, HIGH);
  40.   analogWrite(motorPin, 0 - 127*speed);
  41. }
  42.  
  43. void motor_stop(int motorPin, int reversePin){
  44.   digitalWrite(reversePin, LOW);
  45.   analogWrite(motorPin, 0);
  46. }
  47.  
  48. bool is_stick_X;
  49. bool forward;
  50. bool right;
  51. bool hold_A;
  52. bool is_stick;
  53.  
  54. void loop() {
  55.   Usb.Task();
  56.   if(Xbox.XboxReceiverConnected){
  57.  
  58.     is_stick_X = false;
  59.     forward = true;
  60.     right = false;
  61.     hold_A = false;
  62.     is_stick = false;
  63.  
  64.     for(uint8_t i = 0; i < 4; i++){
  65.       if(Xbox.Xbox360Connected[i]){
  66.  
  67.         if(Xbox.getAnalogHat(LeftHatY, i)>20000 || Xbox.getAnalogHat(LeftHatY, i)<-20000||
  68.           Xbox.getAnalogHat(LeftHatX, i)>20000 || Xbox.getAnalogHat(LeftHatX, i)<-20000){
  69.  
  70.           is_stick = true;
  71.  
  72.           if(Xbox.getAnalogHat(LeftHatY, i)>20000){
  73.             forward = true;
  74.             is_stick_X = false;
  75.           }
  76.  
  77.           if(Xbox.getAnalogHat(LeftHatY, i)<-20000){
  78.             forward = false;
  79.             is_stick_X = false;
  80.           }          
  81.  
  82.           if(Xbox.getAnalogHat(LeftHatX, i)>20000){
  83.             right = true;
  84.             is_stick_X = true;
  85.           }          
  86.  
  87.           if(Xbox.getAnalogHat(LeftHatX, i)<-20000){
  88.             right = false;
  89.             is_stick_X = true;
  90.           }
  91.  
  92.         }
  93.  
  94.         if(Xbox.getButtonPress(A, i)){
  95.             hold_A = true;
  96.         }
  97.  
  98.       }
  99.     }
  100.  
  101.     if(is_stick){
  102.       if(!is_stick_X){
  103.         if(forward){
  104.           motor_forward(motorPin, reversePin, hold_A);
  105.           motor_forward(motorPin1, reversePin1, hold_A);
  106.         }else{
  107.           motor_back(motorPin, reversePin, hold_A);
  108.           motor_back(motorPin1, reversePin1, hold_A);
  109.         }
  110.       }else{
  111.         if(right){
  112.           motor_back(motorPin, reversePin, hold_A);
  113.           motor_forward(motorPin1, reversePin1, hold_A);
  114.         }else{
  115.           motor_forward(motorPin, reversePin, hold_A);
  116.           motor_back(motorPin1, reversePin1, hold_A);
  117.         }
  118.       }
  119.     }else{
  120.       motor_stop(motorPin, reversePin);
  121.       motor_stop(motorPin1, reversePin1);
  122.     }
  123.  
  124.   }
  125. }
Add Comment
Please, Sign In to add comment