Advertisement
Semior001

Xbox controller sketch (частично работает)

Jul 25th, 2016
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 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. int motorSpeed;
  18. int reverse;
  19. bool is_stick;
  20.  
  21. void setup() {
  22.   pinMode(motorPin, OUTPUT);
  23.   pinMode(motorPin, OUTPUT);
  24.   pinMode(motorPin, OUTPUT);
  25.   pinMode(motorPin, OUTPUT);
  26.   Serial.begin(115200);
  27.   #if !defined(__MIPSEL__)
  28.     while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  29.   #endif
  30.   if (Usb.Init() == -1) {
  31.     Serial.print(F("\r\nOSC did not start"));
  32.     while (1); //halt
  33.   }
  34.   motorSpeed = 0;
  35.   digitalWrite(reversePin, HIGH);
  36.   Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
  37. }
  38.  
  39.  
  40. void loop() {
  41.   Usb.Task();
  42.   if (Xbox.XboxReceiverConnected) {
  43.     for (uint8_t i = 0; i < 4; i++) {
  44.  
  45.       is_stick = false;
  46.  
  47.       if (Xbox.Xbox360Connected[i]) {
  48.  
  49.           if (Xbox.getAnalogHat(LeftHatY, i) > 7500 || Xbox.getAnalogHat(LeftHatY, i) < -7500) {
  50.  
  51.             if(Xbox.getAnalogHat(LeftHatY, i) > 7500){
  52.               // Если стик тянем вперед
  53.               digitalWrite(reversePin, HIGH);
  54.               analogWrite(motorPin, motorSpeed-255);
  55.             }
  56.  
  57.             if(Xbox.getAnalogHat(LeftHatY, i) < -7500){
  58.               // Если стик тянем назад
  59.               digitalWrite(reversePin, LOW);
  60.               analogWrite(motorPin, motorSpeed);
  61.             }
  62.  
  63.             is_stick = true;
  64.  
  65.             Serial.print(F("LeftHatY: "));
  66.             Serial.print(Xbox.getAnalogHat(LeftHatY, i));
  67.             Serial.print("\t");
  68.             Serial.println();
  69.           }
  70.  
  71.         if (Xbox.getButtonClick(A, i)){
  72.           Serial.println(F("A"));
  73.           // Если меняем скорость
  74.           if(motorSpeed == 255){
  75.             motorSpeed = 127;
  76.           }else if(motorSpeed == 127){
  77.             motorSpeed = 255;
  78.           }
  79.  
  80.         }
  81.  
  82.       }
  83.  
  84.       if(!is_stick){
  85.         analogWrite(motorPin, 0);
  86.       }
  87.     }
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement