byte columnPins[] = { 7, 6, 5, 4 };
byte rowPins[] = { 13, 12, 9, 8 };
bool prevStates[16];
unsigned long pressTimes[16];
byte delayTime = 20;
bool buttonsFiltered[16];
byte ids[16] = { 1, 2, 3, 253, 4, 5, 6, 254, 7, 8, 9, 255, 100, 0, 101, 102 };
bool isR = false, isG = false, isB = false, stateNumbering = false;
byte rgbNum[3] = { 0, 0, 0 };
bool isLight = false;
const byte ledRED = 3;
const byte ledGREEN = 10;
const byte ledBLUE = 11;
void setup() {
for (byte i = 0; i < 4; i++) {
pinMode(rowPins[i], OUTPUT);
pinMode(columnPins[i], INPUT_PULLUP);
}
Serial.begin(9600);
}
void loop() {
if (isLight) {
updateLEDs();
}
readButtons();
}
void updateLEDs() {
analogWrite(ledRED, rgbNum[0]);
analogWrite(ledGREEN, rgbNum[1]);
analogWrite(ledBLUE, rgbNum[2]);
}
void readButtons() {
for (byte r = 0; r < 4; r++) {
digitalWrite(rowPins[r], LOW);
for (byte c = 0; c < 4; c++) {
handleButton(r, c);
}
digitalWrite(rowPins[r], HIGH);
}
}
void handleButton(byte r, byte c) {
byte buttonIndex = r * 4 + c;
bool buttonReading = !digitalRead(columnPins[c]);
if (buttonReading != prevStates[buttonIndex]) {
pressTimes[buttonIndex] = millis();
}
if (millis() - pressTimes[buttonIndex] >= delayTime) {
if (buttonsFiltered[buttonIndex] != buttonReading) {
buttonsFiltered[buttonIndex] = buttonReading;
if (buttonsFiltered[buttonIndex]) {
processButtonPress(ids[buttonIndex]);
}
}
}
prevStates[buttonIndex] = buttonReading;
}
void processButtonPress(byte id) {
if (id == 102) {
toggleLight();
} else if (stateNumbering && isLight) {
handleStateNumbering(id);
} else if (isLight) {
handleColorSelection(id);
} else {
Serial.println("Для включения нажмите на 'C'.");
}
}
void toggleLight() {
isLight = !isLight;
if (isLight) {
Serial.println("Светодиод включен");
} else {
turnOffLEDs();
Serial.println("Светодиод выключен");
}
}
void turnOffLEDs() {
analogWrite(ledRED, 0);
analogWrite(ledGREEN, 0);
analogWrite(ledBLUE, 0);
}
void handleStateNumbering(byte id) {
if (id == 101) {
stateNumbering = false;
Serial.print("Изменения сохранены ");
printRGB();
resetColorFlags();
} else if (id >= 0 && id <= 10) {
setNumber(id);
} else {
Serial.println("Введите значения");
}
}
void resetColorFlags() {
isG = false;
isR = false;
isB = false;
}
void handleColorSelection(byte id) {
if (id >= 253 && id <= 255) {
if (id == 253) {
isR = true; rgbNum[0] = 0; Serial.println("Введите значение для красного цвета.");
} else if (id == 254) {
isG = true; rgbNum[1] = 0; Serial.println("Введите значение для зелёного цвета.");
} else if (id == 255) {
isB = true; rgbNum[2] = 0; Serial.println("Введите значение для синего цвета.");
}
Serial.print("Нажмите # для сохранения. Сейчас: ");
printRGB();
stateNumbering = true;
} else {
Serial.println("Для настройки нажмите на R, G или B");
}
}
void setNumber(byte a) {
if (isR && rgbNum[0] * 10 + a <= 255) {
rgbNum[0] = rgbNum[0] * 10 + a;
} else if (isG && rgbNum[1] * 10 + a <= 255) {
rgbNum[1] = rgbNum[1] * 10 + a;
} else if (isB && rgbNum[2] * 10 + a <= 255) {
rgbNum[2] = rgbNum[2] * 10 + a;
} else {
Serial.println("Введите значения от 0 до 255");
}
printRGB();
}
void printRGB() {
if (stateNumbering) {
Serial.print("Вводимые значения ");
}
Serial.print("R=");
Serial.print(rgbNum[0]);
Serial.print(" G=");
Serial.print(rgbNum[1]);
Serial.print(" B=");
Serial.println(rgbNum[2]);
}