Hi all,
I'm trying to control a servo with an xbox controller using an arduino.
I got the servo going with the 'sweep' example, but now I am trying to control it with the xbox controller.
Using this code.
#include <Servo.h>
#include <Wire.h> // include library for communicating with I2C devices
#include <XBOXRECV.h> // include XBOX receiver library
#ifdef dobogusinclude // satisfy IDE, which only needs to see the include statement in the ino
#include <spi4teensy3.h> // satisfy IDE, which only needs to see the include statement in the ino
#endif // satisfy IDE, which only needs to see the include statement in the ino
USB Usb; // define USB Host Shield
XBOXRECV Xbox(&Usb); // define XBOX receiver and USB connection
Servo BaseServo;
Servo ServoArmHeight;
Servo ServoArmLength;
Servo ClawRotationServo;
int BaseServoValue; // define an integer for the servo value
int ServoArmHeightValue; // define an integer for the servo value
int ServoArmLengthValue; // define an integer for the servo value
int ClawRotationServoValue; // define an integer for the servo value
void setup()
{
if (Usb.Init() == -1) // boots USB connection
{
while (1); // wait 1ms
}
Serial.begin(115200);
BaseServo.attach(11); // define pin
ServoArmHeight.attach(10); // define pin
ServoArmLength.attach(9); // define pin
ClawRotationServo.attach(6); // define pin
BaseServo.write(90); // initial angle
ServoArmHeight.write(90); // initial angle
ServoArmLength.write(90); // initial angle
ClawRotationServo.write(90); // initial angle
}
void loop()
{
Usb.Task(); // gives task to USB Host Shield
if (Xbox.XboxReceiverConnected) // if the XBOX receiver is connected
{
for (uint8_t i = 0; i < 4; i++) // for an unsigned integer of length 8 bits, variable i should = 0, be less than 4, and should increment +1 at a time
{
if (Xbox.Xbox360Connected[i]) // if XBOX 360 controller is connected, with variable i defined above
{
BaseServoValue = map (Xbox.getAnalogHat(LeftHatX, i), 0, 32769, 0, 180); // create integer for servo, and map 15 bit value to 8 bit value directly
ServoArmHeightValue = map (Xbox.getAnalogHat(LeftHatY, i), 0, -32769, -0, 180); // create integer for servo, and map 15 bit value to 8 bit value inversely
ServoArmLengthValue = map (Xbox.getAnalogHat(RightHatY, i), 0, 32769, 0, 180); // create integer for servo, and map 15 bit value to 8 bit value directly
ClawRotationServoValue = map (Xbox.getAnalogHat(RightHatX, i), 0, -32769, -0, 180); // create integer for servo, and map 15 bit value to 8 bit value inversely
}
BaseServo.write(BaseServoValue); // ticked servo angle
ServoArmHeight.write(ServoArmHeightValue); // ticked servo angle
ServoArmLength.write(ServoArmLengthValue); // ticked servo angle
ClawRotationServo.write(ClawRotationServoValue); // ticked servo angle
}
}
}
However, this didn't work and when I attached a scope to the servo on pin 11 I got a trace that looked like the attached image.
I don't have a clue and this even persisted when I completely commented out the mapping and the final value mapping and just told it to write to the midpoint of the servo.
Any ideas?