// Rotary Encoder Inputs
#define CLK 2
#define DT 4
#define SW 3
int currentStateCLK;
int lastStateCLK;
unsigned long lastButtonPress = 0;
int leds [] = {27, 28};
unsigned int menu = 0;
int ledStatus[27] = {0};
int timer = 500;
void setup() {
Serial1.begin(9600);
// Set encoder pins as input
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
for (int i = 0; i < 2; i++) {
pinMode(leds[i], OUTPUT);
}
menu = 0;
updateMenu();
}
void loop() {
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
if (digitalRead(DT) != currentStateCLK) {
menu --;
updateMenu();
} else {
menu ++;
updateMenu();
}
}
lastStateCLK = currentStateCLK;
int btnState = digitalRead(SW);
if (btnState == LOW) {
if (millis() - lastButtonPress > 50) {
executeAction();
updateMenu();
}
lastButtonPress = millis();
}
delay(1);
}
void printlcd(){
Serial1.println("LED Controller");
}
void updateMenu() {
switch (menu) {
case 0:
menu = 1;
break;
case 1:
printlcd();
Serial1.println("<LED 1>");
break;
case 2:
printlcd();
Serial1.println("<LED 2>");
break;
case 3:
menu = 4;
break;
}
}
void executeAction() {
switch (menu) {
case 1:
action1();
break;
case 2:
action2();
break;
}
}
void action1() {
if (ledStatus[menu] != 0) {
ledStatus[menu] = 0;
Serial1.println("> LED 1 OFF...");
digitalWrite(leds[menu - 1], ledStatus[menu]);
delay(timer);
} else {
ledStatus[menu] = 1;
// display.clearDisplay();
// display.println("> LED 1 ON...");
Serial1.println("> LED 1 ON...");
digitalWrite(leds[menu - 1], ledStatus[menu]);
delay(timer);
}
}
void action2() {
if (ledStatus[menu] != 0) {
ledStatus[menu] = 0;
Serial1.println("> LED 2 OFF...");
digitalWrite(leds[menu - 1], ledStatus[menu]);
delay(timer);
} else {
ledStatus[menu] = 1;
Serial1.println("> LED 2 ON...");
digitalWrite(leds[menu - 1], ledStatus[menu]);
delay(timer);
}
}