#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x27, 16, 2);
LiquidCrystal_I2C lcd2(0x26, 16, 2);
const int BTN_PIN = 2;
unsigned long clickCount = 0;
bool swapped = false;
// debounce
bool lastRaw = HIGH;
bool stable = HIGH;
unsigned long lastDebounce = 0;
const unsigned long DEBOUNCE_MS = 30;
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 wasPressed() {
bool raw = digitalRead(BTN_PIN);
if (raw != lastRaw) {
lastRaw = raw;
lastDebounce = millis();
}
if (millis() - lastDebounce > DEBOUNCE_MS) {
if (raw != stable) {
stable = raw;
if (stable == LOW) return true;
}
}
return false;
}
void drawInfo(LiquidCrystal_I2C &lcd) {
printLine(lcd, 0, "Ekran INFO");
printLine(lcd, 1, "Kliknij przycisk");
}
void drawCounter(LiquidCrystal_I2C &lcd) {
printLine(lcd, 0, "Ekran LICZNIK");
printLine(lcd, 1, "Klikniec: " + String(clickCount));
}
void drawDisplays() {
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);
drawDisplays();
}
void loop() {
if (wasPressed()) {
clickCount++;
swapped = !swapped;
drawDisplays();
}
}