source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/08.Strings/StringToInt/StringToInt.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 1.2 KB
Line 
1/*
2  String to Integer conversion
3
4 Reads a serial input string until it sees a newline, then converts
5 the string to a number if the characters are digits.
6
7 The circuit:
8 No external components needed.
9
10 created 29 Nov 2010
11 by Tom Igoe
12
13 This example code is in the public domain.
14 */
15
16String inString = "";    // string to hold input
17
18void setup() {
19  // Open serial communications and wait for port to open:
20  Serial.begin(9600);
21  while (!Serial) {
22    ; // wait for serial port to connect. Needed for native USB port only
23  }
24
25  // send an intro:
26  Serial.println("\n\nString toInt():");
27  Serial.println();
28}
29
30void loop() {
31  // Read serial input:
32  while (Serial.available() > 0) {
33    int inChar = Serial.read();
34    if (isDigit(inChar)) {
35      // convert the incoming byte to a char
36      // and add it to the string:
37      inString += (char)inChar;
38    }
39    // if you get a newline, print the string,
40    // then the string's value:
41    if (inChar == '\n') {
42      Serial.print("Value:");
43      Serial.println(inString.toInt());
44      Serial.print("String: ");
45      Serial.println(inString);
46      // clear the string for new input:
47      inString = "";
48    }
49  }
50}
51
Note: See TracBrowser for help on using the repository browser.