1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-30 16:24:09 +03:00

added two examples to String examples for String to integer conversion

This commit is contained in:
Tom Igoe
2010-11-29 13:59:09 -05:00
parent 448222e4b6
commit d6b457967f
2 changed files with 283 additions and 0 deletions

View File

@ -0,0 +1,50 @@
/*
String to Integer conversion
Reads a serial input string until it sees a newline, then converts
the string to a number if the characters are digits.
The circuit:
No external components needed.
created 29 Nov 2010
by Tom Igoe
This example code is in the public domain.
*/
// include the character conversion functions:
#include <WCharacter.h>
String inString = ""; // string to hold input
void setup() {
// Initialize serial communications:
Serial.begin(9600);
}
void loop() {
// Read serial input:
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string,
// then the string's value:
if (inChar == '\n') {
Serial.print("Value:");
Serial.println(inString.toInt());
Serial.print("String: ");
Serial.println(inString);
// clear the string for new input:
inString = "";
}
}
}