Serial.read

I have the following code:

int incomingByte = 0;	// for incoming serial data

void setup() {
	Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
        // initialize the digital pin as an output.
        pinMode(4, OUTPUT);
        pinMode(5, OUTPUT);
        pinMode(6, OUTPUT);
        pinMode(7, OUTPUT);
        pinMode(12, OUTPUT);
        pinMode(13, OUTPUT);
}

void loop() {

	// act on data only when you receive data:
	if (Serial.available() < 1) {
          return;
          }
        
	// read the incoming byte:
	incomingByte = Serial.read();

        //Red LED, Pin 13   
	if (incomingByte == 65){    // Capital " A "
          digitalWrite(13, HIGH);   // LED on
          delay (5000);             // delay in milliseconds
          digitalWrite(13, LOW);    // LED off
        }

I would like to read in ‘Yes’ instead of ‘A’. I tried changing 65 to ‘Yes’, but this did not work. Can someone point me in the right direction?

"Yes" is three characters, need to read in 3 and see if all three match.

if (Serial.available()>2){ // got at least 3 characters?
    if (Serial.read()=='Y'){
      if (Serial.read() == 'e'){ 
        if (Serial.read() == 's'){
     // do light stuff
     }
    }
   }
 }

There is a couple ways you can do this but first I need to clarify, Yes is not a single char so 'Yes' wont work, it's a char string(an array of chars).

1) Read in each char and store them in an array(I think it need to be NULL terminated) then use strcmp() to see if what you sent is a match to what you're looking for.

2) Use Serial.find() to go through the serial buffer and see if the chars 'Y','e','s' are found together.

Where is the “Yes” coming from? If it’s coming from the serial monitor, you can use the line ending to indicate when you have received a complete command. In that case you can buffer and process it like this:

// incomplete, untested
void handleSerial()
{
    const int BUFF_SIZE = 32; // make it big enough to hold your longest command
    static char buffer[BUFF_SIZE+1]; // +1 allows space for the null terminator
    static int length = 0; // number of characters currently in the buffer

    if(Serial.available())
    {
        char c = Serial.read();
        if((c == '\r') || (c == '\n'))
        {
            // end-of-line received
            if(length > 0)
            {
                handleReceivedMessage(buffer);
            }
            length = 0;
        }
        else
        {
            if(length < BUFF_SIZE)
            {
                buffer[length++] = c; // append the received character to the array
                buffer[length] = 0; // append the null terminator
            }
            else
            {
                // buffer full - discard the received character
            }
        }
    }
}

void handleReceivedMessage(char *msg)
{
	if(strcmp(msg, "Yes") == 0)
	{
		// handle the command "Yes"
	}
	else
	{
		// etc
	}
}

Wow, bit of a convoluted solution.

A little simpler:

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

void loop()
{
 if(Serial.available() > 2) // checks for at least 3 chars are entered
 {
  if(Serial.find("Yes"))
    Serial.println("Found it");
  else
    Serial.println("Not found");
 } 
}

HazardsMind: A little simpler:

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

void loop() { if(Serial.available() > 2) // checks for at least 3 chars are entered {   if(Serial.find("Yes"))     Serial.println("Found it");   else     Serial.println("Not found"); } }

this is nice, but is it extensible? It digests completely every byte?

what if he then says... "now I want I test for a 'no' ?"

It does kinda work if you send "YesnoYes" I've been playing around with it and for multiple strings, its not very responsive. but I never said it was the best way to solve the OP's issue.

Also PeterH made my first option already.

HazardsMind: It does kinda work if you send "YesnoYes" I've been playing around with it and for multiple strings, its not very responsive. but I never said it was the best way to solve the OP's issue.

Also PeterH made my first option already.

not intended to be confrontational... I was impressed to see you remember a function that read about and hadn't yet used!