GETTING STARTED WITH ARDUINO PROGRAMMING PART 4

PAGE 4

Serial.available:

Get the number of bytes (characters) available for reading from the serial port. This is data that’s already arrived and stored in the serial receive buffer (which holds 64 bytes). available() inherits from the Stream utility class.

Syntax: Serial.available()

Arduino Mega only: Serial1.available(), Serial2.available(), Serial3.available()

Parameters:

  1. None
  2. Returns: The number of bytes available to read

Serial.write:

Get the number of bytes (characters) available for reading from the serial port. This is data that’s already arrived and stored in the serial receive buffer (which holds 64 bytes). available() inherits from the Stream utility class.

Syntax: Serial.available()

Arduino Mega only: Serial1.available(), Serial2.available()
Serial3.available()

Parameters:

  1. None
  2. Returns: The number of bytes available to read

Serial.print:

Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is.

For example:

  • Serial.print(78) gives “78”
  • Serial.print(1.23456) gives “1.23”
  • Serial.print(‘N’) gives “N”
  • Serial.print(“Hello world.”) gives “Hello world.”

An optional second parameter specifies the base (format) to use; permitted values are BIN (binary, or base 2), OCT (octal, or base 8), DEC (decimal, or base 10), HEX (hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of decimal places to use. 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 pass flash-memory based strings to Serial.print() by wrapping them with F().

For example:

Serial.print(F(“Hello World”))

“Note:To send a single byte, use Serial.write(),”

Syntax: Serial.print(val)
Serial.print(val, format)

Parameters:

  1. val: the value to print – any data type
  2. format: specifies the number base (for integral data types) or number of decimal places (for floating point types)
  3. Returns: size_t (long): print() returns the number of bytes written, though reading that number is optional

Goto Page 3 Goto Page 5

Leave a Reply