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!