In lesson 3.3, we send and receive single characters to control a LED. Sometimes, single characters are not enough, and we need to apply some more complex commands or send data that consists of more than one character.
In this tutorial, we discuss a couple of methods for receiving and sending multiple characters, starting with the functions that are build-in the Arduino Language and moving to a function made by the community.
A common mistake that a lot of beginners make when they send data from the serial monitor to an Arduino Board or from board to board is that the data arrives at the same time. IT DOES NOT.
When you use serial communication and send the characters string “HELLO” to another board, the characters are received one character at a time.
If you would like to check “HELLO”, for instance, you need to check each character on the receiving Arduino Board and put them in order. There are a number of ways to solve this problem. Let’s start with the command Serial.readBytesUntil().
Serial.readBytesUntil() reads characters from the serial buffer into an array. If the determined length is reached. The function terminates if it times out or if the terminator character is detected.
Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commission if you buy a product from the affiliated websites by clicking on the links provided above.
Example code with the serial monitor
The above example is very basic and sets the buffer to a maximum length of 30 characters. You could enter more than 30 characters which will lead that the sketch will misbehave.
Give the above a try. It should work quite well as long as you remember the limitations.
// Arduino serial communication sketch
// example of Serial.readBytesUntil() function
int length = 30;
char buffer ;
char termChar = 'n';
Serial.println("Set line endings Newline");
Serial.println("Please enter your name and click Send");