├── images └── xbox1motor.jpg ├── .gitignore ├── README.md ├── LICENSE └── xbox1motor └── xbox1motor.ino /images/xbox1motor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gdsports/xbox1motor/HEAD/images/xbox1motor.jpg -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Control DC Motor with an Xbox One controller 2 | 3 | ![Image of Xbox One Controlling a motor](./images/xbox1motor.jpg) 4 | 5 | Demonstrate using an Xbox One to control motor speed. This projects uses the 6 | USB host capability of the SAMD21. The 7 | [USB Host Library for SAMD](https://github.com/gdsports/USB_Host_Library_SAMD) 8 | is required to make this work. 9 | 10 | The Boring Company (Elon Musk's tunnel digging company) uses an 11 | [Xbox controller](https://www.engadget.com/2018/09/10/boring-company-steers-machine-with-xbox-controller/). 12 | 13 | ## Dependencies 14 | 15 | * [USB Host Library for SAMD](https://github.com/gdsports/USB_Host_Library_SAMD) 16 | * Adafruit Motor Shield V2 Library by Adafruit 17 | 18 | ## Components 19 | 20 | * 1 x Adafruit Feather M0 Express 21 | * 1 x Adafruit FeatherWing Motor Controller 22 | * 1 x Microsoft Xbox One Controller 23 | * 1 x USB OTG to host cable or adapter 24 | * 1 x USB micro cable 25 | * 1 x 5V, 2A power pack 26 | * 1 x CP2104 USB serial 27 | 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 gdsports625@gmail.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /xbox1motor/xbox1motor.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Example sketch for the Xbox ONE USB library - by guruthree, based on work by 3 | Kristian Lauszus. 4 | 5 | Combined with DCMotorTest example from Adafruit Motor Shield v2 library. 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | // Create the motor shield object with the default I2C address 12 | Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 13 | 14 | // Select which 'port' M1, M2, M3 or M4. In this case, M2 15 | Adafruit_DCMotor *myMotor = AFMS.getMotor(2); 16 | 17 | // Analog joysticks do not always return 0 when at dead center 18 | // Create +/- DEAD_ZONE around 0. 19 | #define DEAD_ZONE (2000) 20 | 21 | #include 22 | 23 | // On SAMD boards where the native USB port is also the serial console, use 24 | // Serial1 for the serial console. This applies to all SAMD boards except for 25 | // Arduino Zero and M0 boards. 26 | #if (USB_VID==0x2341 && defined(ARDUINO_SAMD_ZERO)) || (USB_VID==0x2a03 && defined(ARDUINO_SAM_ZERO)) 27 | #define SerialDebug SERIAL_PORT_MONITOR 28 | #else 29 | #define SerialDebug Serial1 30 | #endif 31 | 32 | USBHost UsbH; 33 | XBOXONE Xbox(&UsbH); 34 | 35 | bool lastDirForward = true; 36 | uint8_t lastSpeed = 0; 37 | 38 | void setup_motor() { 39 | AFMS.begin(); // create with the default frequency 1.6KHz 40 | //AFMS.begin(1000); // OR with a different frequency, say 1KHz 41 | 42 | // Set the speed to start, from 0 (off) to 255 (max speed) 43 | myMotor->setSpeed(0); 44 | myMotor->run(FORWARD); 45 | // turn on motor 46 | myMotor->run(RELEASE); 47 | SerialDebug.println(F("Motor controller started")); 48 | } 49 | 50 | void setup() { 51 | SerialDebug.begin(115200); 52 | SerialDebug.println(F("Xbox One DC motor demo")); 53 | 54 | if (UsbH.Init()) { 55 | SerialDebug.print(F("\r\nUSB host did not start")); 56 | while (1); //halt 57 | } 58 | SerialDebug.print(F("\r\nXBOX USB Library Started")); 59 | 60 | setup_motor(); 61 | } 62 | 63 | void loop() { 64 | UsbH.Task(); 65 | 66 | if (Xbox.XboxOneConnected) { 67 | int hat_print = 0; 68 | if (Xbox.getAnalogHat(LeftHatX) < -DEAD_ZONE || Xbox.getAnalogHat(LeftHatX) > DEAD_ZONE) { 69 | SerialDebug.print(F("LeftHatX: ")); 70 | SerialDebug.print(Xbox.getAnalogHat(LeftHatX)); 71 | SerialDebug.print("\t"); 72 | hat_print++; 73 | } 74 | 75 | int16_t y = Xbox.getAnalogHat(LeftHatY); 76 | if (y < 0) { 77 | if (lastDirForward) { 78 | lastDirForward = false; 79 | myMotor->run(BACKWARD); 80 | } 81 | if (y == -32768) { 82 | y = 32767; 83 | } 84 | else { 85 | y = -y; 86 | } 87 | } 88 | else { 89 | if (!lastDirForward) { 90 | lastDirForward = true; 91 | myMotor->run(FORWARD); 92 | } 93 | } 94 | if (y < DEAD_ZONE) y = 0; 95 | uint8_t speed = (uint8_t)map(y, 0, 32767, 0, 255); 96 | if (speed != lastSpeed) { 97 | lastSpeed = speed; 98 | myMotor->setSpeed(speed); 99 | } 100 | if (y != 0) { 101 | SerialDebug.print(F("LeftHatY: ")); 102 | SerialDebug.print(y); 103 | SerialDebug.print("\t"); 104 | hat_print++; 105 | } 106 | 107 | if (Xbox.getAnalogHat(RightHatX) < -DEAD_ZONE || Xbox.getAnalogHat(RightHatX) > DEAD_ZONE) { 108 | SerialDebug.print(F("RightHatX: ")); 109 | SerialDebug.print(Xbox.getAnalogHat(RightHatX)); 110 | SerialDebug.print("\t"); 111 | hat_print++; 112 | } 113 | 114 | if (Xbox.getAnalogHat(RightHatY) < -DEAD_ZONE || Xbox.getAnalogHat(RightHatY) > DEAD_ZONE) { 115 | SerialDebug.print(F("RightHatY: ")); 116 | SerialDebug.print(Xbox.getAnalogHat(RightHatY)); 117 | hat_print++; 118 | } 119 | if (hat_print > 0) SerialDebug.println(); 120 | 121 | if (Xbox.getButtonPress(L2) > 0 || Xbox.getButtonPress(R2) > 0) { 122 | if (Xbox.getButtonPress(L2) > 0) { 123 | SerialDebug.print(F("L2: ")); 124 | SerialDebug.print(Xbox.getButtonPress(L2)); 125 | SerialDebug.print("\t"); 126 | } 127 | if (Xbox.getButtonPress(R2) > 0) { 128 | SerialDebug.print(F("R2: ")); 129 | SerialDebug.print(Xbox.getButtonPress(R2)); 130 | SerialDebug.print("\t"); 131 | } 132 | SerialDebug.println(); 133 | } 134 | 135 | if (Xbox.getButtonClick(UP)) 136 | SerialDebug.println(F("Up")); 137 | if (Xbox.getButtonClick(DOWN)) 138 | SerialDebug.println(F("Down")); 139 | if (Xbox.getButtonClick(LEFT)) 140 | SerialDebug.println(F("Left")); 141 | if (Xbox.getButtonClick(RIGHT)) 142 | SerialDebug.println(F("Right")); 143 | 144 | if (Xbox.getButtonClick(START)) 145 | SerialDebug.println(F("Start")); 146 | if (Xbox.getButtonClick(BACK)) 147 | SerialDebug.println(F("Back")); 148 | if (Xbox.getButtonClick(XBOX)) 149 | SerialDebug.println(F("Xbox")); 150 | if (Xbox.getButtonClick(SYNC)) 151 | SerialDebug.println(F("Sync")); 152 | 153 | if (Xbox.getButtonClick(L1)) 154 | SerialDebug.println(F("L1")); 155 | if (Xbox.getButtonClick(R1)) 156 | SerialDebug.println(F("R1")); 157 | if (Xbox.getButtonClick(L2)) 158 | SerialDebug.println(F("L2")); 159 | if (Xbox.getButtonClick(R2)) 160 | SerialDebug.println(F("R2")); 161 | if (Xbox.getButtonClick(L3)) 162 | SerialDebug.println(F("L3")); 163 | if (Xbox.getButtonClick(R3)) 164 | SerialDebug.println(F("R3")); 165 | 166 | 167 | if (Xbox.getButtonClick(A)) 168 | SerialDebug.println(F("A")); 169 | if (Xbox.getButtonClick(B)) 170 | SerialDebug.println(F("B")); 171 | if (Xbox.getButtonClick(X)) 172 | SerialDebug.println(F("X")); 173 | if (Xbox.getButtonClick(Y)) 174 | SerialDebug.println(F("Y")); 175 | } 176 | delay(1); 177 | } 178 | --------------------------------------------------------------------------------