#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const byte length_vertical = 16;
const byte length_horizont = 2;
char symbols[16][2];
LiquidCrystal_I2C lcd(0x27, length_horizont, length_vertical);
const int up = 512;
const int down = 512;
const int left = 512;
const int right = 512;
const byte pinX = A0;
const byte pinY = A1;
const byte pinB = 2;
int cursor_pos_x = 0;
int cursor_pos_y = 0;
bool buttonDown = false;
unsigned long time = 0;
unsigned long time_joystick = 0;
const short drebezg = 200;
void button()
{
if(cursor_pos_y == 0)
{
cursor_pos_y++;
}
else
{
cursor_pos_y--;
}
lcd.setCursor(cursor_pos_x, cursor_pos_y);
buttonDown = true;
}
//vert
void switching(bool up = true)
{
if (up)
{
if (symbols[cursor_pos_x][cursor_pos_y] < 90)
{
if(symbols[cursor_pos_x][cursor_pos_y] >= 65)
{
symbols[cursor_pos_x][cursor_pos_y]++;
}
else
{
symbols[cursor_pos_x][cursor_pos_y] = 65;
}
}
}
else if(symbols[cursor_pos_x][cursor_pos_y] < 65)
{
symbols[cursor_pos_x][cursor_pos_y] = 65;
}
else if (symbols[cursor_pos_x][cursor_pos_y] > 65)
{
symbols[cursor_pos_x][cursor_pos_y]--;
}
lcd.print(symbols[cursor_pos_x][cursor_pos_y]);
lcd.setCursor(cursor_pos_x, cursor_pos_y);
}
//hor
void cursor(bool right = true)
{
if (right)
{
if (cursor_pos_x < 15)
{
cursor_pos_x++;
}
}
else
{
if (cursor_pos_x > 0)
{
cursor_pos_x--;
}
}
lcd.setCursor(cursor_pos_x, cursor_pos_y);
}
void setup() {
Wire.begin(); // Инициализация I2C
lcd.init(); // Инициализация дисплея
lcd.backlight(); // Включаем подсветку
lcd.setCursor(0, 0);
lcd.blink();
pinMode(pinB, INPUT_PULLUP);
pinMode(pinX, INPUT);
pinMode(pinY, INPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(pinB) == false && buttonDown == false && time + drebezg < millis())
{
button();
time = millis();
}
else if (analogRead(pinX) > left && time_joystick + drebezg < millis())
{
cursor(false);
time_joystick = millis();
}
else if (analogRead(pinX) < right && time_joystick + drebezg < millis())
{
cursor();
time_joystick = millis();
}
else if (analogRead(pinY) > up && time_joystick + drebezg < millis())
{
switching();
time_joystick = millis();
}
else if (analogRead(pinY) < down && time_joystick + drebezg < millis())
{
switching(false);
time_joystick = millis();
}
if (digitalRead(pinB) == true && buttonDown == true && time + drebezg < millis())
{
buttonDown = false;
time = millis();
}
}