Description
The Serial.parseInt() function reads input from the serial monitor one character at a time until it detects a non-numeric character. It then converts the numeric input to an integer.
If the function does not receive any input, it returns zero. If it receives an invalid input (e.g., a character that is not a number), it returns the last valid integer that it has read.
It also terminates when it reaches the times out (Serial.setTimeout()). This function is inherited from the stream class in Arduino programming.
Syntax
The syntax of the Serial.parseInt() function is straightforward. It is written as follows:
Serial.parseInt(lookahead)
Serial.parseInt(lookahead, ignore)
These are methods in the Arduino programming language used to read and convert characters from the serial communication port into integers.
- Serial.parseInt() reads characters from the serial buffer until it encounters a non-numeric character, and then converts the accumulated digits into an integer.
- Serial.parseInt(lookahead) works the same way as Serial.parseInt(), but looks ahead for the next character to determine when to stop reading.
- Serial.parseInt(lookahead, ignore) works the same way as Serial.parseInt(lookahead), but also ignores the specified character(s) when encountered during parsing.
Parameters
In this function, there are no parameters. It reads the input from the serial monitor and converts to an integer.
lookahead: It is the mode which is used by the Arduino program to look ahead for an integer in the coming DataStream. The allowed data types are LookaheadMode.
Following is the list of allowed values:
- SKIP_ALL: This is default mode for the function and all characters are ignored except the digits or a minus sign.
- SKIP_NONE: During this mode nothing is skipped, and stream is parse as it is unless the waiting character is valid.
- SKIP_WHITESPACE: In this mode of function all spaces including tabs and line feed, or carriage return are skipped.
ignore: This will skip the character specified inside the search. The allowed data type is char. For example, it can skip the thousand dividers.
Returns
This function gives the next valid integer. The Data type of returned value is long.
If no numeric input is given to this function, it will return the last valid integer value that it has read. If no input is received, the function returns zero.
Arduino Example Code
Below code cover the use of Serial.parseInt() function:
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() == 0) {
// Wait for user input
}
val = Serial.parseInt();
Serial.print("You entered: ");
Serial.println(val);
}
In this code a while loop is used that waits for user input from the serial monitor. Once it receives input, it uses the Serial.parseInt() function to convert it to integer. It then prints the entered value to the serial monitor.
Output
In output when we send a digit value that number is returned but when we send an alphabet a zero is returned.
Limitations of Serial.parseInt()
The Serial.parseInt() function is limited to integer values. The function only supports input values that are integers. It cannot handle floating-point or decimal numbers.
Difference between Serial.parseInt() and Serial.read() Arduino function
The Serial.parseInt() and Serial.read() functions are both built-in functions in the Arduino programming language that are used to read input from the serial monitor. However, they differ in their behavior and the type of data they return.
Serial.read()
The Serial.read() function reads a single byte of data from the serial monitor and returns it as an integer value.
The function does not take any parameters. It reads a single byte of data from the serial monitor and returns it as an integer value between 0 and 255. In case of no data, it will return -1.
Serial.parseInt()
On the other hand, the Serial.parseInt() function reads a sequence of ASCII characters from the serial monitor and converts them to an integer value.
The function does not take any parameters. It reads a sequence of ASCII characters from the serial monitor and converts them to an integer value. If no numeric character is received the function will give the last valid integer value that it has read. If no input is received, the function returns zero.
Conclusion
The Serial.parseInt() function reads input from the serial monitor one character at a time until it detects a non-numeric character. It then converts the numeric character to integer. For more detail on this function the example code given in the article will guide you.