XBee.read doesnt seem to work

Hello,

I am trying to send serial data from arduino to xbee and then send to another xbee wirelessly in AT Mode. However, can not receive or see the data on X-CTU on the recceivers end. For some reason the data is not beiing written on the transmit xbee. Here is the code.

 #include <Keypad.h>
#include <SoftwareSerial.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'#','0','*','D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

SoftwareSerial XBee(2,3);

char letter[5];
int i = 0;
int text[5];
  
void setup(){
  XBee.begin(9600);
}

void loop()
{
  char key = keypad.getKey();
  if(XBee.available())
  {
    for (i = 0; i <= 5; i+ i + 1) 
    {
    if (key != NO_KEY)
      {
      letter[i] = key;
      }
    }
   text[5]= letter[5];
   XBee.write(text[5]);
  } 
}

hum…

char letter[[color=red]5[/color]];
int text[[color=red]5[/color]];
...
    for (i = 0; i [color=red]<=[/color] 5; i+ i + 1) 
..
      letter[ [color=red]i[/color] ] = key;

   text[[color=red]5[/color]]= letter[[color=red]5[/color]];

what do you think happens to i in that loop if nothing is pressed? how fast do you think this loops?

    for (i = 0; i <= 5; i+ i + 1) {
       if (key != NO_KEY) {
         letter[i] = key;
       }
    }

Do you think this is copying 5 characters and then sending 5?

   text[5]= letter[5];
   XBee.write(text[5]);

Ahhh. The loop happen almost instantly. So the loop will continue all the way through without adding to the array.

And for the code below wouldn't it convert the array of char into int? I tried it with individual variables and it worked.

 text[5]= letter[5];
   XBee.write(text[5]);

And for the code below wouldn't it convert the array of char into int?

No, and an array with 5 elements does NOT have a text[5] or letter[5].

You need to rethink the way you capture your number. Your for loop does not wait for new keypress so indeed does not do what you want; Also as the color tried to indicate an array of size 5 does not have an element at position 5 (index goes from 0 to 4).

Code like this arrayA[4] = arrayB[4]; just copies the index 4 (5th element) of the B array into the 4th index of array A. It does not copy the entire array.

If you want to capture a number with 5 digits you can directly build it as you type

V = 0;
While not received 5 keys
     Wait for key 
     If key is a digit V = 10*V+(key-'0'); else ignore that key
End while
Send V

The way it works is just the base 10 representation for example if you type the characters '1','2' and '3' represented by their ASCII codes then '1'-'0' = 1, '2'-'0'=2 etc gives you the right numerical representation of the new digit so you get

V = 0
V = 0 * 10 + ([color=blue]'1'[/color]-'0') = [color=green]1[/color]
V = [color=green]1[/color] * 10 + ([color=blue]'2'[/color]-'0') = 10+2  = [color=green]12[/color]
V = [color=green]12[/color]* 10 + ([color=blue]'3'[/color]-'0') = 120+3 = [color=green]123[/color]

See you are building the right number as you go

Choose the type for V smartly, 5 digits do not fit always in 16 bits

Thanks a lot that helped!