Description

Serial.find() reads data from the serial buffer until the target is found.

Serial.find() inherits from the stream utility class.

Syntax

Use the following functions to search for a string in incoming serial data:

  • Serial.find(target)
  • Serial.find(target, length)

Parameters

The function admits the following objects and parameters:

  • Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
  • target: the string to search for. Allowed data types: char.
  • length: length of the target. Allowed data types: size_t.

Returns

The function returns true if the target string is found, false if it times out. Data type: bool.

Example Code

The following code waits for the target string OK to appear:

void setup() {
  Serial.begin(9600);
  while (!Serial); // Wait for Serial (needed on some boards)

  Serial.println("Type 'OK' to continue:");
}

void loop() {
  if (Serial.available()) {
    // Look for the word "OK" in the incoming serial data
    if (Serial.find("OK")) {
      Serial.println("Received OK!");
    } else {
      Serial.println("Didn't find OK, try again.");
    }
  }

  delay(1000);
}

See also