Serial.parseInt()

Descripción

Busca el siguiente número int válido en la entrada serie.

Serial.parseInt() se hereda de la clase Stream.

En particular:

  • Los caracteres iniciales que no son dígitos o un signo menos, se omiten; Detiene el análisis cuando no hay caracteres leídos durante un tiempo de espera configurable, o se lee un dato que no es un dígito;
  • Si no se leen dígitos válidos durante el tiempo de espera (ver Serial.setTimeout()), devuelve 0;

Sintaxis

Serial.parseInt()

Serial.parseInt(char skipChar)

Solo Arduino Mega:

Serial1.parseInt()

Serial2.parseInt()

Serial3.parseInt()

Parámetros

  • skipChar: se utiliza para saltar el carácter indicado en la búsqueda. Se utiliza por ejemplo para saltar el divisor de miles.

Retornos

  • Long: el siguiente int válido

Ejemplo

void setup() { Serial.begin(9600); } void loop() { if (Serial.available() > 0) { long myInt = Serial.parseInt(SKIP_ALL, '\n'); // prints the received integer Serial.print("I received: "); Serial.println(myInt); } }
  • Compile and upload the above code to Arduino
  • Open Serial Monitor and select Newline option
  • Type -125 on Serial Monitor and click Send button:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Ln 11, Col 1
Arduino Uno on COM15
2
  • The result on Serial Monitor:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
I received: -125
Ln 11, Col 1
Arduino Uno on COM15
2

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .

※ OUR MESSAGES