receiving string from serial monitor

I am having problem with the strings

I want receive the full text string for ex. if I send "variable" then I must receive the same

int i;
char str[20];

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
    while (Serial.available()!=0)
  {
    for(i=0; str[i]!='\0'; i++)
     {
      str[i]= Serial.read();
      Serial.print(str);
     }
  }
}

this method shows no output

char indata[20];
byte index = 0;

//char data[] = "";

void setup()
{
  Serial.begin(9600);
}

void loop() 
{
  while(Serial.available()!=0)
  {
    //inchar = Serial.read();
    indata[index] = Serial.read();
    index++;
    indata[index] = '\0';
  }
  Serial.print(indata);
}

this method show output which repeats itself continuously

am i doing it wrong?

don't want to use Serial.readString()

Serial input basics.

The second one repeatedly prints the input string because the Serial.print() is in loop(). You need to detect that the input has been read completely and only then print what you received once

Is the input terminated in some way ? Carriage Return or Linefeed maybe ?

See Serial input basics - updated

am i doing it wrong?

Maybe. Is that what you want it to do?

The Loop function will run over and over and over. That is Standard.

In the Loop function, if data is available, you receive it into the variable indata.

Independent of data having been received (because the println is outside the while Loop) you Display it using the Serial Monitor.

So far, so good.

Loop will repeat itself (because that is Standard Arduino behavior). Assume that this time, no data is available so nothing will be received into the variable indata. However, since indata is global, it is unchanged and still has the Content last received.

Again, Independent of having receiving anything (because it's after the while Loop Ends) you Display the (old) Contents of indata to the Serial Monitor.

One Problem here is that you don't seem to be looking for the end of Transmission. The program could be fixed to only Display the text, for example, after having received an end-of-line. As it is... I don't see a solution...

What should the string terminate with? (a Zero or carriage return or line feed are Standards).

Thank you guys for the tutorial and your guidance.

One thing at last I want to know is that, why does the first method is not working?

gauravntpl:
One thing at last I want to know is that, why does the first method is not working?

It would be a very good learning exercise to spend some time comparing your code with the programs in my tutorial. As well as getting an answer to your question you would get the experience of figuring out how a program works which will be useful for other projects.

...R

I tried it, will try it again.

gauravntpl:
I tried it, will try it again.

If you have a specific question about something you don't understand please ask it.

...R

This is what I tried.

static int i;
char str[20];

boolean stat = false;

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  while(Serial.available()>0 && stat == false)
  {
   char data = Serial.read();

   if(data != '\0')
   {
    for(i = 0; str[i] != '\0'; i++)
    {
      str[i] = data;
    }
   }

        if(str == '\0')
      {
        Serial.print(str[i]);
      }

      else
      {
        Serial.print("there is no data to print");
        Serial.println();
      }
   
  }
}

I guess this code doesn't goes in the "if(data != '\0')" loop.

It keeps on printing "there is no data to print".

The basic that I know is that char variable will hold a single character at a time. So we have to increment the counter of the size to save the next character.

I want to use for loop to increment the counter of the char str[20].

Guide me.

gauravntpl:
I want to use for loop to increment the counter of the char str[20].

not sure what that means...

are you trying to recieve a string sent from the terminal?

how do you want to increment the counter?

gauravntpl:
This is what I tried.

You have not told us what your trial hopes to achieve.

In your Reply #4 you said "why does the first method is not working?" and I don't understand why you would need to write more code in an effort to answer that question.

...R

Robin2:
You have not told us what your trial hopes to achieve.

In your Reply #4 you said "why does the first method is not working?" and I don't understand why you would need to write more code in an effort to answer that question.

...R

I just compared your tutorial code to mine as you asked.

Robin2:
It would be a very good learning exercise to spend some time comparing your code with the programs in my tutorial. As well as getting an answer to your question you would get the experience of figuring out how a program works which will be useful for other projects.

...R

My main motive is to use for loop insted of using this

    indata[index] = Serial.read();
    index++;
    indata[index] = '\0';

I want to receive the string from Serial monitor

BulldogLowell:
not sure what that means...

are you trying to recieve a string sent from the terminal?

how do you want to increment the counter?

Thank you.

gauravntpl:
My main motive is to use for loop insted of using this

You need to be aware that the serial input buffer may not have the complete message the first time you check it. This is because serial data arrives very slowly by Arduino standards.

The code in my 2nd and 3rd examples keeps taking data from the buffer and does not set newData = true until it knows it has the complete message. There might be 10 or 20 calls to the recvWithxxx() function before the complete message has been received - even for a short message.

...R