Need help with code for my remote control tank, using XBees and joysticks

I have a DFRobot Shop Rover(w/ XBee) i want to control using two (thumb)joysticks on a boarduino(ATMEGA328) with XBEE. I’m wanting the rover to increase speed as i push up on the joystick, and go in reverse and increase speed as i push down on the joystick. The left motor is controlled by the left joystick and so fourth. I’m not having any luck getting the receiver (rover) to grab the transmission and execute the data. Both of my SparkFun XBee breakout boards have LED’s blinking rapidly, indicating that there is serial data being transmitted, although the TX and RX led’s on the rover board(ATMEGA328) aren’t blinking. I’m just not sure what’s wrong with my code.
I’ve had help but… (my Newbie-ness is interfering )

transmit code:

/* robot x-mitter */

int potPin1 = 1; //joystick #1
int potPin2 = 2; //joystick #2

void setup()
{

Serial.begin(9600);
}

void loop()
{

int potV1 = analogRead (potPin1); //joystick
int potV2 = analogRead (potPin2); //joystick

byte val1 = map (potV1, 0, 1023, 0, 255);
byte val2 = map (potV2, 0, 1023, 0, 255);

if ( potV1 < 0) potV1 = 0; //
else if ( potV1 > 255) potV1 = 255; // nothing other than 0-255 to be included
if ( potV2 < 0) potV2 = 0; //
else if ( potV2 > 255) potV2 = 255; //

Serial.print (val1); //
Serial.print (val2); // not sure which to use here
Serial.write (val1); //
Serial.write (val2); //

delay(50);

}

/* robot reciever */

int motorPin2 = 5; // speed control - right motor
int motorPin1 = 6; // speed control - left motor
int val1;
int val2;
int d7 = 7; // direction/polarity control - right motor
int d8 = 8; // direction/polarity control - left motor

void setup()

{
Serial.begin(9600);

pinMode (motorPin1, OUTPUT);
pinMode (motorPin2, OUTPUT);
pinMode (d7,OUTPUT);
pinMode (d8,OUTPUT);

}

void loop ()
{
if (Serial.available()>=2)
{
if (Serial.available()==2)

{
byte val1 = Serial.read();
byte val2 = Serial.read();

if (val1 >133) //val #1 , upper half of the joystick
{
digitalWrite (d7,HIGH); // polarity positive - forward
analogWrite (motorPin1,val1); // motor speed control
}

else if((val1 > 122)&&(val1 <= 133)) analogWrite (motorPin1,0); //motor does nothing from 122 - 132
else if (val1 <= 122) //lower half of the joystick
{
digitalWrite (d7, LOW); //polarity negative - reverse
analogWrite (motorPin1, 2 * (122 - val1)); //motor gets faster as the joystick is pushed downwards from center- speed control
}

if (val2 >133)
{
digitalWrite (d8,HIGH);
analogWrite (motorPin2,val2);
}

else if((val2 > 122)&&(val2 <= 133)) analogWrite (motorPin2,0);
else if (val2 <= 122)
{
digitalWrite (d8, LOW);
analogWrite (motorPin2, 2 * (122 - val2));
}

}//if (Serial.available()==2)
}// if (Serial.available()>=2)
else Serial.flush();
} // void loop

photo.JPG

void loop ()
{
  if (Serial.available()>=2)
  {
    if (Serial.available()==2)

...

    }//if (Serial.available()==2)
  }// if (Serial.available()>=2)
  else Serial.flush();
} // void loop

Why do this? You are setting yourself up for a very twitchy response. If you don’t receive 2 or more bytes you throw them away. So if you get one byte, and then a millisecond later, when the second byte arrives on its own you throw it away too.

if ( potV1 < 0) potV1 = 0;                      //
  else if ( potV1 > 255) potV1 = 255;       //   nothing other than 0-255 to be included
  if ( potV2 < 0) potV2 = 0;                     //
  else if ( potV2 > 255) potV2 = 255;       //

Why?
potV can’t be less than zero, or greater than 1023, and by inference, “val1” can’t be less than zero or greater than 255.
However, you’re not constraining the right variables anyway.
Just get rid of it - it’ll be simpler.

Apart from the 50 millisecond pause, there’s nothing in your data stream to tell your receiver which number belongs to which joystick.
This may not be a problem if you send the values using “write”, but it almost certainly will be if you use “print”.

(please ue the # (code) icon on the editor’s toolbar when posting code)

Awesome, thanks for the tips guys! Some of the code I put in there was something I saw somewhere and thought I needed it. I've been attempting to write code for about a month now, so I appreciate you guys' help. Ima go try it this evening.