[SOLVED] help reading chars from XBee and populating array

I’ve Google and local searched, and read quite a lot of posts both here on the Arduino forum and elsewhere. There are many examples and posts surrounding this, however I couldn’t make any of them work. If you know of a website where there is good example code, please post the URL and I’ll go study it.

Here is what I’m trying to do on a very basic, beginner level. I have two XBee Series 1 working in a point-to-point setup. One of the XBee is connected to my Arduino Uno R3 where I’m programming it with IDE 1.0.1.

The other XBee is connected to a Sparkfun XBee Explorer and I’m connected directly to it with a Terminal.

I can type chars into either XBee from one of the terminals and those chars are displayed on the other terminal.

What I would like to do now is build on that same code by populating an array with a few chars. I’m not trying to make this code robust and error proof yet. At this point I’m just trying to get some chars into the array and then print them out to the terminal.

Here is the code. Populating and printing out the array doesn’t work. Any help would be greatly appreciated.

Thanks,
Jake

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2,3);  // 2=RX 3=TX

char myArray[20];

void setup()  {
  Serial.begin(9600);
  Serial.println("Goodnight moon!");
  // set the data rate for the SoftwareSerial port
  mySerial.begin(19200);
  mySerial.println("Hello, world?");
  // null terminate array
  myArray[0]='\0';
}

void loop()                     // run over and over again
{
  char c;
  int len=0;
  int i=0;

  // read from Xbee and write to term
  if (mySerial.available()) {
      c = mySerial.read();
          if(i<20) {
                myArray[i++]=c;
          } else {
                Serial.println(myArray);
                i=0;
          }
      Serial.write(c);
  }   
  // read from term and send
  if (Serial.available()) {
      c = Serial.read();
      mySerial.write(c);
  }
  delay(100);
}

Printing char arrays expects the array to be null-terminated; you should be adding a null character to the end before trying to print it.

Progress! :smiley:

After adding a null terminate to the string and more print statements, I’m getting one char into myArray[0] with the following code. I need to figure out why no more that one char is getting into the array. I’m sending only one char at a time from the terminal. The output I’m seeing looks like this:

Goodnight moon!
a
myArray>a<
b
myArray>b<
c
myArray>c<
1
myArray>1<
2
myArray>2<
3
myArray>3<

Here is latest code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2,3);  // 2=RX 3=TX

char myArray[20];

void setup()  {
  Serial.begin(9600);
  Serial.println("Goodnight moon!");
  // set the data rate for the SoftwareSerial port
  mySerial.begin(19200);
  mySerial.println("Hello, world?");
  // null terminate array
  myArray[0]='\0';
}

void loop()                     // run over and over again
{
  char c;
  int len=0;
  int i=0;

  // read from Xbee and write to term
  if (mySerial.available()) {
      c = mySerial.read();
      if(i<20) {
          myArray[i++]=c;
          myArray[i]='\0';
      } else {
          Serial.print("myArray= ");
          Serial.println(myArray);
        i=0;
      }
      Serial.write(c);
      Serial.println();
      Serial.print("myArray>");
      Serial.print(myArray);
      Serial.println("<");
  }   
  // read from term and send
  if (Serial.available()) {
      c = Serial.read();
      mySerial.write(c);
  }
  delay(100);
}

Solved: I had to move my index "i" out of the loop(). It kept getting reset to 0. Thanks!