// Keyboard Matrix Tutorial Example
// baldengineer.com
// CC BY-SA 4.0
#include <LiquidCrystal_I2C.h> // Подключение библиотеки
LiquidCrystal_I2C lcd(0x27,16,2); // Указываем I2C адрес (наиболее распространенное значение), а также параметры экрана
// энкодер и прерывания
#include <Arduino.h>
#include <EncButton.h>
EncButton eb(2, 3, 4);
int var1 = 0;
int var2 = 0;
int var3 = 0;
uint8_t select = 1; // выбранная переменная
long lasttime ;
//установка пинов-строк
// JP1 is an input
byte rows[] = {7,8};
const int rowCount = sizeof(rows)/sizeof(rows[0]);
//установка пинов-столбцов
// JP2 and JP3 are outputs
byte cols[] = {9,10,11,12};
const int colCount = sizeof(cols)/sizeof(cols[0]);
byte keys[colCount][rowCount];
void setup() {
Serial.begin(115200);
attachInterrupt(0, isr, CHANGE);
attachInterrupt(1, isr, CHANGE);
eb.setEncISR(true);
lcd.init(); // Инициализация дисплея
lcd.backlight(); // Подключение подсветки
attachInterrupt(0, isr, CHANGE); // прерывание на 2 пине! CLK у энкодера
for(int x=0; x<rowCount; x++) {
Serial.print(rows[x]); Serial.println(" as input");
pinMode(rows[x], INPUT);
}
for (int x=0; x<colCount; x++) {
Serial.print(cols[x]); Serial.println(" as input-pullup");
pinMode(cols[x], INPUT_PULLUP);
}
}
void isr() {
eb.tickISR();
}
void readMatrix() {
// iterate the columns
for (int colIndex=0; colIndex < colCount; colIndex++) {
// col: set to output to low
byte curCol = cols[colIndex];
pinMode(curCol, OUTPUT);
digitalWrite(curCol, LOW);
// row: interate through the rows
for (int rowIndex=0; rowIndex < rowCount; rowIndex++) {
byte rowCol = rows[rowIndex];
pinMode(rowCol, INPUT_PULLUP);
keys[colIndex][rowIndex] = !digitalRead(rowCol);
pinMode(rowCol, INPUT);
}
// disable the column
pinMode(curCol, INPUT);
}
}
void printMatrix() {
//lcd.clear();
for (int rowIndex=0; rowIndex < rowCount; rowIndex++) {
// if (rowIndex < 10)
// Serial.print(F("0"));
//Serial.print(rowIndex); Serial.print(F(": "));
for (int colIndex=0; colIndex < colCount; colIndex++) {
//Serial.print(keys[colIndex][rowIndex]);
lcd.print(keys[colIndex][rowIndex]);
lcd.setCursor((colIndex+1)*2,rowIndex);
/*if (colIndex < colCount)
Serial.print(F(", "));
*/
}
//Serial.println("");
lcd.setCursor(0,1);;
}
//Serial.println("");
lcd.setCursor(0,0);
}
void loop() {
eb.tick();
if (eb.turn()) {
Serial.print("направление: шаг ");
Serial.print(eb.dir());
Serial.print(", быстро ");
Serial.print(eb.fast());
Serial.print(", кнопка ");
Serial.print(eb.pressing());
Serial.print(", значение ");
Serial.println(eb.counter);
}
lasttime = millis();
readMatrix();
printMatrix();
//lcd.setCursor(0,0); // Установка курсора в начало первой строки
// lcd.print(); // Набор текста на первой строке
}