(a) Suppose that x is declared as int and has the value 5. Fill in the following
ID: 3606821 • Letter: #
Question
(a) Suppose that x is declared as int and has the value 5. Fill in the following table indicating the number of bytes sent from the Arduino out of the serial port and the value(s) of those bytes. Pay close attention to the functions being called, especially print () vs. println(). Feel free to use the ASCll table repro- duced later in this exam, and provide all of the values in hex. Number of bytes (decimal, please) Byte value(s) (hex, please) Statement Serial.print ("x-") Serial.print (x); Serial.write(x);Explanation / Answer
Before starting the answer lets understand what is "Serial.print()" and "Serial.write()"
1. Serial.print(val,format)
It is a print function which prints data to the serial port as Human-readable ASCII text.
val ==> The value to be printed, It can be any data type
format ==> Specifies the format or the number base, the data to be printed (Like BIN or base 2, OCT or base 8, DEC or base 10, HEX or base 16)
Remember: Numbers and Floats are printed with ASCII character for each digit, Bytes are sent as single character, Strings and Characters are sent as it is.
EX : Serial.print(10) will print an ASCII encoded decimal of 10
Serial.print(10,HEX) will print an ASCII encoded hexadecimal of 10
2. Serial.write() : Writes binary data to the serial port
Format :
Serial.write(val) :- val is single byte value to send
Serial.write(str) :- Aseries of bytes to send
EX: Serial.write(10) // It sends a byte with value 10
Now Comes to your answer :
int X = 5; // As X is an interger with value 5
Option 1 :
Serial.print("X=") // Remember that the parameter is a string
// Will be printed "X=", So the number of byte in decimal is 2 and value in hexadecimal is "X="
Option 2 :
Serial.print(X) // Parameter is a integer
// Will be printed as 5, So the number of byte in decimal is 1 and value in hexadecimal is 5 as hexadecimal value of 5 is 5
Option 3:
Serial.write(X) // Parameter is integer
// It will send 5 as binary value so number of bytes is 1 and Byte value is 101
Final Result :
Statement Number of bytes in Decimal Byte Value in Hexadecimal
Serial.print("X=") 2 "X="
Serial.print(X) 1 5
Serial.write(X) 1 101
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.