#include <LiquidCrystal.h> // Подключаем стандартную библиотеку LiquidCrystal
#include <Bounce2.h>;
// Инициализируем объект-экран, передаём использованные
// для подключения контакты на Arduino в порядке:
// RS, E, D4, D5, D6, D7
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const int pinX = A0; // ось X джойстика
const int pinY = A1; // ось Y джойстика
const int pinButton = 2;
int lastButton = LOW; //предыдущее состояние кнопки
int curButton = LOW; //Текущее состояние кнопки
const int indexXMax = 15;
const int indexXMin = 0;
int indexX = 0;
const int rowMax = 1;
const int rowMin = 0;
int row = -1;
const char numberLetterMax = 'Z';
const char numberLetterMin = 'A';
char numberLetter = 'A';
// Создаем объект
Bounce debouncer = Bounce();
void changeIndexX(const int& x)
{
if (x < 8)
{
++indexX;
if (indexX > indexXMax)
{
indexX = indexXMax;
}
}
else if (x > 8)
{
--indexX;
if (indexX < indexXMin)
{
indexX = indexXMin;
}
}
}
void changeNumberLetter(const int& y)
{
if (y > 127)
{
++numberLetter;
if (numberLetter > numberLetterMax)
{
numberLetter = numberLetterMin;
}
}
else if (y < 127)
{
--numberLetter;
if (numberLetter < numberLetterMin)
{
numberLetter = numberLetterMax;
}
}
}
void setup()
{
Serial.begin(9600);
pinMode(pinButton, INPUT_PULLUP);
// Даем бибилотеке знать, к какому пину мы подключили кнопку
debouncer.attach(pinButton);
debouncer.interval(5); // Интервал, в течение которого мы не буем получать значения с пина
// устанавливаем размер (количество столбцов и строк) экрана
lcd.begin(16, 2);
// ощищаем экран
lcd.clear();
pinMode(pinX, INPUT);
pinMode(pinY, INPUT);
}
void loop()
{
int x = analogRead(pinX);
int y = analogRead(pinY);
x = map(x, 0, 1023, 0, 16);
y = map(y, 0, 1023, 0, 255);
changeIndexX(x);
changeNumberLetter(y);
// Даем объекту бибилотеки знать, что надо обновить состояние - мы вошли в новый цкил loop
debouncer.update();
// Получаем значение кнопки
int curButton = debouncer.read();
if (curButton == LOW && lastButton == HIGH) //условие нажатия
{
++row;
if (row > 2)
{
row = rowMin;
}
}
lastButton = curButton;
lcd.setCursor(indexX, row);
lcd.print(numberLetter);
delay(100);
}