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

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

Adding new version

File size: 1.3 KB
Line 
1/*
2  String length()
3
4 Examples of how to use length() in a String.
5 Open the Serial Monitor and start sending characters to see the results.
6
7 created 1 Aug 2010
8 by Tom Igoe
9
10 http://www.arduino.cc/en/Tutorial/StringLengthTrim
11
12 This example code is in the public domain.
13 */
14
15String txtMsg = "";                         // a string for incoming text
16int lastStringLength = txtMsg.length();     // previous length of the String
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  length():");
27  Serial.println();
28}
29
30void loop() {
31  // add any incoming characters to the String:
32  while (Serial.available() > 0) {
33    char inChar = Serial.read();
34    txtMsg += inChar;
35  }
36
37  // print the message and a notice if it's changed:
38  if (txtMsg.length() != lastStringLength) {
39    Serial.println(txtMsg);
40    Serial.println(txtMsg.length());
41    // if the String's longer than 140 characters, complain:
42    if (txtMsg.length() < 140) {
43      Serial.println("That's a perfectly acceptable text message");
44    } else {
45      Serial.println("That's too long for a text message.");
46    }
47    // note the length for next time through the loop:
48    lastStringLength = txtMsg.length();
49  }
50}
Note: See TracBrowser for help on using the repository browser.