Skip to content

Commit d800fc8

Browse files
committed
cleanup, golden code, two triggers go full speed ahead
1 parent eaecfea commit d800fc8

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

robot-xbox-controller-arduino.ino

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ void loop() {
4343
}
4444
}
4545

46+
// per experience, this is the max speed backward and forward
47+
#define MAX_BACKWARD 77
48+
#define MAX_FORWARD 177
49+
#define MIDDLE_POINT 127
50+
#define CONTROLLER_RATIO 656
51+
4652
void process_buttons() {
4753
for (uint8_t i = 0; i < 4; i++) {
4854
if (Xbox.Xbox360Connected[i]) {
@@ -58,22 +64,25 @@ void process_buttons() {
5864
if (Xbox.getButtonClick(BACK, i) || Xbox.getButtonClick(XBOX, i) || Xbox.getButtonClick(SYNC, i)) {
5965
controlling = 0;
6066
// stop everything
61-
set_motor(0, 0);
67+
set_motor(MIDDLE_POINT, MIDDLE_POINT);
6268
Xbox.setLedBlink(ALL, i);
6369
Serial.print(F("Battery: "));
6470
Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3
6571
return;
6672
}
6773

68-
int16_t lh = Xbox.getAnalogHat(LeftHatY, i);
69-
int16_t rh = Xbox.getAnalogHat(RightHatY, i);
74+
if (Xbox.getButtonPress(L2, i) && Xbox.getButtonPress(R2, i)) {
75+
set_motor(MAX_FORWARD, MAX_FORWARD);
76+
} else if (Xbox.getButtonPress(L2, i)) {
77+
set_motor(MAX_FORWARD, MAX_BACKWARD);
78+
} else if (Xbox.getButtonPress(R2, i)) {
79+
set_motor(MAX_BACKWARD, MAX_FORWARD);
80+
} else {
81+
int16_t lh = Xbox.getAnalogHat(LeftHatY, i);
82+
int16_t rh = Xbox.getAnalogHat(RightHatY, i);
7083

71-
set_motor(c2m_scale(lh), c2m_scale(rh));
72-
// Serial.print(F("L: "));
73-
// Serial.print(lh);
74-
// Serial.print(F("\t\tR: "));
75-
// Serial.print(rh);
76-
// Serial.println();
84+
set_motor(c2m_scale(lh), c2m_scale(rh));
85+
}
7786
}
7887
}
7988
}
@@ -88,13 +97,12 @@ uint8_t c2m_scale(int16_t stick) {
8897
}
8998

9099
// make it range from 0 to 250 or so
91-
uint8_t ret = stick/656 + 127;
100+
uint8_t ret = stick/CONTROLLER_RATIO + MIDDLE_POINT;
92101

93102
return ret;
94103
}
95104

96105
void set_motor(uint8_t left, uint8_t right) {
97-
// FIXME -- do motor control here
98106
Serial.print(F("L: "));
99107
Serial.print(left);
100108
Serial.print(F("\t\tR: "));

0 commit comments

Comments
 (0)