I'm setting a string variable (String recipient) inside an if statement. Problem is, I'm trying to use that variable outside the IF statement. I'm new to the arduino but normally I wouldn't consider this to be a problem.
But when I try to print the variable outside the IF statement, it's set to "nothing". Doesn't make sense to me - why wouldn't a variable survive outside an IF statement?
String recipient is declared as a global variable. I'm running the arduino code on an ESP8266 if that's relevant to know.
I hope some of you clever guys can spot my mistake?
Thanks in advance.
Cheers
Serial.print("Recipient 1: ");
Serial.println(Settings.cpsms_receiver1); // Outputs int e.g. 4500000000
Serial.print("Recipient 2: ");
Serial.println(Settings.cpsms_receiver2); // Outputs int e.g. 4500000000
if ( Settings.cpsms_receiver1 != NULL ) {
String recipient = String(Settings.cpsms_receiver1);
Serial.println("Recipient 1: OK"); // This is printed
}
if (( Settings.cpsms_receiver1 != NULL ) && ( Settings.cpsms_receiver2 != NULL )) {
String recipient = String(Settings.cpsms_receiver1 + "," + Settings.cpsms_receiver2);
Serial.println("Recipient 2: OK"); // This is printed if 2 recipients are set
}
Serial.print("Recipients: ");
Serial.println(recipient); // Outputs nothing ? Why ???
This declares a variable, as per guix post. If you have a global of the same name the local name overrides the global. Bad practice to use the same name for this reason.
This declares a variable, as per guix post. If you have a global of the same name the local name overrides the global. Bad practice to use the same name for this reason.
I understand - I wasn't trying to declare the variable twice, but only to set it's value inside the IF statement.
As I said, rookie mistake