user29799: I'm a beginner on arduino.

I have an arduino mega with two LED (like the image with only 2 less and without the buzzer). A red LED and a green LED. When the arduino receive the code 200 I want the red LED turn on and the green LED turn off. But when I test that but any led turn off or turn on..
void setup()
{
  pinMode(LR, OUTPUT);
  pinMode(LV, OUTPUT);
  Serial.begin(9600);
}

void readSerial(){
  while(Serial.available() > 0){
    car = Serial.read();

    if(car != '+'){
      buff[i] = car;
      buff[i+1] = '\0';
      i++;
    } 
    else{
      sscanf(buff, "%d", &received_code);
    }
  }
}

void loop()
{     
  readSerial();
  if (received_code == 200){
    LRstate = HIGH;
    digitalWrite(LR, LRstate); // turn the LED on (HIGH is the voltage level)
    digitalWrite(LR,LOW);
    delay(100);
    digitalWrite(LV,LOW); 
  } 
}

How can I solve my problem?
Thanks a lot.

user16085: First, make sure the LEDs are connected correctly: I presume the red LED is connected to whichever pin LR is, and the green LED to LV.
According to your diagram, your LEDs will be on if the output is HIGH, and off if the output is LOW.  Your code sets LR to high, but then immediately to LOW again.
Here's a fixed (I hope) verson of your loop():
void loop()
{     
  readSerial();
  if (received_code == 200){
    // Turn the red LED on
    digitalWrite(LR,HIGH);

    // Turn the green LED off
    digitalWrite(LV,LOW); 
  } 
}


Unrelated to the problem you're having, but worth a mention anyway:
There's nothing in your code to stop you overrunning buf if you get a longer input than you're expecting.  If this happens you'll get unpredictable results as you trash over whatever happens to be in that part of memory.

29799: <p>I'm a beginner on arduino.</p>

<p><a href="https://i.stack.imgur.com/fCOJu.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fCOJu.jpg" alt="enter image description here"></a></p>

<p>I have an arduino mega with two LED (like the image with only 2 less and without the buzzer). A red LED and a green LED. When the arduino receive the code 200 I want the red LED turn on and the green LED turn off. But when I test that but any led turn off or turn on..</p>

<pre><code>void setup()
{
  pinMode(LR, OUTPUT);
  pinMode(LV, OUTPUT);
  Serial.begin(9600);
}

void readSerial(){
  while(Serial.available() &gt; 0){
    car = Serial.read();

    if(car != '+'){
      buff[i] = car;
      buff[i+1] = '\0';
      i++;
    } 
    else{
      sscanf(buff, "%d", &amp;received_code);
    }
  }
}

void loop()
{     
  readSerial();
  if (received_code == 200){
    LRstate = HIGH;
    digitalWrite(LR, LRstate); // turn the LED on (HIGH is the voltage level)
    digitalWrite(LR,LOW);
    delay(100);
    digitalWrite(LV,LOW); 
  } 
}
</code></pre>

<p>How can I solve my problem?</p>

<p>Thanks a lot.</p>
 16085: <p>First, make sure the LEDs are connected correctly: I presume the red LED is connected to whichever pin <code>LR</code> is, and the green LED to <code>LV</code>.</p>

<p>According to your diagram, your LEDs will be on if the output is <code>HIGH</code>, and off if the output is <code>LOW</code>.  Your code sets <code>LR</code> to high, but then immediately to <code>LOW</code> again.</p>

<p>Here's a fixed (I hope) verson of your <code>loop()</code>:</p>

<pre><code>void loop()
{     
  readSerial();
  if (received_code == 200){
    // Turn the red LED on
    digitalWrite(LR,HIGH);

    // Turn the green LED off
    digitalWrite(LV,LOW); 
  } 
}
</code></pre>

<hr>

<p>Unrelated to the problem you're having, but worth a mention anyway:</p>

<p>There's nothing in your code to stop you overrunning <code>buf</code> if you get a longer input than you're expecting.  If this happens you'll get unpredictable results as you trash over whatever happens to be in that part of memory.</p>
