#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x27, 16, 2);
LiquidCrystal_I2C lcd2(0x26, 16, 2);
// ====== Przycisk ======
const uint8_t BTN_PIN = 2;
// ====== Odświeżanie LCD bez delay ======
const unsigned long LCD_REFRESH_MS = 200;
unsigned long lastRefreshMs = 0;
// ====== Debounce + pojedyncze naciśnięcie ======
const unsigned long DEBOUNCE_MS = 20;
bool lastRaw = HIGH;
bool stable = HIGH;
unsigned long lastDebounceMs = 0;
unsigned long clickCount = 0;
bool swapped = false; // false: lcd1=INFO, lcd2=LICZNIK; true: odwrotnie
bool uiDirty = true;
unsigned long uptimeSeconds() {
return millis() / 1000UL;
}
void printLine(LiquidCrystal_I2C &lcd, uint8_t row, const String &text) {
lcd.setCursor(0, row);
String t = text;
if (t.length() > 16) t = t.substring(0, 16);
lcd.print(t);
for (int i = t.length(); i < 16; i++) lcd.print(' ');
}
bool buttonPressedEvent() {
bool raw = digitalRead(BTN_PIN);
if (raw != lastRaw) {
lastRaw = raw;
lastDebounceMs = millis();
}
if (millis() - lastDebounceMs > DEBOUNCE_MS) {
if (raw != stable) {
stable = raw;
if (stable == LOW) return true; // INPUT_PULLUP: wciśnięty = LOW
}
}
return false;
}
void drawInfo(LiquidCrystal_I2C &lcd) {
printLine(lcd, 0, "Ekran INFO");
printLine(lcd, 1, "Kliknij przycisk");
}
void drawCounter(LiquidCrystal_I2C &lcd) {
unsigned long up = uptimeSeconds();
printLine(lcd, 0, "Ekran LICZNIK");
printLine(lcd, 1, "K=" + String(clickCount) + " Up=" + String(up) + "s");
}
void drawUI() {
if (!swapped) {
drawInfo(lcd1);
drawCounter(lcd2);
} else {
drawCounter(lcd1);
drawInfo(lcd2);
}
}
void setup() {
Wire.begin();
lcd1.init(); lcd1.backlight();
lcd2.init(); lcd2.backlight();
pinMode(BTN_PIN, INPUT_PULLUP);
drawUI();
}
void loop() {
if (buttonPressedEvent()) {
clickCount++;
swapped = !swapped;
uiDirty = true;
}
unsigned long now = millis();
if (uiDirty || (now - lastRefreshMs >= LCD_REFRESH_MS)) {
lastRefreshMs = now;
uiDirty = false;
drawUI();
}
}