user3020: I'm starting to build my first Arduino project but I'm running into some problems with serial communication.
I get serial data from the console and store it in a char array called "data".
Then, when I send a new console message to the Arduino, I want it to clear the existing "data" array and store only the new data in that array.
I don't understand exactly what's wrong with my code: I believe those nested Serial.available() statements don't work but I have no ideas on how to fix the code.
Data is correctly stored by the Arduino but it concatenates the newer string to the old one.
int count = 0;
char data[30];
boolean dataComplete = false;

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


void loop() {

  if (Serial.available() > 0){
    if (dataComplete == true){
    Serial.println("There is data already, clearing...");
    char data[30];
    dataComplete = false;
  }
    if (dataComplete == false){
      Serial.println("New command, collecting...");
      while (Serial.available()>0){
        char character = Serial.read();
        data[count] = character;
        count++;
      }
     dataComplete = true;
    }
  }
  Serial.print("Command received: ");
  Serial.println(data);
  delay(1000);
}

Thanks in advance!

user2839: This probably doesn't do what you intend it to do:
Serial.println("There is data already, clearing...");
char data[30];

Your output says you are clearing the data array, but you're doing no such thing. In fact, you are declaring a new local variable called data, which is independent of the global data you already have declared at the top of your program. Local variables only exist within the scope in which they are declared (inside their nearest enclosing { }). 
Instead, your count variable keeps track of how much data you have received. So perhaps do the following:
Serial.println("There is data already, clearing...");
count = 0;

This is not the only thing that might cause your program to operate unexpectedly, but it should at least fix the problem stated in your question.

user220: First of all, this is an excellent example why whitespace matters. Your code is really hard to read as it is, as skimming over it, it seems as the second if statement is outside of the first one.
Fixed code:
int count = 0;
char data[30];
boolean dataComplete = false;

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

void loop() {
  if (Serial.available() > 0){
    if (dataComplete == true){
      Serial.println("There is data already, clearing...");
      char data[30];
      dataComplete = false;
    }
    if (dataComplete == false){
      Serial.println("New command, collecting...");
      while (Serial.available()>0){
        char character = Serial.read();
        data[count] = character;
        count++;
      }
     dataComplete = true;
    }
  }
  Serial.print("Command received: ");
  Serial.println(data);
  delay(1000);
}

Also, it seems as it prints "Command received:" every iteration, regardless of if there's new data or not (although, this could be the intended feature).
Like mentioned, you don't clear the variable, you just create a new one. You'll need to clear and reset count to fix this issue. However, just resetting count won't work if the second command is shorter than the one before.
Also, why do you complicate the code with the dataComplete variable? I simplified the code below:
int count = 0;
char data[30];
boolean dataComplete = false;

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

void loop() {
  if (Serial.available()){
    Serial.println("New command, collecting...");
    count = 0;
    data[] = "";
    while (Serial.available()){
      char character = Serial.read();
      data[count] = character;
      count++;
    }
  }
  Serial.print("Command received: ");
  Serial.println(data);
  delay(1000);
}


user313: To clear an array you would do:
for( int i = 0; i < sizeof(data);  ++i )
   data[i] = (char)0;

or
memset(data, 0, sizeof(data));

, which does the same thing using a library function.
However, because strings of characters (not referring to 'String' objects here) are terminated by a zero byte, only the first byte needs to be zeroed: 
data[0] = (char)0;

will do it.

3020: <p>I'm starting to build my first Arduino project but I'm running into some problems with serial communication.</p>

<p>I get serial data from the console and store it in a char array called "data".</p>

<p>Then, when I send a new console message to the Arduino, I want it to clear the existing "data" array and store only the new data in that array.</p>

<p>I don't understand exactly what's wrong with my code: I believe those nested Serial.available() statements don't work but I have no ideas on how to fix the code.</p>

<p>Data is correctly stored by the Arduino but it concatenates the newer string to the old one.</p>

<pre><code>int count = 0;
char data[30];
boolean dataComplete = false;

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


void loop() {

  if (Serial.available() &gt; 0){
    if (dataComplete == true){
    Serial.println("There is data already, clearing...");
    char data[30];
    dataComplete = false;
  }
    if (dataComplete == false){
      Serial.println("New command, collecting...");
      while (Serial.available()&gt;0){
        char character = Serial.read();
        data[count] = character;
        count++;
      }
     dataComplete = true;
    }
  }
  Serial.print("Command received: ");
  Serial.println(data);
  delay(1000);
}
</code></pre>

<p>Thanks in advance!</p>
 2839: <p>This probably doesn't do what you intend it to do:</p>

<pre><code>Serial.println("There is data already, clearing...");
char data[30];
</code></pre>

<p>Your output says you are clearing the <code>data</code> array, but you're doing no such thing. In fact, you are declaring a <em>new</em> local variable called <code>data</code>, which is independent of the global <code>data</code> you already have declared at the top of your program. Local variables only exist within the scope in which they are declared (inside their nearest enclosing <code>{ }</code>). </p>

<p>Instead, your <code>count</code> variable keeps track of how much data you have received. So perhaps do the following:</p>

<pre><code>Serial.println("There is data already, clearing...");
count = 0;
</code></pre>

<p>This is not the only thing that might cause your program to operate unexpectedly, but it should at least fix the problem stated in your question.</p>
 220: <p>First of all, this is an <em>excellent</em> example why whitespace matters. Your code is really hard to read as it is, as skimming over it, it seems as the second if statement is outside of the first one.</p>

<p>Fixed code:</p>

<pre><code>int count = 0;
char data[30];
boolean dataComplete = false;

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

void loop() {
  if (Serial.available() &gt; 0){
    if (dataComplete == true){
      Serial.println("There is data already, clearing...");
      char data[30];
      dataComplete = false;
    }
    if (dataComplete == false){
      Serial.println("New command, collecting...");
      while (Serial.available()&gt;0){
        char character = Serial.read();
        data[count] = character;
        count++;
      }
     dataComplete = true;
    }
  }
  Serial.print("Command received: ");
  Serial.println(data);
  delay(1000);
}
</code></pre>

<p>Also, it seems as it prints "Command received:" every iteration, regardless of if there's new data or not (although, this could be the intended feature).</p>

<p>Like mentioned, you don't <em>clear</em> the variable, you just create a new one. You'll need to clear <em>and</em> reset <code>count</code> to fix this issue. However, just resetting count won't work if the second command is shorter than the one before.</p>

<p>Also, why do you complicate the code with the <code>dataComplete</code> variable? I simplified the code below:</p>

<pre><code>int count = 0;
char data[30];
boolean dataComplete = false;

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

void loop() {
  if (Serial.available()){
    Serial.println("New command, collecting...");
    count = 0;
    data[] = "";
    while (Serial.available()){
      char character = Serial.read();
      data[count] = character;
      count++;
    }
  }
  Serial.print("Command received: ");
  Serial.println(data);
  delay(1000);
}
</code></pre>
 313: <p>To clear an array you would do:</p>

<pre><code>for( int i = 0; i &lt; sizeof(data);  ++i )
   data[i] = (char)0;
</code></pre>

<p>or</p>

<pre><code>memset(data, 0, sizeof(data));
</code></pre>

<p>, which does the same thing using a library function.</p>

<p>However, because strings of characters (not referring to 'String' objects here) are terminated by a zero byte, only the first byte needs to be zeroed: </p>

<pre><code>data[0] = (char)0;
</code></pre>

<p>will do it.</p>
