Serial.print()
Send data to the serial ports, without break.
- Description
Send data to the serial ports in the ASCII code format which can be understood, and this function has many formats. Each digit of the integer will be sent in the form of ASCII code. So do the floating-point numbers, and keep two decimal places by default. Byte type data will be sent in the form of single character. Characters and character strings will be sent in the corresponding form. For example:
Serial.print(78) send "78" Serial.print(1.23456) send "1.23" Serial.print('N') send "N" Serial.print("Hello world.") send "Hello world."
You can choose the second parameter to specify the format of the data. The values allowed:BIN (binary), OCT (octal), DEC (decimal), and HEX (hexadecimal). For floating-point numbers, this parameter specifies the decimal digits. For example:
Serial.print(78, BIN) gives "1001110" Serial.print(78, OCT) gives "116" Serial.print(78, DEC) gives "78" Serial.print(78, HEX) gives "4E" Serial.println(1.23456, 0) gives "1" Serial.println(1.23456, 2) gives "1.23" Serial.println(1.23456, 4) gives "1.2346"
You can use F() to pack the character string to be sent to flash memory. For example:
Serial.print(F(“Hello World”))
To send a single byte data, please use Serial.write().
- Syntax:
Serial.print(val) Serial.print(val, format)
- Parameters:
val: the data to be sent(of any data type)
format: to specify the base of the number(for integer numbers) or the decimal digits(for the floating point numbers).
- Returned valuse:<>
size_t (long): print() returns the number of sent bytes(the return value can be discarded).
- For example:
/* Uses a FOR loop for data and prints a number in various formats. */ int x = 0; // variable void setup() { Serial.begin(9600); // open the serial port at 9600 bps: } void loop() { // print labels Serial.print("NO FORMAT"); // prints a label Serial.print("\t"); // prints a tab Serial.print("DEC"); Serial.print("\t"); Serial.print("HEX"); Serial.print("\t"); Serial.print("OCT"); Serial.print("\t"); Serial.print("BIN"); Serial.print("\t"); for(x=0; x< 64; x++){ // only part of the ASCII chart, change to suit // print it out in many formats: Serial.print(x); // print as an ASCII-encoded decimal - same as "DEC" Serial.print("\t"); // prints a tab Serial.print(x, DEC); // print as an ASCII-encoded decimal Serial.print("\t"); // prints a tab Serial.print(x, HEX); // print as an ASCII-encoded hexadecimal Serial.print("\t"); // prints a tab Serial.print(x, OCT); // print as an ASCII-encoded octal Serial.print("\t"); // prints a tab Serial.println(x, BIN); // print as an ASCII-encoded binary // then adds the carriage return with "println" delay(200); // delay 200 milliseconds } Serial.println(""); // prints another carriage return }