Xbee S1 packet of instructions

I've been working over a week on this, there is a lot to find on the internet, but I cannot seem to get this to work. I'm overlooking a major point.

I'm trying to get communication between two Xbee S1's (terminal of X-ctu receives the packets send by the Arduino so the link works!)

On the sender side have a LCD to monitor the values. The receiver side uses softwareserial to debug.

Sender code:

// Transmitter

//LCD Library
#include "U8glib.h"

U8GLIB_PCD8544 u8g(13, 11, 10, 9, 8);                    // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, Reset = 8


//declarations array
int arBytes = 10;
int val;
int inData[3];  // 2 values
int index;
int inInt;

boolean conf = false;

byte first = 0;
byte second = 1;


//array stuff
int controlArray[5];

char inChar = '!';

byte test = 0;

boolean comm = false;


//LCD Draw screen
void draw(void) 
  {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_6x13);
  
  u8g.setPrintPos(0, 10);
  u8g.print(inData[0], DEC);
  
  u8g.setPrintPos(25, 10);
  u8g.print(inData[1], DEC);

  //Draw array new line
  u8g.setPrintPos(0, 22);
  u8g.print("Array");
  
  u8g.setPrintPos(35, 22);
  u8g.print(controlArray[0], DEC); 
  
  u8g.setPrintPos(60, 22);
  u8g.print(controlArray[1], DEC); 
  
  //ArraySize new line
  u8g.setPrintPos(0, 34);
  u8g.print("Size");
    
  u8g.setPrintPos(60, 34);
  u8g.print(sizeof(controlArray), DEC);

  /* //Message new line
  if(comm == true)
  {
    u8g.setPrintPos(0, 46);
    u8g.print('XBEE ON', DEC);  
  }
  else
  {
    u8g.setPrintPos(0, 46);
    u8g.print('ERROR', DEC);
  }
  */
  }
  

void setup(void) 
{
  // flip screen, if required
  // u8g.setRot180();
  
  // set SPI backup if required
  //u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) 
    u8g.setColorIndex(255);     // white
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
    u8g.setColorIndex(3);         // max intensity
  else if ( u8g.getMode() == U8G_MODE_BW )
    u8g.setColorIndex(1);         // pixel on
  

  // Start serial comm with xbee
  Serial.begin(9600);   
}

void loop() 
{
  // picture loop
  u8g.firstPage();  
  do 
  {
    draw();
  } 
  while( u8g.nextPage() );
  
  //Read analog values
  for (int count=0;count<2;count++)
  {
    int b1 = analogRead(count);
    b1 =  map(b1, 0, 1023, 0, 255);
    constrain(b1, 0, 255);
    controlArray[count] = b1;
  }

  //SEND DATA PACKAGE
  Serial.write("<");
  
  for (int count=0;count<2;count++) 
   {
     if(count<2)
     {
       Serial.write(",");       
     }
   Serial.write(controlArray[count]);
   
   }
  
  Serial.write(">");   
  
delay(500);  //Wait for receiver to 
}

it is sending packets like: <,255,255>
This seems to work fine!

Receiver code:

//Receiver

//SOFTWARE SERIAL
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX


int val = 0;


char inChar;
byte inByte;

char inData[10];  // 2 values

int index;

boolean started = false;
boolean ended = false;

int LED1 = 6;
int LED2 = 9;



void setup(void) 
{
  // Start serial comm with PC
  Serial.begin(19200);
  
  // Start serial comm with xbee 
  mySerial.begin(9600);
  
  //declare Led pins
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  
  Serial.println("Setup done!");

  
}

void loop(void) 
{


  
  

if(mySerial.available() > 0);
  {

    
    inChar = mySerial.read();
	 if(inByte == '<')
	 {
	     started = true;
	     index = 0;    
	 }
	 else if(inByte == '>')
	 {
	     ended = true;             
	 }
	 else if(inByte == ',')
	 {
	     inData[index] = mySerial.read();
             index++;
         }
         
         else if(started && ended) // all bytes are in the array, let's do something!
         {
         

         for (int count=0;count<2;count++)
         {
            Serial.print(inData[count], DEC);
            Serial.print(",");
         }  
         
         
         analogWrite(LED1, inData[0]); 
         analogWrite(LED1, inData[1]);   
         
         // reset statements
         started = false;   
         ended = false;
         delay(5);
         }
  }

}

It tries to parse the code into an array to use later for motor control. It just does nothing. I tried working with zoomkat's code, but it just gives me errors on the string- end: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1285370583

    constrain(b1, 0, 255);

Back to the reference page for you. The constrain function returns a value, the constrained value, yet you discard it, making the constrain call useless. Why do you bother?

Is this somehow related to your problem?

  /* //Message new line
  if(comm == true)
  {
    u8g.setPrintPos(0, 46);
    u8g.print('XBEE ON', DEC);  
  }
  else
  {
    u8g.setPrintPos(0, 46);
    u8g.print('ERROR', DEC);
  }
  */

If not, GET RID OF IT!

char inData[10];  // 2 values

Ten is not two in my universe. Are the rules not the same in yours?

if(mySerial.available() > 0);

That ; on the end is the body of the while statement. Hardly seems what you want.

Thanks for your reply:

The constrain is there to avoid a "strange" value that might be send (the goal is of this is to control a toy chopper). I read that this might be wise to do in case a strange value occurs.

The part for the LCD is not used (its between the /* and */) I got rid of it.

The array I changed because I thought it had something to do with my problem, its changed back to 2.

  • also got rid of the semicolon, not sure how that got there. Should it be a while statement?

Still no results: I just get a printed "Setup done!" from my receiver.

The constrain is there to avoid a "strange" value that might be send (the goal is of this is to control a toy chopper). I read that this might be wise to do in case a strange value occurs.

I know what the constrain function does. It takes an input, the value to be constrained, and a range of valid output values. It returns the constrained value.

Which you discard. So, the point of having called constrain() is lost. So, you might as well not call it.

Should it be a while statement?

It doesn't really matter. The loop() function is called in an endless loop, so it will eventually (and nearly as quickly) collect the serial data. Assuming any is actually arriving.

Still no results: I just get a printed "Setup done!" from my receiver.

Which appears is not the case.

How is the XBee connected to the Arduino? How is the other XBee connected to the PC? How are the two XBees configured?

Which you discard. So, the point of having called constrain() is lost. So, you might as well not call it.

Sorry for not understanding, I'm using the constrain on the b1 value, thus making sure it never leaves its range, I do not see how I'm discarding it.

How is the XBee connected to the Arduino? How is the other XBee connected to the PC? How are the two XBees configured?

Ok, the sender is connected to the rx/tx on a arduino duemilanove. When I use a FTDI board with the other xbee and X-CTU software, I receive all the packets witout any problem: I'm assuming since I haven't changed anything major in the setup that everything is working and the receiving Xbee puts out serial data (I have been able to send single bytes in the past). However more complex packets (which contain more information) have never been received correctly by the other arduino which is:

A arduino mini pro with the xbee connected to pin 10 (rx) and 11 (tx) using softserial (mostly for debugging).

I have been looking into strings, but since i want to send/receive bytes (with values from 0 to 255), a simple array would be the most efficient way.

I'm using the constrain on the b1 value, thus making sure it never leaves its range, I do not see how I'm discarding it.

You are discarding it, because constrain() returns a value, which you don't use, it does not operate on the parameter directly.

Read constrain() - Arduino Reference.

You could do:

 b1 = constrain(b1, 0, 255);

You could do:
Code:
b1 = constrain(b1, 0, 255);

Thank you, yes I understand. I see now that my code is a little silly haha.