title: How to compare string from Serial.read()?
body: <p>I have this code below where I got from <a href="http://forum.arduino.cc/index.php/topic,204484.0.html" rel="noreferrer">this forum</a> that I followed through. It did not work for me but they claim that the code is fine. I already tried several string comparison methods such as <code>string.equals(string)</code> and the standard <code>==</code> operator, still with no luck.</p>
  
  <pre><code>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() &gt;0) {
      if (readString == "on") {
        Serial.println("switching on");
        digitalWrite(ledPin, HIGH);
      }
      if (readString == "off") {
        digitalWrite(ledPin, LOW);
      }
      readString="";
    } 
  }
  </code></pre>