Skip to content

Instantly share code, notes, and snippets.

@bashkirtsevich
Last active July 27, 2019 17:52
Show Gist options
  • Save bashkirtsevich/b7b61ac9093a510f4fa60f75533a1cbd to your computer and use it in GitHub Desktop.
Save bashkirtsevich/b7b61ac9093a510f4fa60f75533a1cbd to your computer and use it in GitHub Desktop.
Arduino LCD Keypad Shield test
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define BTN_UP 1
#define BTN_DOWN 2
#define BTN_LEFT 3
#define BTN_RIGHT 4
#define BTN_SELECT 5
#define BTN_NONE 10
String str;
byte customChar[8] = {
B00000,
B00000,
B01010,
B10101,
B10001,
B01010,
B00100,
B00000
};
int detectButton() {
int keyAnalog = analogRead(A0);
if (keyAnalog < 100) {
// Значение меньше 100 – нажата кнопка right
return BTN_RIGHT;
} else if (keyAnalog < 200) {
// Значение больше 100 (иначе мы бы вошли в предыдущий блок результата сравнения, но меньше 200 – нажата кнопка UP
return BTN_UP;
} else if (keyAnalog < 400) {
// Значение больше 200, но меньше 400 – нажата кнопка DOWN
return BTN_DOWN;
} else if (keyAnalog < 600) {
// Значение больше 400, но меньше 600 – нажата кнопка LEFT
return BTN_LEFT;
} else if (keyAnalog < 800) {
// Значение больше 600, но меньше 800 – нажата кнопка SELECT
return BTN_SELECT;
} else {
// Все остальные значения (до 1023) будут означать, что нажатий не было
return BTN_NONE;
}
}
void clearLine(int line){
lcd.setCursor(0, line);
lcd.print(" ");
}
void printDisplay(String message){
clearLine(1);
lcd.setCursor(0, 1);
lcd.print(message);
delay(1000);
}
void setup() {
lcd.createChar(0, customChar);
lcd.begin(16, 2);
lcd.print("Look at me! ^_^");
lcd.setCursor(0, 0);
lcd.print("Look at me! ^_^");
Serial.begin(9600);
}
void loop() {
int button = detectButton();
switch (button) {
case BTN_UP:
printDisplay("UP");
break;
case BTN_DOWN:
printDisplay("DOWN");
break;
case BTN_LEFT:
printDisplay("LEFT");
break;
case BTN_RIGHT:
printDisplay("RIGHT");
break;
case BTN_SELECT:
printDisplay("SELECT");
break;
default:
// when characters arrive over the serial port...
if (Serial.available())
{
clearLine(1);
lcd.setCursor(11, 1);
str = Serial.readString();
Serial.println(str);
printDisplay(str);
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment