How to avoid "serial available" to register printlines?

Hello

I have the following code. It workes as it's supposed to: In the beginning it does nothing. Then I use the serial monitor to send an int with 5 digits (f.ex. 02255). And the program prints the digit "0".

int state = 0;
if (Serial.available() > 0)
{
    state = Serial.read(); // used to read incoming data
    visualC[i] = state;
    i++;
    if (i >=5)
    {
      i = 0;
      Serial.println(visualC[0]);
    }
}

But when I also tries to send something back to the serial monitor, the if-statement with "Serial available" starts. I don't understand or want the "serial available" if-statement to do anything before it RECIEVES values. What happens now is that it prints out an value that looks random. Here is the code that does not work as I want:

int state = 0;

Serial.println("5.32;4.63;2.36;1.88;4.65;4.21");
if (Serial.available() > 0)
{
    state = Serial.read(); // used to read incoming data
    visualC[i] = state;
    i++;
    if (i >=5)
    {
      i = 0;
      Serial.println(visualC[0]);
    }
}

It helps if you post all the code because your error is often in the bit you miss out. Like in this case there is nothing wrong with that code section you posted. I made it into a full program and tested it and nothing untoward happens.

int state, i;
int visualC[30];

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

void loop(){

Serial.println("5.32;4.63;2.36;1.88;4.65;4.21");
if (Serial.available() > 0)
{
    state = Serial.read(); // used to read incoming data
    visualC[i] = state;
    i++;
    if (i >=5)
    {
      i = 0;
      Serial.print(visualC[0]);
      Serial.println(" Error");
    }
}

}

You will see that "Error" is never sent unless you send something from the serial monitor.

What is the Arduino communicating with?

Serial.available() returns the count of bytes in the incoming serial data buffer. It is completely independent of the output that Serial.print() sends.

If sending data causes Serial.available() to return a value greater than 0, it is because whatever you sent the serial data generated a response.

Without knowing what you are talking to, we can't help you determine what to do about the problem.

For testing purposes, the arduino is only communicating with the serial monitor (sending and recieving). Here is the whole code. It's not verry fit :P

Edit: just to clarify: The problem is that the VisualC-array gets filled up with numbers that it does not recieves from the serial monitor. This is not a fact wihout that print line.

/* Recive

*/

/*PIN-list
3 = PWM, LED, room 3
5 = PWM, LED, room 2
6 = PWM, LED, room 1
9 = PWM, fan, room 2
12 = PWM, oven, room 3
13 = PWM, oven, room 2

*/

//Initialates the pins
int fan2 = 9;
int oven2 = 13;
int oven3 = 12;
int LED1 = 6;
int LED2 = 5;
int LED3 = 3;

//Initialates the objects
int fan = 0;
int oven = 1;
int LED = 2;

//Initializes the room
int room1 = 1;
int room2 = 2;
int room3 = 3;

int arduino = 0;
int state=0;
int visualC[5];
int i = 0;
void setup() {
    Serial.begin(115200); // same as in your c++ script
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
    pinMode(LED3, OUTPUT);
    pinMode(fan2, OUTPUT);
    pinMode(oven2, OUTPUT);
    pinMode(oven3, OUTPUT);
}

void loop() {

  //Reads the value of light
  
//============================================================

// (Ground) ---- (10k-Resister) -------|------- (Thermistor) ---- (+5v)
//                                     |
//                                Analog Pin 0

//============================================================ 

  double sensorValue = analogRead(A0);
  double volt = 5.17*sensorValue/1024;
  byte sende = 5;
  delay(1000);
  if(fan == 0)

//This is the line that messes it all up
  Serial.println("5.32;4.63;2.36;1.88;4.65;4.21");
  
  delay(1000);

  if (Serial.available() > 0)
  {
    state = Serial.read(); // used to read incoming data
    visualC[i] = state;
    i++;
    
   /* if (state == 0)
    {
      digitalWrite(9, HIGH);
    }*/
    
    if (i >=5)
    {
      i = 0;
      Serial.println(visualC[0]);
      Serial.println(visualC[1]);
      Serial.println(visualC[2]);
      Serial.println(visualC[3]);
      Serial.println(visualC[4]);
      
      //Sends the value from Visual C++ to the oven
      if (visualC[0] == oven) //
      {      
        //Sets the value on the oven to room 2
        if (visualC[1] == room2)
        {
          arduino = (visualC[4]);
          digitalWrite(oven2, arduino);
        }
        
        //Sets the value on the oven to room 3
        if (visualC[1] == room3)
        {
          arduino = (visualC[4]);
          digitalWrite(oven3, arduino);
        }
      }
      
      //Sends the value from Visual C++ to the fan
      //if (visualC[0] == '0') //
      //if (visualC[0] == 0)

      if (visualC[0] == fan)
      {
        //analogWrite(9, 100);
        //Sets the value on the fan to room 2
        if (visualC[1] == room2)
          {     
            arduino = (((visualC[2])*100)+((visualC[3])*10)+(visualC[4]));
            //arduino = (((visualC[2]-48)*100)+((visualC[3]-48)*10)+(visualC[4]-48));
       analogWrite(fan2, arduino);
            //analogWrite(9, 255);
      }
      }
      
      //Sends the value from Visual C++ to the LEDs
      if (visualC[0] == LED) //
      {
        //Sets the value on the fan to room 1
    if (visualC[1] == room1)
    {
          arduino = (((visualC[2])*100)+((visualC[3])*10)+(visualC[4]));
      analogWrite(LED1, arduino);
    }
        //Sets the value on the fan to room 2
    if (visualC[1] == room2)
    {
          arduino = (((visualC[2])*100)+((visualC[3])*10)+(visualC[4]));
      analogWrite(LED2, arduino);
    }
        //Sets the value on the fan to room 3
    if (visualC[1] == room3)
    {
          arduino = (((visualC[2])*100)+((visualC[3])*10)+(visualC[4]));
      analogWrite(LED3, arduino);
    }
      }
    } 
    
    
    /*
    if (visualC[1] == '2')
    {
      analogWrite(9, 128);
    }
    */
  }
//  
  //delay(1000);
  //Serial.println(visualC[0]);
  //delay(1000);
  delay(30);
}

Nothing wrong with that code as regarding misbehaving of the serial available.

So… you say that the the code does not go into the if-statement with serial available while you have the println-line?

I can’t understand why it does not work for me.

  if (Serial.available() > 0)
  {
    state = Serial.read(); // used to read incoming data
    visualC[i] = state;

As soon as the first byte of serial data arrives, you store it in visualC0, and then:

      Serial.println(visualC[0]);
      Serial.println(visualC[1]);
      Serial.println(visualC[2]);
      Serial.println(visualC[3]);
      Serial.println(visualC[4]);

you think that you can print 5 values and

      if (visualC[0] == oven) //
      {      
        //Sets the value on the oven to room 2
        if (visualC[1] == room2)
        {
          arduino = (visualC[4]);

that the whole array contains valid data. It does not.

you say that the the code does not go into the if-statement with serial available while you have the println-line

I took the code as you posted it, downloaded onto my arduino and opened the serial monitor. All I got out was the line you printed. Then I typed in a load of digits and things appeared on the output. What are you doing that is different?

Hmm, I understand that I will need to look a bit closer to the problem. Break it up a bit and try to trigger the problem again with some less code. But I'm a bit bussy now these days, so I don't know when I will return.

Thank you so far :D