Advertisement
Semior001

Хуй ардуине в жопу, рабочий код!

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