# Joystick,xbee, sabertooth program help questions.

I am having trouble getting this code to run properly. It kinda works. When the xbees connects the wheels go toward but wont go in reverse. What they wii reverse for a turn though

My fist question is can a transmitted Byte be a negative number?? Or am i understand the byte description wrong? Does it have to be a integer??

The whole story.

Have joystick connected to a Arduino UNO sending the data using an XBEE.

The robot has a Arduino with an XBee receiving the data then sending it to the Saber-tooth 2X35 using software serial and Packet serial on the Saber-tooth motor controller.

I can see the data being received by the robot arduino using serial monitor.

My question is. Line 52 to 54 is maping the data to -126 to +126 which the Saber tooth want to see. ( I think)

// The switch is towards the joystick (full speed)
tempFB = map(FB, 0, 1024, -126, 126);
tempLR = map(LR, 0, 1024, -126, 126);

But then on line 66-69 its shifting the data to 0 to +255 and i don’t understand why.

// Shift analog data up so we have a range of
// 0-255
tempFB = tempFB + 127;
tempLR = tempLR + 127;

On line 74-76 it assembles the packet (BYTE) to be sent to the Saber-tooth.

So is the Byte a negative number? Does Arduino serial monitor display a Negative number?

So the data being sent to the Saber-tooth is 0 to +255. Which i believe is incorrect for Packet serial.

``````// Analog Input Variables
int FB;     // Forward and Back
int LR;     // Left and Right
int Spin;   // Rotation (used on vectoring robots)

// Temporary variables used in calculations
double tempFB, tempLR, tempSpin;

// Transmitted packet
byte byteFB;
byte byteLR;
byte Digital1;
byte checksum;

// Reverse bits
bool revA0 = 1;  // These are used to easily flip
bool revA1 = 0;  // the direction of the joystick

// Misc variables

void setup() {
Serial.begin(9600);   // COM to xBee
}

void loop() {
reinitVars();    // nulls out temporary variables
captureData();  // retrieves new data
packetizeData();  // assembles data to be sent
transmitData();       // send data off to xBee
//debugData();        // print out formatted debug data

delay(20);  // Delay needed for consistant communication
}

// Reinitialize temporary variables
void reinitVars() {
Digital1 = 0;
tempFB = 0;
tempLR = 0;
}

void captureData() {

// Reverse input analogs if flagged
if (revA0)  { FB = 1024 - FB; }
if (revA1)  { LR = 1024 - LR; }

// The switch is towards the joystick (full speed)
tempFB = map(FB, 0, 1024, -126, 126);
tempLR = map(LR, 0, 1024, -126, 126);

{ tempFB = 0; }
{ tempLR = 0; }

}

// Assembles data to be sent
void packetizeData() {

// Shift analog data up so we have a range of
// 0-255
tempFB = tempFB + 127;
tempLR = tempLR + 127;

// Assemble the digital packet
// Add status of switches to digital1 here

// convert transmitted data to bytes
byteFB = byte(tempFB);
byteLR = byte(tempLR);

// calculate checksum
checksum = byteFB + byteLR + byte(Digital1);
}

// xBee Communication
void transmitData() {
Serial.write("S");
Serial.write("D");
Serial.write("R");
Serial.write(byteFB);
Serial.write(byteLR);
Serial.write(byte(Digital1));
Serial.write(checksum);
}

// Print out values for easy debugging
void debugData() {
Serial.print("[FB: ");  Serial.print(byteFB, HEX);          Serial.print("]");
Serial.print("[LR: ");  Serial.print(byteLR, HEX);          Serial.print("]");
Serial.print("[D1: ");  Serial.print(byte(Digital1), BIN);  Serial.print("]");
Serial.println("");
}
``````