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 ?
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).
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.
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].
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.
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
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.