Switch - problem with case position in code.

Hello to everyone! This is my first post, but I am reading the forum quite often.

So I have written small Switch case, but the problem is with case 0:
If the case 0 is in the first position in my code i.e 0,1,2....n then my LCD is not displaying anything.
In the code below my case 0 is at 3rd place and if by pressing UP button i make menu variable to 3 - LCD is blank. Everything below 3 is ok. The code in Case 0 is displaying date and time, if I remove it from the case then everything is working correctly.
I would like to know why this printDateTime function is making such a problem. I haven't written it by myself, it is simply copy pasted from RTC examples.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 4);

#include <ThreeWire.h>
#include <RtcDS1302.h>
ThreeWire myWire(44, 45, 42 ); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
String glowneMenu[10] = {"", "Menu", "Ogrzewanie", "Swiatlo", "Rolety", "Brama"};
unsigned long currentTime;
unsigned long debounceTime;
unsigned long debouncePeriod = 500;

byte menu = 0;

#define UP 12
#define DN 11
#define ENT 10
#define LED 13

void setup() {
  Serial.begin(9600);

  pinMode(UP, INPUT_PULLUP);
  pinMode(DN, INPUT_PULLUP);
  pinMode(ENT, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);

  lcd.init();
  lcd.backlight();
  lcd.clear();

  Rtc.Begin();
  RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
}

void loop() {
  currentTime = millis();
  if (digitalRead(UP) == LOW) {
    if (currentTime - debounceTime >= debouncePeriod ) {
      menu++;
      lcd.clear();
      debounceTime = currentTime;
    }
  }
  if (digitalRead(DN) == LOW) {
    if (currentTime - debounceTime >= debouncePeriod ) {
      menu--;
      lcd.clear();
      debounceTime = currentTime;
    }
  }
  switch (menu) {
    case 1:
      lcd.setCursor(0, 0);
      lcd.print(glowneMenu[menu]);
      break;
    case 2:
      lcd.setCursor(0, 0);
      lcd.print(glowneMenu[menu]);
      break;
    case 0:
      RtcDateTime now = Rtc.GetDateTime();
      printDateTime(now + 2);
      break;
    case 3:
      lcd.setCursor(0, 0);
      lcd.print(glowneMenu[menu]);
      break;
  }
}

void printDateTime(const RtcDateTime & dt) {
#define countof(a) (sizeof(a) / sizeof(a[0]))
  char datestring1[11];
  char datestring2[11];
  snprintf_P(datestring1,
             countof(datestring1),
             PSTR("%02u-%02u-%04u"),
             dt.Day(),
             dt.Month(),
             dt.Year() );
  snprintf_P(datestring2,
             countof(datestring2),
             PSTR("%02u:%02u:%02u"),
             dt.Hour(),
             dt.Minute(),
             dt.Second() );
  lcd.setCursor(0, 0);
  lcd.print(datestring1);
  lcd.setCursor(0, 1);
  lcd.print(datestring2);
}

Can you post the broken code?

Do you mean printDateTime function? It is at the end of the code.

what is "PSTR()"?

No, the version you refer to here:

If the case 0 is in the first position in my code i.e 0,1,2....n then my LCD is not displaying anything.

If you declare a variable within a case, enclose the code for that case in { } brackets. The compiler gets a bit confused and thinks the variable is out of scope for any subsequent cases.

gcjr:
what is "PSTR()"?

Similar to the F() macro, used to store a text literal in PROGMEM.

david_2018:
If you declare a variable within a case, enclose the code for that case in { } brackets. The compiler gets a bit confused and thinks the variable is out of scope for any subsequent cases.

So I have shifted

  RtcDateTime now = Rtc.GetDateTime();

To void loop() and everything seems to be working. How I didn't notice it earlier? :confused:
Thanks

piggybank:
So I have shifted

  RtcDateTime now = Rtc.GetDateTime();

To void loop() and everything seems to be working. How I didn't notice it earlier? :confused:
Thanks

Not an obvious problem, the compiler will generate warning messages, but the IDE will not display those unless set to show all compiler warnings.

case 0:
      printDateTime(Rtc.GetDateTime() + 2);
      break;