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