-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridKeyboard.ino
90 lines (77 loc) · 2.13 KB
/
GridKeyboard.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <LiquidCrystal_I2C.h>
const int LCD_I2C_ADDRESS = 0x27;
const int LCD_COLUMNS = 16;
const int LCD_ROWS = 2;
LiquidCrystal_I2C lcd(LCD_I2C_ADDRESS, LCD_COLUMNS, LCD_ROWS);
// Button pins
const short upButtonPin = 6;
const short downButtonPin = 5;
const short leftButtonPin = 4;
const short rightButtonPin = 3;
const short confirmButtonPin = 2;
// Alphabet configuration
const short alphabetLength = 7;
const short alphabetHeight = 4;
const char alphabetAndSymbols[alphabetHeight][alphabetLength] = {
{'a', 'b', 'c', 'd', 'e', 'f', 'g'},
{'h', 'i', 'j', 'k', 'l', 'm', 'n'},
{'o', 'p', 'q', 'r', 's', 't', 'u'},
{'v', 'w', 'x', 'y', 'z', ' ', ','}
};
// State variables
String lcdText = "";
int stringSize = 0;
short alphabetPositionX = 0;
short alphabetPositionY = 0;
void setup() {
lcd.begin();
lcd.backlight();
pinMode(upButtonPin, INPUT_PULLUP);
pinMode(downButtonPin, INPUT_PULLUP);
pinMode(leftButtonPin, INPUT_PULLUP);
pinMode(rightButtonPin, INPUT_PULLUP);
pinMode(confirmButtonPin, INPUT_PULLUP);
}
void loop() {
handleButtonInput();
updateLCDText();
printPosition();
delay(250);
}
void handleButtonInput() {
if (!digitalRead(upButtonPin)) {
alphabetPositionY = max(alphabetPositionY - 1, 0);
} else if (!digitalRead(downButtonPin)) {
alphabetPositionY = min(alphabetPositionY + 1, alphabetHeight - 1);
}
if (!digitalRead(leftButtonPin)) {
alphabetPositionX = max(alphabetPositionX - 1, 0);
} else if (!digitalRead(rightButtonPin)) {
alphabetPositionX = min(alphabetPositionX + 1, alphabetLength - 1);
}
if (!digitalRead(confirmButtonPin)) {
addNewCharacter();
handleLCDTextOverflow();
}
}
void addNewCharacter() {
char selectedChar = alphabetAndSymbols[alphabetPositionY][alphabetPositionX];
lcdText.concat(selectedChar);
}
void handleLCDTextOverflow() {
if (lcdText.length() > LCD_COLUMNS) {
lcd.clear();
lcdText = "";
}
}
void updateLCDText() {
lcd.setCursor(0, 0);
lcd.print(lcdText);
}
void printPosition() {
lcd.setCursor(0, 1);
lcd.print("X: ");
lcd.print(alphabetPositionX + 1);
lcd.print(" Y: ");
lcd.print(alphabetPositionY + 1);
}