|
| 1 | +/* |
| 2 | + * Robot controller with the Xbox Wireless Receiver library on Arduino |
| 3 | + */ |
| 4 | + |
| 5 | +#include <XBOXRECV.h> |
| 6 | + |
| 7 | +// Satisfy the IDE, which needs to see the include statment in the ino too. |
| 8 | +#ifdef dobogusinclude |
| 9 | +#include <spi4teensy3.h> |
| 10 | +#include <SPI.h> |
| 11 | +#endif |
| 12 | + |
| 13 | +USB Usb; |
| 14 | +XBOXRECV Xbox(&Usb); |
| 15 | + |
| 16 | +uint8_t controlling = 0; |
| 17 | + |
| 18 | +void setup() { |
| 19 | + Serial.begin(115200); |
| 20 | +#if !defined(__MIPSEL__) |
| 21 | + while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection |
| 22 | +#endif |
| 23 | + int done = 0; |
| 24 | + |
| 25 | + Serial.print(F("\r\nWelcome to the robot controller software!")); |
| 26 | + |
| 27 | + while (!done) { |
| 28 | + if (Usb.Init() == -1) { |
| 29 | + Serial.print(F("\r\nOSC did not start")); |
| 30 | + delay(2000); |
| 31 | + } else { |
| 32 | + done = 1; |
| 33 | + } |
| 34 | + } |
| 35 | + //Xbox.setLedBlink(ALL, 0); |
| 36 | + Serial.print(F("\r\nPress the Start button to start controlling the robot")); |
| 37 | +} |
| 38 | + |
| 39 | +void loop() { |
| 40 | + Usb.Task(); |
| 41 | + if (Xbox.XboxReceiverConnected) { |
| 42 | + process_buttons(); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +uint8_t current_motor = 127; |
| 47 | + |
| 48 | +void process_buttons() { |
| 49 | + for (uint8_t i = 0; i < 4; i++) { |
| 50 | + if (Xbox.Xbox360Connected[i]) { |
| 51 | + if (!controlling) { |
| 52 | + if (Xbox.getButtonClick(START, i)) { |
| 53 | + Xbox.setLedMode(ALTERNATING, i); |
| 54 | + controlling = 1; |
| 55 | + } else { |
| 56 | + return; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (Xbox.getButtonClick(BACK, i) || Xbox.getButtonClick(XBOX, i) || Xbox.getButtonClick(SYNC, i)) { |
| 61 | + controlling = 0; |
| 62 | + // stop everything |
| 63 | + set_motor(127, 127); |
| 64 | + Xbox.setLedBlink(ALL, i); |
| 65 | + Serial.print(F("Battery: ")); |
| 66 | + Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3 |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (Xbox.getButtonClick(Y, i) && current_motor < 250) { |
| 71 | + current_motor += 5; |
| 72 | + set_motor(current_motor, 0); |
| 73 | + } |
| 74 | + if (Xbox.getButtonClick(A, i) && current_motor > 0) { |
| 75 | + current_motor -= 5; |
| 76 | + set_motor(current_motor, 0); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +void set_motor(uint8_t left, uint8_t right) { |
| 84 | + // FIXME -- do motor control here |
| 85 | + Serial.print(F("L: ")); |
| 86 | + Serial.print(left); |
| 87 | + Serial.print(F("\t\tR: ")); |
| 88 | + Serial.print(right); |
| 89 | + Serial.println(); |
| 90 | + analogWrite(2, left); |
| 91 | + analogWrite(3, right); |
| 92 | +} |
| 93 | + |
| 94 | + |
0 commit comments