A Complete Step by Step Tutorial On How To Use Arduino Serial Flush
A Complete Step by Step Tutorial On How To Use Arduino Serial Flush
Flush.
Moreover, I hope that’s gonna be my last lecture on Arduino Serial Port because I have
covered it in full detail. Although I am gonna summarize all the Arduino Serial Posts in a
single Post. You should also have a look at How to do Arduino Serial communication,
because in that tutorial, I have combined all Arduino Serial Projects. Anyways, let’s get
started with How to use Arduino Serial Flush:
Arduino Serial Flush is used to flush the data send through Arduino Serially.
When we send data on a serial port through arduino then we use the command
Serial.print or Serial. write.
So when the data is sent it is sent using interrupt and when we use Arduino Serial
Flush command then it makes sure that all the data is sent through serial port and
nothing’s left in the stream.
In simple words, it clears the serial data stream and kind of refresh it and also makes
you ready to send next data.
Here’s the syntax to use Arduino Serial Flush command:
Serial.flush();
It doesn’t return anything that’s why we haven’t assigned any variable to it.
It’s just a simple function which clears the data on transmitting pin of Arduino.
Now, if you want to remove that on receiving pin of Arduino, then you just need to
write this command:
while(Serial.available());
But you make sure while using the above command because if you are receiving the
data continuously then your code will remain stuck and won’t move forward unless
you got the complete data.
So, here’s the small code which I have also used in the Arduino Serial Write tutorial
but now I am using Arduino Serial Flush command too.
So, here’s the code:
#include
1
2
#include
3
// initialize the library with the numbers of the interface pins
4
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
5
void setup() {
6
// set up the LCD's number of columns and rows:
7
lcd.begin(20, 4);
8
// Print a message to the LCD.
9
lcd.setCursor(1,0);
10
lcd.print("www.TheEngineering");
11
lcd.setCursor(4,1);
12
lcd.print("Projects.com");
13
lcd.setCursor(1,0);
14
Serial.begin(9600);
15
// lcd.clear();
16
17
Serial.write("www.TheEngineeringProjects.com");
18
Serial.flush();
19
}
20
void loop()
21
{
22
}
23
24
In the above code I have simply added the Arduino Serial Flush code and now it will
take a little more time to complete because now it will make sure that all the data has
been sent.
So, that’s all for today. I hope you guys have enjoyed this short Arduino Serial Flush tutorial.
If you have any questions, please ask in the comments. Take care