2011-08-30 21:33:32 +02:00
|
|
|
/*
|
|
|
|
String constructors
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
Examples of how to create strings from other data types
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
created 27 July 2010
|
|
|
|
modified 30 Aug 2011
|
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2015-05-20 17:10:06 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/StringConstructors
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
This example code is in the public domain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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) {
|
|
|
|
; // wait for serial port to connect. Needed for Leonardo only
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// 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
|
|
|
}
|