Problem with Serial Communication Loop

Hi everybody!

This is one of my first projects with the Arduino UNO so far, and i am using a pdf guide to take my first steps...
This exercise involved giving inputs from the serial communication monitor to control the Arduino's own LED.

After having analized the code the guide was providing, i decided to make things more complex using strings instead of numbers to turn on and off the LED, and i wanted to get a line of text back when the command had been executed. However i only managed to give the first input and it would start a loop, turning the LED on and showing a "LED ACCESO" message ininterruptly. This happened even if i had given as first input the command to turn the LED off...

Here's the code, i'm sure you will be able to find my mistake and report it to me.
Thank you for helping in advance!

const int LED_PIN = 13;            
String command = "";              

void setup(){                              
    pinMode (LED_PIN, OUTPUT);            
    Serial.begin(9600);                   
}

void loop(){
    if (Serial.available() > 0) {                               
      command = command + (String)Serial.read();              
      if (command = "accendi"){
        digitalWrite(LED_PIN, HIGH);
        Serial.print("LED ACCESO");
                }
        else if (command = "spegni"){
        digitalWrite(LED_PIN, LOW);
        Serial.print("LED SPENTO");
                }
      else {
      Serial.println("Comando Sconosciuto");
      Serial.println(command);}
      
    }
}

i decided to make things more complex using strings

You are not using strings. You are using Strings. Not the same thing at all.

      if (command = "accendi"){

This is an assignment statement, not an equality test.

Review == and =

==
Arduino - If

A very basic way to control the arduino board LED via the serial monitor.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

Thank you all for answering my question.

There’s still one thing that is not clear to me… it’s zoomkat’s while loop

while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}

why is there a delay()?
I tried to figure it out, but i really couldn’t…
thank you again for the patience

To allow for the data to arrive completely.

TryingItOut:
I tried to figure it out, but i really couldn't...

Serial takes time to send. Without delays, it's possible for the loop() function to run hundreds of times between when bytes are received. Be weary, though, Zoomkat is the only regular that encourages and supports Strings and such type of Serial parsing.

TryingItOut:
Thank you all for answering my question.

There’s still one thing that is not clear to me… it’s zoomkat’s while loop

while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}

why is there a delay()?

To keep the controller from doing anything while waiting longer for the next serial character to arrive than it should, and save on actually coding an end-of-line check.

TryingItOut:
Thank you all for answering my question.

There's still one thing that is not clear to me... it's zoomkat's while loop

while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}

why is there a delay()?
I tried to figure it out, but i really couldn't...
thank you again for the patience

The arduino loops far faster than bytes arrive in the serial buffer. The delay slows the looping to allow the byte string to be captured into a String as it arrives, without exiting the byte capture loop. Usually serial strings sent have some type of end of data marker appended so a delay is not required.

Okay, thank everybody again for the quick and thorough answers, you helped a lot!

if u have any general suggestion for a newbie like me, i'll be happy to learn some more :slight_smile: