Debian Codegraph @

examples/08.Strings/StringConstructors/StringConstructors.ino

raw
1 /*
2 String constructors
3
4 Examples of how to create Strings from other data types
5
6 created 27 Jul 2010
7 modified 30 Aug 2011
8 by Tom Igoe
9
10 This example code is in the public domain.
11
12 https://www.arduino.cc/en/Tutorial/BuiltInExamples/StringConstructors
13 */
14
15 void setup() {
16 // Open serial communications and wait for port to open:
17 Serial.begin(9600);
18 while (!Serial) {
19 ; // wait for serial port to connect. Needed for native USB port only
20 }
21
22 // send an intro:
23 Serial.println("\n\nString Constructors:");
24 Serial.println();
25 }
26
27 void loop() {
28 // using a constant String:
29 String stringOne = "Hello String";
30 Serial.println(stringOne); // prints "Hello String"
31
32 // converting a constant char into a String:
33 stringOne = String('a');
34 Serial.println(stringOne); // prints "a"
35
36 // converting a constant string into a String object:
37 String stringTwo = String("This is a string");
38 Serial.println(stringTwo); // prints "This is a string"
39
40 // concatenating two strings:
41 stringOne = String(stringTwo + " with more");
42 // prints "This is a string with more":
43 Serial.println(stringOne);
44
45 // using a constant integer:
46 stringOne = String(13);
47 Serial.println(stringOne); // prints "13"
48
49 // using an int and a base:
50 stringOne = String(analogRead(A0), DEC);
51 // prints "453" or whatever the value of analogRead(A0) is
52 Serial.println(stringOne);
53
54 // using an int and a base (hexadecimal):
55 stringOne = String(45, HEX);
56 // prints "2d", which is the hexadecimal version of decimal 45:
57 Serial.println(stringOne);
58
59 // using an int and a base (binary)
60 stringOne = String(255, BIN);
61 // prints "11111111" which is the binary value of 255
62 Serial.println(stringOne);
63
64 // using a long and a base:
65 stringOne = String(millis(), DEC);
66 // prints "123456" or whatever the value of millis() is:
67 Serial.println(stringOne);
68
69 // using a float and the right decimal places:
70 stringOne = String(5.698, 3);
71 Serial.println(stringOne);
72
73 // using a float and less decimal places to use rounding:
74 stringOne = String(5.698, 2);
75 Serial.println(stringOne);
76
77 // do nothing while true:
78 while (true);
79
80 }