#include <LiquidCrystal_I2C.
h>
int led1 = 10;
int led2 = 11;
int led3 = 12;
int btn1 = 7;
int a = 0;
int b = 0;
int cursor = 1;
int btn2 = 8;
int c = 0;
int d = 0;
bool onOff = true;
int buttonState1;
int buttonState2;
int count = 0;
int currentLed = 1;
int modes[] = {0, 0, 0}; // MODES NG BAWAT LED NAKA ARRAY
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long previousMillis[3] = {0, 0, 0};
const long interval = 1000; // DELAYS FUNCTIONS
const long shortInterval = 200;
void setup() { //MGA INPUTS AT OUTPUTS
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
lcd.init();
lcd.backlight();
Serial.begin(9600);
_updatePrint();
}
void _updatePrint() {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("LEDs");
lcd.setCursor(8, 0);
lcd.print("MODEs");
if (cursor == 1) {
lcd.setCursor(0, 1);
lcd.print(">");
} else {
lcd.setCursor(7, 1);
lcd.print(">");
}
lcd.setCursor(1, 1);
lcd.print(currentLed);
lcd.setCursor(8, 1);
int currentMode = modes[currentLed - 1];
if (currentMode == 0) {
lcd.print("OFF");
} else if (currentMode == 1) {
lcd.print("A");
} else if (currentMode == 2) {
lcd.print("B");
} else if (currentMode == 3) {
lcd.print("C");
}
}
void loop() {
lcd.setCursor(1, 0);
lcd.print("LEDs");
lcd.setCursor(8, 0); //TITLE's
lcd.print("MODEs");
buttonState1 = digitalRead(btn1); //BUTTON 1
if (buttonState1 == 1) {
Serial.print("x");
delay(300);
if (cursor == 1) {
currentLed++;
} else {
modes[currentLed - 1]++;
if (modes[currentLed - 1] > 3) {
modes[currentLed - 1] = 0;
}
}
if (currentLed > 3) {
currentLed = 1;
}
_updatePrint();
}
if (currentLed == 1) {
updateLed(led1, modes[0]);
Serial.print("1");
} else if (currentLed == 2) {
updateLed(led2, modes[1]); //MGA LEDs
Serial.print("2");
} else if (currentLed == 3) {
updateLed(led3, modes[2]);
Serial.print("3");
}
buttonState2 = digitalRead(btn2); // BUTTON 2
if (buttonState2 == 1) {
Serial.print("x");
delay(300);
cursor++;
if (cursor > 2) {
cursor = 1;
}
_updatePrint();
}
}
void updateLed(int selectedLed, int selectedMode) { // MGA MODES
unsigned long currentMillis = millis();
switch (selectedMode) {
case 0:
digitalWrite(selectedLed, LOW);
break;
case 1:
if (currentMillis - previousMillis[selectedLed - 10] >= interval) {
previousMillis[selectedLed - 10] = currentMillis;
onOff = !onOff;
digitalWrite(selectedLed, onOff ? HIGH : LOW);
}
break;
case 2:
if (currentMillis - previousMillis[selectedLed - 10] >= shortInterval) {
previousMillis[selectedLed - 10] = currentMillis;
onOff = !onOff;
digitalWrite(selectedLed, onOff ? HIGH : LOW);
}
break;
case 3:
digitalWrite(selectedLed, HIGH);
break;
}
}