2011-08-30 21:33:32 +02:00
|
|
|
/*
|
2017-07-15 00:30:34 +02:00
|
|
|
String constructors
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
Examples of how to create Strings from other data types
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
created 27 Jul 2010
|
2017-07-15 00:30:34 +02:00
|
|
|
modified 30 Aug 2011
|
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
This example code is in the public domain.
|
2017-07-12 22:18:23 +02:00
|
|
|
|
|
|
|
http://www.arduino.cc/en/Tutorial/StringConstructors
|
2017-07-15 00:35:58 +02:00
|
|
|
*/
|
2011-08-30 21:33:32 +02:00
|
|
|
|
|
|
|
void setup() {
|
2012-04-06 12:59:54 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2011-08-30 21:33:32 +02:00
|
|
|
Serial.begin(9600);
|
2012-04-06 20:00:31 +02:00
|
|
|
while (!Serial) {
|
2015-09-21 14:44:19 +02:00
|
|
|
; // wait for serial port to connect. Needed for native USB port only
|
2012-04-06 20:00:31 +02:00
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-04-06 20:00:31 +02:00
|
|
|
// send an intro:
|
2012-04-06 12:59:54 +02:00
|
|
|
Serial.println("\n\nString Constructors:");
|
2012-04-06 20:00:31 +02:00
|
|
|
Serial.println();
|
2011-08-30 21:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2012-04-06 12:59:54 +02:00
|
|
|
// using a constant String:
|
2013-10-21 09:58:40 +02:00
|
|
|
String stringOne = "Hello String";
|
2012-04-06 12:59:54 +02:00
|
|
|
Serial.println(stringOne); // prints "Hello String"
|
|
|
|
|
|
|
|
// converting a constant char into a String:
|
2013-10-21 09:58:40 +02:00
|
|
|
stringOne = String('a');
|
2012-04-06 12:59:54 +02:00
|
|
|
Serial.println(stringOne); // prints "a"
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2012-04-06 12:59:54 +02:00
|
|
|
// converting a constant string into a String object:
|
2013-10-21 09:58:40 +02:00
|
|
|
String stringTwo = String("This is a string");
|
2012-04-06 12:59:54 +02:00
|
|
|
Serial.println(stringTwo); // prints "This is a string"
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2012-04-06 12:59:54 +02:00
|
|
|
// concatenating two strings:
|
2013-10-21 09:58:40 +02:00
|
|
|
stringOne = String(stringTwo + " with more");
|
2012-04-06 12:59:54 +02:00
|
|
|
// prints "This is a string with more":
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println(stringOne);
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2012-04-06 12:59:54 +02:00
|
|
|
// using a constant integer:
|
2013-10-21 09:58:40 +02:00
|
|
|
stringOne = String(13);
|
2012-04-06 12:59:54 +02:00
|
|
|
Serial.println(stringOne); // prints "13"
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2012-04-06 12:59:54 +02:00
|
|
|
// using an int and a base:
|
2013-10-21 09:58:40 +02:00
|
|
|
stringOne = String(analogRead(A0), DEC);
|
2012-04-06 12:59:54 +02:00
|
|
|
// prints "453" or whatever the value of analogRead(A0) is
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println(stringOne);
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2012-04-06 12:59:54 +02:00
|
|
|
// using an int and a base (hexadecimal):
|
2013-10-21 09:58:40 +02:00
|
|
|
stringOne = String(45, HEX);
|
2012-04-06 12:59:54 +02:00
|
|
|
// prints "2d", which is the hexadecimal version of decimal 45:
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println(stringOne);
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2012-04-06 12:59:54 +02:00
|
|
|
// using an int and a base (binary)
|
2013-10-21 09:58:40 +02:00
|
|
|
stringOne = String(255, BIN);
|
2012-04-06 12:59:54 +02:00
|
|
|
// prints "11111111" which is the binary value of 255
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println(stringOne);
|
2012-04-06 12:59:54 +02:00
|
|
|
|
|
|
|
// using a long and a base:
|
|
|
|
stringOne = String(millis(), DEC);
|
2013-10-21 09:58:40 +02:00
|
|
|
// prints "123456" or whatever the value of millis() is:
|
|
|
|
Serial.println(stringOne);
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2017-07-12 19:58:31 +02:00
|
|
|
// using a float and the right decimal places:
|
2015-06-01 09:48:28 +02:00
|
|
|
stringOne = String(5.698, 3);
|
|
|
|
Serial.println(stringOne);
|
|
|
|
|
2017-07-12 19:58:31 +02:00
|
|
|
// using a float and less decimal places to use rounding:
|
2015-06-01 09:48:28 +02:00
|
|
|
stringOne = String(5.698, 2);
|
|
|
|
Serial.println(stringOne);
|
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
// do nothing while true:
|
2013-10-21 09:58:40 +02:00
|
|
|
while (true);
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2012-04-06 12:59:54 +02:00
|
|
|
}
|