Advertisement
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;
- int motorSpeed;
- bool direction;
- bool is_stick;
- bool hold_A;
- void setup() {
- pinMode(motorPin, OUTPUT);
- pinMode(reversePin, 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
- }
- motorSpeed = 0;
- digitalWrite(reversePin, HIGH);
- Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
- }
- void loop() {
- Usb.Task();
- if(Xbox.XboxReceiverConnected){
- is_stick = false;
- hold_A = false;
- for(uint8_t i = 0; i < 4; i++){
- if(Xbox.Xbox360Connected[i]){
- if(Xbox.getAnalogHat(LeftHatY, i)>7500){
- is_stick = true;
- direction = true;
- }
- if(Xbox.getAnalogHat(LeftHatY, i)<-7500){
- is_stick = true;
- direction = false;
- }
- if(Xbox.getButtonPress(A, i)){
- hold_A = true;
- }
- }
- }
- // Serial.println(is_stick);
- if(is_stick){
- // direction == true - вперед
- // direction == false - назад
- if(direction){
- if(hold_A)
- analogWrite(motorPin, 127);
- else
- analogWrite(motorPin, 255);
- digitalWrite(reversePin, LOW);
- }else{
- if(hold_A)
- analogWrite(motorPin, 127-255);
- else
- analogWrite(motorPin, 0);
- digitalWrite(reversePin, HIGH);
- }
- }else{
- if(direction){
- analogWrite(motorPin, 0);
- }else{
- analogWrite(motorPin, 255);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement