storing incoming data from BT to a char string

im currently doing a project for school, where im sending data from my phone to the arduino via Bluetooth. i can send data (words, numbers) and get them to display on the serial monitor, but i cannot find a way to store the incoming data into a string, so i can then compare it or use it in a formula.
I've followed this blog on setting it up...

using the code through the second download link (TRY WITH THIS CODE ALSO: DOWNLOAD)

im just stuck on figuring out how to store the incoming data from the phone into a string.

im just stuck on figuring out how to store the incoming data from the phone into a string.

Easy. Like so:

char inData[24]; // Use whatever size you need
byte index = 0;

void loop()
{
   while(Serial.available() > 0)
   {
      char inChar = Serial.read();
      if(index < 22)
      {
         inData[index++] = inChar;
         inData[index] = '\0';
      }
   }

   // Use the data in inData, then reset

   inData[0] = '\0';
   index = 0;
}

Of course, then you'll run into the issue where the whole packet does not arrive all at once. Then, you'll need to add start and end of packet markers, and properly read the data. I regularly post how to do that. Search for "started && ended".

ive been messing with this code, and ive been having so trouble displaying whats being stored in 'inChar' correctly. so if i typed in "hello" on my phone and sent it via Bluetooth. i want the ardunio to display "hello" on the serial monitor. currently i can only get it to display 1 character at a time (displaying vertically)

char inChar;
char inData[24]; // Use whatever size you need
byte index = 0;

void loop()
{
   while(Serial.available() > 0)
   {
      inChar = Serial.read();
      if(index < 22)
      {
         inData[index++] = inChar;
         inData[index] = '\0';
      }
      
      Serial.print(inChar);
   } 
   // Use the data in inData, then reset

   inData[0] = '\0';
   index = 0;
}

I had problems using PaulS's while loop method at 9600 baud. Sometimes, it would only display 1 or 2 characters on each line. I think it is due to the reading rate not fast enough. This code worked for me:

char buf[51];
void setup(){
  Serial.begin(9600);
  Serial.setTimeout(100);
}
void loop(){
  Serial.readBytes(buf, 50);
  if (strlen(buf) > 0) Serial.println(buf);
  for (byte i = 0; i < 51; i++) buf[i] = NULL;
}

I used this method to control a servo from the Serial Monitor, and it works great! You might need to change the timeout. You can print a string up to 50 characters long with this code. You might also consider SerialEvent().

I had problems using PaulS's while loop method at 9600 baud. Sometimes, it would only display 1 or 2 characters on each line. I think it is due to the reading rate not fast enough

At 9600 bits per second, it takes over 1 millisecond to transmit and receive a character.
In that time, the processor could execute up to 16 000 instructions, so to say "the reading rate is not fast enough" doesn't seem very likely, does it?

I don't think you've understood how PaulS's code works, and your code will not work with an ordinary terminal emulator and a hesitant typist.

Your code also contains an unnecessary initialisation loop

void loop()
{
  size_t len = Serial.readBytes(buf, 50);
  buf [len] = '\0';
  Serial.println(buf);
}

This code: Serial.read crazy results - #20 by system - Programming Questions - Arduino Forum didn't work. It displayed 1 or 2 characters on each line.

I don't think you've understood how PaulS's code works, and your code will not work with an ordinary terminal emulator and a hesitant typist.

What is "ordinary terminal emulator and a hesitant typist"? I will try PaulS's code later, and modify it to print the string received.

What is "ordinary terminal emulator and a hesitant typist"?

PuTTY or Hyperterm, and someone who spends longer than1/10th of a second between typed characters.

#include <Servo.h>
Servo servo;
char buf[4];
void setup(){
  Serial.begin(9600);
  Serial.setTimeout(11);
  servo.attach(9);
}
void loop(){
  Serial.readBytes(buf, 3);
  if (strlen(buf) > 0){
    Serial.println(atoi(buf));
    servo.write(atoi(buf));
  }
  for (byte i = 0; i < 4; i++) buf[i] = NULL;
}

This is exactly the code I used for controlling a servo from the Serial Monitor. In a simple code like this, the time I take to type the characters does not matter. I'm sure PaulS's code would be better. I'll need to wait until next Saturday to try it.

void loop(){
  Serial.readBytes(buf, 3);

"readBytes" returns an important item of information, but you discard it.
Why?
See reply #4.

 buf[i] = NULL;

NULL != '\0'
If you're going to squeeze a pointer into a "char", it is usually polite to tell the compiler it's a legitimate thing to do.
Or, better still, don't do it at all.
See reply #4

In a simple code like this, the time I take to type the characters does not matter.

It's nothing to do with the complexity, or otherwise, of your code, it is simply how the serial monitor works.

Okay, Serial.readBytes() returns the number of bytes read. Why does it have to be stored in a size_t object, instead of byte, int, word, etc.? Then, you add a NULL terminate character after the bytes have been buffered. Next, I can manipulate the char array (e.g. use atoi() and control a servo), according to your example. Does Serial.readBytes() halt your code? If it does, and it is not tolerable, I'll use PaulS's solution. There are many solutions to one problem; you probably know the most efficient one.

I have another alternate solution:

String in = "";
void setup(){
  Serial.begin(9600);
}
void loop(){
  for(byte i = 1; i <= Serial.available(); i++){
    in += Serial.read();
  }
  if(in != ""){
    Serial.println(in);
    in = "";
  }
}

Does Serial.readBytes() halt your code? If it does, and it is not tolerable, I

It's not my code, it's your code written properly.Of course it waits - serial comms are slow.

serialEvent is a useful way of building strings behind the scenes.

Your new example won't work correctly either; think about the length of time between receiving characters.

serialEvent is a useful way of building strings behind the scenes.

Yeah; build a string, then use a flag when it's done (when Serial.available() == 0).

Your new example won't work correctly either; think about the length of time between receiving characters.

Earlier, you said the "reading rate not fast enough" is not likely. At 9600 baud, it takes about 0.83 ms to read a character. That will interefere with my loop idea (unless I put delay(1);), caused by the time needed to receive the characters.

At 9600 baud, it takes about 0.83 ms to read a character.

Sorry, back to arithmetic 101 for you.