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(); }
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();
}
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.
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
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.