Project 2.02 Talk Back
The serial monitor works both ways. In this project we send messages to the Arno through the serial monitor. Messages are entered in the box at the top of the serial monitor and sent once the Enter key is hit. Our sketch returns the message through the serial monitor normally and backwards. Let’s look at the code and then see how it works.
Concepts: Serial object, char array
Circuits: none
Concepts: Serial object, char array
Circuits: none
The sketch queries the user every five seconds. The timer is set by setting the variable now to millis() + 5000:
if(now < millis()){
Serial.println("Type a message in the Serial
Monitor and hit Enter...");
now = millis() + 5000;
}
When a line is sent through the serial monitor, it is stored temporarily in a buffer. The serial buffer can hold up to 128 bytes (1 character = 1 byte). Characters can be lost if you try to send more than 128 before reading them with a Serial.read() statement. The command Serial.available returns the number of bytes in the buffer. If the value is greater than zero, it means that there’s a message waiting for us. The command Serial.read reads the bytes one at a time. We create a char array, getChars, of the appropriate length to hold the bytes:
nBytes = Serial.available();
if(nBytes > 0){
char getChars[nBytes];
for(int k = 0; k<nBytes;k++){
getChars[k] = Serial.read();
}
The array getChar is sent back through the serial monitor in its entirety. Each character, beginning with the last, is then sent individually, but appears on a single line since we use the Serial.print command. A Serial.println command starts a new line:
//print what we just read
Serial.println(getChars);
//now backwards one character at a time
for(int j = nBytes - 1; j > -1; j = j - 1){
Serial.print(getChars[j]);
}
Serial.println();
if(now < millis()){
Serial.println("Type a message in the Serial
Monitor and hit Enter...");
now = millis() + 5000;
}
When a line is sent through the serial monitor, it is stored temporarily in a buffer. The serial buffer can hold up to 128 bytes (1 character = 1 byte). Characters can be lost if you try to send more than 128 before reading them with a Serial.read() statement. The command Serial.available returns the number of bytes in the buffer. If the value is greater than zero, it means that there’s a message waiting for us. The command Serial.read reads the bytes one at a time. We create a char array, getChars, of the appropriate length to hold the bytes:
nBytes = Serial.available();
if(nBytes > 0){
char getChars[nBytes];
for(int k = 0; k<nBytes;k++){
getChars[k] = Serial.read();
}
The array getChar is sent back through the serial monitor in its entirety. Each character, beginning with the last, is then sent individually, but appears on a single line since we use the Serial.print command. A Serial.println command starts a new line:
//print what we just read
Serial.println(getChars);
//now backwards one character at a time
for(int j = nBytes - 1; j > -1; j = j - 1){
Serial.print(getChars[j]);
}
Serial.println();