XBee reading and Char error

The xbee will received data every few short second comes in line of 18 digits. I received the xbee in do… while because if not the program would run the entire program before the xbee finish receive all the 18 digit in the line.

So what Im trying to do here is to be able to read the received data individually. For example in the first line of data I have 123456789123456789, and I would like to be able to access it one by one. However I dont know how to do it. And this is my attempt which result in error “request for member “charAt” in ‘received’, which is non-class type ‘char’” I think its because of the ‘received’ is not in array of char.

void loop()
{
    char received;
    int i = 0;
    char keep[] = {};
    do
    {
      if (XBee.available())
      {
        keep[i] = XBee.read();
        Serial.write(keep[i]);
      }
       i++;
    }
    while (i <= 17);
   
    char key=received.charAt(0);
    Serial.println(key);
}

With this code you overwrite other variables and

do
    {
      if (XBee.available())
      {
        keep[i] = XBee.read(); // THERE IS NO SPACE FOR THAT DATA... HOW BIG IS THE ARRAY?
        Serial.write(keep[i]);
      }
       i++; // <=== WHY ARE YOU INCREMENTING i EVEN IF YOU DID NOT READ DATA ??
    }
    while (i <= 17);

if no data is available on the serial line you still increase i

And indeed what is the type of received? Do you think you can call charAt()?

received.charAt(0);

If that were an array how do you access elements in an array?

    int i = 0;
    String keep;
    do
    {
      if (XBee.available())
      {
        keep = XBee.read();
        Serial.write(keep);
      }
       i++;
    }
    while (i <= 17);
    Serial.println(keep.charAt(0));

Sorry I posted and look at the wrong code. This is the latest one. I got error saying no matching function for call to ‘HardwareSerial::write(String&)’. I thought serial.write can be use for string?

You still have your i++ at the wrong place What do you think keep = XBee.read(); does? You should not use the String class and You should study Serial Input Basics And explore cstrings’ (null terminated char array) associated C functions from stdlib.h and string.h

Thank you for our advice. I have studied the link you have given, it solves part of my problems here and this is the new code

boolean newData = false;
const byte num = 18;
char receivedA[num];

void loop()
{
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;
  if (XBee.available() > 0)
  {

    rc = XBee.read();//Thumb
    Serial.write(rc);

    if (rc != endMarker) {
      receivedA[ndx] = rc;
      ndx++;
      if (ndx >= num) {
        ndx = num - 1;
      }
    }
    else {
      receivedA[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
      //Serial.println(rc[0]);
//**********************************************************************
//****PROGRAM STARTS HERE***********************************************
      char x = rc[0];
//**********************************************************************
    }
  }

I studied about the cstring also but I still dont see how to access rc individually. I would appreciate if you could gives me example on how to do it because I could not seem to understand why I get the error which is 'invalid tpes 'char[int]' for array subscript.

Also the second 'Serial.write(rc);' at the bottom seems to not save what the first one saved because the second one prints blank only

void receivedWithEndMarker()
{
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;
  if (XBee.available() > 0)
  {

    rc = XBee.read();//Thumb
    Serial.write(rc);

    if (rc != endMarker) {
      receivedA[ndx] = rc;
      ndx++;
      if (ndx >= num) {
        ndx = num - 1;
      }
    }
    else {
      receivedA[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
      //Serial.println(rc[0]);
//**********************************************************************
//****PROGRAM STARTS HERE***********************************************
      
      Serial.write(rc);
//**********************************************************************
    }
  }
}

I studied about the cstring also but I still dont see how to access rc individually.

You can think of "rc" as a temporary value "receivedchar" which picks up each character in turn form the serial data, and then passes it to receivedA[ndx].

receivedA[ndx] = rc;

Once the data string is received, you have the end marker and new data =true, you can access the individual chars in receivedA[ndx].

Oh now I understand. Thank you for the help :)