What is Arduino Serial find Function
- Arduino Serial find Function reads the received buffer and test for specified string or word present or not.
- This function returns a boolean value.
- If specified string is present in the buffer than the function return true, otherwise return false.
- By default the find function wait for one second before return false, if string is not found.
- The Serial.read() function read one character at a time whereas the find able to test string or word.
To test for word with the help of Serial.read() is really complex, and find function is a easy solution for testing multiple character response from devices.
Syntax of find function
int x = Serial.find(“on”);
If “on” received within one second of time the function return true, otherwise return false.
The find function is mostly used with the if logic, to perform some task in case target string received.
How find Function Works
The function continuously read the received data from the buffer and if target string found than return true.
And if the target string is not received until one second than it will return false, it means the function will wait for one second to receive the target word or string.
Caution: The default wait time is one second, which can be modified with the help of function Serial.setTimeout().
Arduino Serial.find Example
In this small project code we will on the LED on pin number 13 when we receive the “on”. And turn off the LED if received anything else.
Because the “on” is a multiple characters word, the Serial.read() function not able to perform this task easily.
Example 1 – Serial.find() function code to control the LED from PC.
/* Control the LED on pin 13 with PC from serial monitor. Author: Nilesh Chaurasia https://elextutorial.com */ void setup() { Serial.begin(9600); // Set the baud rate to 9600 setMode(13,OUTPUT); digitalWrite(13,LOW); } void loop() { if(Serial.available()) // Check if received { if(Serial.find("on"))// test the received buffer for "on" { digitalWrite(13,HIGH); }else { digitalWrite(13,LOW); } } }
In this program we have used Serial.available() function to check first something is received. The find function is used to directly test the “on” string received in the serial buffer or not.
If received “on” we have used digitalWrite() function to turn ON the LED.
Arduino Serial timeout Function ( Serial.setTimeout() )
The default wait time of the Serial.find() function is one second, with the help of Serial.setTimeout() you can modify the timeout of serial functions.
Syntax of Serial.setTimeout() function
Serial.setTimeout(5000);// Set the timeout to 5 second.
In the set timeout function you have to specify the time in milli-seconds ( ms ). The default value is 1000 for timeout.
I feel that is one of the so much important info for me. And i’m happy studying your article. However wanna remark on few normal things, The website style is ideal, the articles is truly great : D. Good job, cheers|
Thankyou for this wonderful post, I am glad I discovered this site on yahoo.
Good day! This is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a outstanding job!