I'm writing code in the Arduino (1.6.5) environment. In my code, I want to be able to define a string value, then use it and also Serial.println() it to the serial console.
For example:
#define THEVAL 12345 // Define the value
...
v = v + THEVAL; // Use the value in code.
...
Serial.println("The value is: #THEVAL"); // Show the value to user (for debugging)
However, the compiler doesn't replace constants inside quoted strings. I also tried this (C++ stringification) which indicates that you place the constant outside the quoted string
#define THEVAL 12345
...
Serial.println("This is the value: " #THEVAL);
but that yields a "Stray # character" error in the compiler.
I'd appreciate any insight! Thanks!
EDIT: ODD BEHAVIOR
On testing I discovered the following: (Note: the IP address uses commas to separate the octets because each octet is passed as a separate parameter to the EthernetServer.begin in a byte array (byte ip[] = { a, b, c, d })
#define IP_ADDRESS 192,168,1,1
#define IP_ADDRESS_STRING(a,b,c,d) xstr(a)"."xstr(b)"."xstr(c)"."xstr(d)
#define xstr(a) str(a)
#define str(a) #a
If I do the following, I get the error "IP_ADDRESS_STRING requires 4 arguments, but only one given"
debug("IP Address is: " IP_ADDRESS_STRING(IP_ADDRESS));
but if I do the following, I get the error "macro 'str' passed 4 arguments, but just takes 1"
debug("IP ADDRESS: " xstr(IP_ADDRESS));
But if I do this, it works:
String ipAddressString(int a, int b, int c, int d)
{
return String(a) + "." + String(b) + "." + String(c) + "." + String(d);
}
debug("IP Address is: " + ipAddressString(IP_ADDRESS));
I'm confused - why does one macro consider IP_ADDRESS to be a single argument, and the other macro sees it as 4 arguments, while the function works correctly: it sees 4 arguments?