/*
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.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/StringToInt
*/StringinString="";// string to hold input
String型
(クラス)の変数を定義し、初期化します。
setup()
20
21
22
23
24
25
26
27
28
29
30
31
voidsetup(){// Open serial communications and wait for port to open:
Serial.begin(9600);while(!Serial){;// wait for serial port to connect. Needed for native USB port only
}// send an intro:
Serial.println("\n\nString toInt():");Serial.println();}
voidloop(){// Read serial input:
while(Serial.available()>0){intinChar=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="";}}}