swarm robotics project trouble communicating with the xbees

Hi guys,

Im doing a small swarm robotics project,basically what i want to do is .there is robot A and robot B ,and on the field there are 4 balls two are black and two are white .i use a line tracking sensor to pick the color up and then once a robot find a ball it should tell the other one which color it picked and so that it can pick the same color .both A and B are communicating wirelessly via Xbees continuously.it doesn't matter who picks the ball first but once a robot finds the ball it should tell the other one the color.there is no one to help me out i live in Beijing and they hardly speak english =(((
here is simple trial code i wrote ,it doesnt have bugs but it freezes in a while .it would be amazing if you can help me out

int colorSensor = 2;
const int ledPinB = 3;
const int ledPinW = 4;
int incomingByte;      

int val =0;
int val1 =0;

void setup()
{
  pinMode(ledPinB, OUTPUT);
  pinMode(ledPinW, OUTPUT);

  Serial.begin(57600);
}

void loop()
{
  val = digitalRead(colorSensor);
  delay(100);
  val1 = digitalRead(colorSensor);
  delay(100);
  
  if (val == val1)
  
  {
    Serial.print('B');
   
    Serial.println("Black Ball");
  }
  else
  {
      Serial.print('W');
          
    Serial.println("WhiteBall");
    
  }
    
      if (Serial.available() > 0)
      {
    
    incomingByte = Serial.read();
    
    if (incomingByte == 'B') 
    {
      digitalWrite(ledPinB, HIGH);
    } 
    else
    {
           digitalWrite(ledPinB, LOW);
    }

    if (incomingByte == 'W')
    {
      digitalWrite(ledPinW, HIGH);
    }
    else
    {
      digitalWrite(ledPinW, LOW);
    }

      }
    
  }

thanks a bunch!
Biman

Wouldn't the values read from the color sensor be colorVal1 and colorVal2, not val and val1?

What kind of sensors are these?

    Serial.print('B');
   
    Serial.println("Black Ball");
  }
  else
  {
      Serial.print('W');
          
    Serial.println("WhiteBall");
    
  }

Why is it necessary to have two print statements in each case?

More importantly, what if a robot has not picked up a ball at all, you are having each robot telling the other what color ball it picked up on every pass through loop, without any way of knowing that the robot has actually picked up a ball.

Now, on to the really important questions. What kind of XBees are you using, and how have you configured them? Do you KNOW that they actually talk to each other?

What problem(s) are you having? All I got from reading your post is that you are having problems, but you didn't say what they are.

there is no one to help me out i live in Beijing and they hardly speak english

If you live in Beijing and expect to communicate with the people there, should you learn Chinese, rather than expect them to learn English?

thanks for the response,
well the i did configure them correctly and I did a small project to use an potentiometer to control 2 servos via wireless using the xbee shields the problem i have is the original robot design i had in my mind was to identify two differnt colors (BLUE annd RED) but the sensor i made have a problem with the amibient light so it doesn't give out the correct output.then i bought 2 color sensors

and the problem is the same some times it gives out the correct color but its not accurate since this project is totally based on the accuracy of the transmitted information i thought of using simple line tracking IR sensors
http://dfrobot.en.gongchang.com/product/8063807

,when the color is changing from black to white it sends a signal to the other xbee.but the simple program i wrote is not working properly after some time if you can guide me to solve the color sensor problem ( an example code to detect the color) or see how to improve my code with line tracking sensors will be great.I am studying in beijing and i can speak chinese but the technical terms are extremely difficult and its really difficult to communicate i didn't mean to disrespect them I just wanted to tell that I don't have anyone to seek help from and I'm extremly happy that you replied i was trying really hard to solve this problem.

thanks

when the color is changing from black to white it sends a signal to the other xbee

No, that is not what is happening. When loop runs, it reads the sensor twice, 100 milliseconds apart. If it gets the same reading twice, regardless of what the reading is, it sends 'B'. If the readings are not the same, it sends 'W'.

Now, I could be mistaken, but I don't think this is what you want to do.

First, I don't think you want to read the sensor at all until the robot has picked up a ball.

Second, I think that reading the color twice is a good idea, but you should not send anything unless the value is the same.

Third, the value to send should depend on the color actually read.

Fourth, the data sent to the other bot should only be sent once.