#include <LiquidCrystal_I2C.h>
#define incButton 2
#define onButton 3
#define offButton 4
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool ledky[4] = {0, 0, 0, 0};
String menu[4] = {"LED 1", "LED 2", "LED 3", "LED 4"};
byte poloha = 0;
bool zmena = true;
void setup() {
pinMode(incButton, INPUT_PULLUP);
pinMode(onButton, INPUT_PULLUP);
pinMode(offButton, INPUT_PULLUP);
DDRB |= 0b00001111; // Set pins 8, 9, 10, 11 as outputs
PORTB &= 0b11110000; // Turn off LEDs initially
lcd.init();
lcd.backlight();
}
void loop() {
if (zmena) {
updateLEDs();
updateLCD();
zmena = false;
}
checkButtons();
}
void updateLEDs() {
for (byte ledka = 0; ledka < 4; ledka++) {
if (ledky[ledka])
PORTB |= 1 << (3 - ledka);
else
PORTB &= ~(1 << (3 - ledka));
}
}
void updateLCD() {
lcd.clear();
lcd.print(">");
for (byte i = 0; i < 2; i++) {
lcd.setCursor(1, i);
byte currentIndex = (poloha + i) % 4;
lcd.print(menu[currentIndex]);
lcd.setCursor(11, i);
if (ledky[currentIndex])
lcd.print("ON");
else
lcd.print("OFF");
}
}
void checkButtons() {
static unsigned long lastIncButtonTime = 0;
static unsigned long lastOnButtonTime = 0;
static unsigned long lastOffButtonTime = 0;
static const unsigned long debounceDelay = 50; // debounce čas v milisekundách
if (digitalRead(incButton) == LOW && (millis() - lastIncButtonTime) > debounceDelay) {
zmena = true;
poloha = (poloha < 3) ? poloha + 1 : 0;
lastIncButtonTime = millis();
}
if (digitalRead(onButton) == LOW && (millis() - lastOnButtonTime) > debounceDelay) {
zmena = true;
ledky[poloha] = true; // Zapnutie príslušnej LED
lastOnButtonTime = millis();
}
if (digitalRead(offButton) == LOW && (millis() - lastOffButtonTime) > debounceDelay) {
zmena = true;
ledky[poloha] = false; // Vypnutie príslušnej LED
lastOffButtonTime = millis();
}
}