Strings in if() statements

Can anyone tell me why this line of code will compile correctly

String currentTime = String(hour()) + ":" + minute();

but the following code comes up with an error which says 'currentTime' is not declared in this scope? (I want to add a padding zero to the minutes when the minutes value is less than 10).

if (minute() <= 9) {
  String currentTime = String(hour()) + ":0" + minute(); // Pad single digits with leading zero
  }
else {  String currentTime = String(hour()) + ":" + minute(); }

What IDE version?

1.8.19

What if the minute changes between the two calls to minute()?

I didn't think about that! But would it throw the "not declared" error because of that subtlety of timing?

No. That is not the cause of your issue.

Please post a minimum complete example which compiles and demonstrates your issue.

3 Likes

This is the code from the Blynk RTC example. Lines of interest are 24 and 25,26. It will compile correctly in the current mode, but will throw an error with the commenting is reversed (24 commented out, and 25,26 active)

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "TMPxxxxxx"
#define BLYNK_TEMPLATE_NAME         "Device"
#define BLYNK_AUTH_TOKEN            "abcd1234"


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

BlynkTimer timer;

WidgetRTC rtc;

// Digital clock display of the time
void clockDisplay()
{
  String currentTime = String(hour()) + ":" + minute();
//  if(minute()<10) String currentTime = String(hour()) + ":0" + minute();
//  else  String currentTime = String(hour()) + ":" + minute();

  String currentDate = String(day()) + " " + month() + " " + year();

  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send time to the App
  Blynk.virtualWrite(V1, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V2, currentDate);
}

BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(BLYNK_AUTH_TOKEN);

  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

  // Display digital clock every 10 seconds
  timer.setInterval(10000L, clockDisplay);
}

void loop()
{
  Blynk.run();
  timer.run();
}

Please post the entire error message, verbatim.

Missing brace after 'else'

There's a lot of differences between the codes you show, but one reason could be that there is no 'String currentTime' declared outside of the braces within the if statements, so even if you do things with a local variable declared deep within braces, it gets destroyed at the end of the braces and isn't available outside of the braces.

You should declare an empty String currentTime outside the if(){...}else{...} and instead of re-declaring disposable variables within the if's braces, use the outer scope's variable.

The error is you are declaring a new instance of the "currentTime" variable by using String in front of it. Any time you put a type name (int, char, String, etc.) in front of a name, the compiler makes it a new variable of that type. In this case the two names clash.
Take the "String" out of both lines, that will allow the compiler to reference the declaration made in the line above those two.

  • Wes
1 Like
C:\Users\Brian\Documents\Arduino\RTC_time_padding\RTC_time_padding.ino: In function 'void clockDisplay()':
RTC_time_padding:33:16: error: 'currentDate' was not declared in this scope
   Serial.print(currentDate);
                ^
Multiple libraries were found for "Ethernet.h"
 Used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\libraries\Ethernet
 Not used: C:\Users\Brian\Documents\Arduino\libraries\Ethernet
exit status 1
'currentDate' was not declared in this scope

Braces on a single if or else line aren't necessary, but I did try and confirmed that it makes no difference.

Perfect!! Thanks Wes, much appreciated.

Cheers
Brian

Yes, provided that there isn't a closing brace. The inclusion of a declaration threw me off.

I normally also put braces on the if statements. It is just defensive programming, having used some pretty sketchy compilers 'back when'. And it helps ensure a later programmer will understand what you meant to do.

Cheers!

2 Likes

The if control structure is associated with pair of parentheses followed by sub-ordinate clause(s) being enclosed by pair of braces. For example:

if(condition)
{
     statement1;
     statement2;
}

That is one form, but if there is only one statement you can:

if(condition)
   statement1;
2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.