#include "LiquidCrystal.h"
#define CURSOR_ON_POS lcd.setCursor(horzCursorPos <= 15 ? horzCursorPos : horzCursorPos - 16, horzCursorPos <= 15 ? 0 : 1);
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
int cells[32] = {0};
int symbols[27] = {32};
int horzCursorPos = 0;
int vert_pin = A1;
int horz_pin = A2;
int butt_pin = 2;
int oldX, oldY;
int lastButton = LOW;
int currButton = LOW;
bool isMovedX = false;
bool isMovedY = false;
int lim = 25;
void setup() {
lcd.begin(16, 2);
pinMode(vert_pin, INPUT);
pinMode(horz_pin, INPUT);
pinMode(butt_pin, INPUT_PULLUP);
for(int i = 1; i < 27; i++)
{
symbols[i] = 65 + i - 1;
}
lcd.cursor();
Welcome();
}
void loop() {
int x = analogRead(horz_pin);
int y = analogRead(vert_pin);
isMovedX = (((x < 512 - lim) || (x > 512 + lim)) && (x != oldX)) ? true : false;
isMovedY = (((y < 512 - lim) || (y > 512 + lim)) && (y != oldY)) ? true : false;
if(isMovedX)
{
if((x < 512 - lim) && (horzCursorPos != 15) && (horzCursorPos != 31))
{
CursorShift("right");
}
else if((x > 512 + lim) && (horzCursorPos != 0) && (horzCursorPos != 16))
{
CursorShift("left");
}
}
if(isMovedY)
{
int currCellIndex = FindIndex(symbols, symbols[cells[horzCursorPos]]);
if(y > 512 + lim)
{
currCellIndex = currCellIndex == 26 ? -1 : currCellIndex;
SymbolChange("up", currCellIndex);
}
if(y < 512 - lim)
{
currCellIndex = currCellIndex == 0 ? 27 : currCellIndex;
SymbolChange("down", currCellIndex);
}
}
PressButton();
oldX = x;
oldY = y;
}
void PressButton()
{
static unsigned long timer;
if(timer + 50 > millis()) return;
currButton = digitalRead(butt_pin);
if(lastButton == HIGH && currButton == LOW)
{
horzCursorPos = horzCursorPos <= 15 ? horzCursorPos + 16 : horzCursorPos - 16;
CURSOR_ON_POS
}
timer = millis();
lastButton = currButton;
}
void SymbolChange(const String dir, int& index)
{
cells[horzCursorPos] = dir == "up" ? index + 1 : index - 1;
lcd.print(static_cast<char>(symbols[cells[horzCursorPos]]));
CURSOR_ON_POS
}
int FindIndex (int arr[], int value)
{
for(int i = 0; i < 27; i++)
{
if(arr[i] == value)
{
return i;
}
}
return 0;
}
void CursorShift(const String dir)
{
if(dir == "right")
{
horzCursorPos += 1;
CURSOR_ON_POS
}
else if(dir == "left")
{
horzCursorPos -= 1;
CURSOR_ON_POS
}
}
void Welcome()
{
byte arrowUp[8] = {
B00100,
B01110,
B10101,
B00100,
B00100,
B00100,
B00100,
};
lcd.createChar(0, arrowUp);
byte arrowDown[8] = {
B00100,
B00100,
B00100,
B00100,
B10101,
B01110,
B00100,
};
lcd.createChar(1, arrowDown);
byte arrowLeft[8] = {
B00000,
B00100,
B01000,
B11111,
B01000,
B00100,
};
lcd.createChar(2, arrowLeft);
byte arrowRight[8] = {
B00000,
B00100,
B00010,
B11111,
B00010,
B00100,
};
lcd.createChar(3, arrowRight);
byte push[8] = {
B00000,
B00000,
B01110,
B01110,
B11111,
B11111,
B11111,
};
lcd.createChar(4, push);
lcd.clear();
lcd.write(byte(0));
lcd.write(byte(1));
lcd.setCursor(3, 0);
lcd.print("Up/Down to");
lcd.setCursor(3, 1);
lcd.print("select a char");
lcd.setCursor(0, 1);
//lcd.blink();
delay(1000);
for(int i = 1; i < 10; i++)
{
if(i < 6)
{
lcd.print(static_cast<char>(symbols[i]));
lcd.setCursor(0, 1);
delay(500);
}else{
lcd.print(static_cast<char>(symbols[10 - i]));
lcd.setCursor(0, 1);
delay(500);
}
}
delay(1000);
lcd.clear();
lcd.write(byte(2));
lcd.write(byte(3));
lcd.setCursor(3, 0);
lcd.print("Left/Right to");
lcd.setCursor(8, 1);
lcd.print("shifting");
delay(1000);
for(int i = 0; i < 13; i++)
{
if(i < 7)
{
lcd.setCursor(i, 1);
delay(300);
}else{
lcd.setCursor(12 - i, 1);
delay(300);
}
}
delay(1000);
lcd.clear();
lcd.setCursor(2, 0);
lcd.write(byte(4));
lcd.setCursor(4, 0);
lcd.print("Press to");
lcd.setCursor(4, 1);
lcd.print("change line");
delay(1000);
for(int i = 0; i < 13; i++)
{
if(i % 2 == 0)
{
lcd.setCursor(0, 1);
delay(300);
}else{
lcd.setCursor(0, 0);
delay(300);
}
}
delay(1000);
lcd.clear();
}