"if(Serial.read() == 'x')"

Hi guys, i would like to know what does this following line of code "if(Serial.read() == 'x')" do or what does it mean??

Hi guys, i would like to know what does this following line of code "if(Serial.read() == 'x')" do or what does it mean??

What part of it is less than clear? What have you looked up to understand it?

Serial.read() reads a value from the serial port, and returns that value. The if statement compares two values - one returned by Serial.read() and one hardcoded. The code after the if statement is, or is not, executed, depending on whether or not Serial.read() returned an 'x'.

WARNING - Here be Dragons There may be problems with data types, reading from the serial input when there is nothing to read and other traps for the unwary.

It reads the next byte from the incoming serial UART buffer, and compares it to the character 'x'. http://www.asciitable.com/

If they match, then there should be some following code to do some some action. It should follow a test to see that there is valid data available to be read:

if (Serial.available() >0){
  // got at least 1 byte, read it & act on it
  if (Serial.read() == 'x'){
  // perform some action
  }
  else{
  // maybe perform some other action
  }
} // end checking if serial data is available

Okay,understand..thanks a lot guys for explaining =)

Note that after comparing the value read with 'x' the value is discarded - if it wasn't 'x' you have no way to know what it was. If you wanted to do anything else with the received character in the 'non-x' case, you'd need to assign the result of read() to a variable and then use that variable in the if expression.

As in:

if (Serial.available() >0){
  // got at least 1 byte, read it & act on it
incomingByte = Serial.read();
  if (incomingByte == 'x'){
  // perform some action
  }
  else{
  // maybe perform some other action
 // what did we get instead? 
  Serial.println(incomingByte);
  }
} // end checking if serial data is available