Serial.Find Problem

I’m trying to look for specific keywords in serial buffer in Arduino.

if (Serial.find(“SOMETHING”))
{
// do something
}
else if (Serial.find(“SOMETHINGELSE”))
{
// do another thing
}

But only the first if works. Even if I send “SOMETHINGELSE” it isn’t checked at all. Does find function clear buffer completely even if it can’t find anything ? If yes, what can i do in this situation?

It won't be checked, no. It'll wait until it finds the first one, and never get to the second one.

You'll have to check the data manually as it comes in.

majenko: It won't be checked, no. It'll wait until it finds the first one, and never get to the second one.

You'll have to check the data manually as it comes in.

But as far as i know timeout is 1 second. If it doesn't find the first, shouldn't else part be tested?

By the way, there is a default timeout of 1000ms on the .find() function.

So you're basically saying:

Wait until I see the string "SOMETHING", or for a maxium of 1 second, whichever is first. Did I see the string? No? Ok, wait for the string "SOMETHINGELSE", or for a maximum of 1 second, whichever is first.

It will only ever see "SOMETHINGELSE" and react to it if it arrives in the second 1000ms after the first .find() for "SOMETHING" has timed out.

You need to read the whole string in and then look at it to see what is in it and compare it to different things to find out what it matches.

Imagine living on a 1-way street of houses numbered 1 to 100, and you live at number 1. You get in the car and "Drive until you reach house number 186, or you reach the end of the road. If you reached the end of the road then keep driving until you reach house number 50, or until you run out of petrol". You're going to run out of petrol, because you drove straight past house number 50 while looking for the non-existent house number 186.

Now, if you had instead gone: "Ok, I need to get to house number 186 - oh, the street doesn't have a house 186, OK, I'll go to house 50 instead." it would have been better.