Understanding data types is especially important if you wish to use serial communication to send data to your Arduino and have the ATmega328 act on this data. Serial data is read from the serial buffer using a sequence of commands like this.
if (Serial.available()>0) { // there are bytes in the serial buffer to read
while(Serial.available()>0) { // every time a byte is read it is expunged
// from the serial buffer so keep reading the buffer until all the bytes
// have been read.
myByte = Serial.read(); // read in the next byte
}
}
It is important to know that characters typed into the serial terminal (or sent from on Arduino to another via serial) are interpreted as ASCII (American Standard Code for Information Interchange) characters and encoded with the decimal number corresponding to the character on the ASCII table. The following is the ASCII table from lookuptables.com, a great source of all sorts of computer-related reference tables.
To give you an example, if you were to send the letter A
to an Arduino via serial and saved the incoming byte as myByte
using code like that given above, the numerical decimal value of myByte would be 65. Try the following code and enter A
in the serial terminal and you will notice that the number 65 is returned to the terminal and output to the screen because the Serial.print()
function by default prints the decimal value of the byte.
byte myByte;
void setup(void){
Serial.begin(9600); // begin serial communication
}
void loop(void) {
if (Serial.available()>0) { // there are bytes in the serial buffer to read
while(Serial.available()>0) { // every time a byte is read it is expunged
// from the serial buffer so keep reading the buffer until all the bytes
// have been read.
myByte = Serial.read(); // read in the next byte
}
Serial.println(myByte); // print byte to screen
delay(100); // a short delay
}
}
If you want to instead print the ASCII character corresponding to myByte, use the Serial.write()
function instead. You can also use an additional argument to tell the Serial.print()
function to display the byte using the decimal, hexadecimal, octadecimal, or binary base numeral systems. The following code will take a byte read in from the serial terminal and print it to the terminal using these various bases.
byte myByte;
void setup(void){
Serial.begin(9600); // begin serial communication
}
void loop(void) {
if (Serial.available()>0) { // there are bytes in the serial buffer to read
while(Serial.available()>0) { // every time a byte is read it is expunged
// from the serial buffer so keep reading the buffer until all the bytes
// have been read.
myByte = Serial.read(); // read in the next byte
}
Serial.println(myByte, DEC); // base 10, this is the default
Serial.println(myByte, HEX); // base 16
Serial.println(myByte, OCT); // base 8
Serial.println(myByte, BIN); // base 2
Serial.write(myByte); // ASCII character
Serial.println(); // carriage return
delay(100); // a short delay
}
}
Assignment
Use the String
class (you can think of this as a data type called a string) to modify the code above to read in a sequence of characters and save these characters as a string then print this string to the terminal. To help you out, know that strings are defined as follows and are surrounded by quotation marks.
String myString = "One or more words";
A character can be concatenated to the end of a string using the following command.
myString.concat(' or numbers') // now myString is "One or more words
// or numbers"
To ensure that a byte read in from the serial buffer is interpreted as a character, precede a call to this character with the (char)
command, which converts a number to the corresponding ASCII character.
Good luck.
Permalink //
Thank you very much! This made my project for my exam much easier! You helped me a lot. Thank you again. 😀
Permalink //
What is I want to use the letter for an other purpose, is it possible to cupture the if condition so that if my condition is to process a function whenever I load a different alphabet. I want to link different alphabet to a function. So that whenever I type a alphabet it, it should do a required function linked to it! Please help me around!
Permalink //
Purpose of carriage return Serial.println();
Permalink //
is it possible to print out as HEX but not in ASCII?
I am reading in data (digital.Read) and then I took the data (x,y) for some calculation
and the result of x and y has to be send in HEX, 16 bits.
Can Arduino do that?