Skip to content

Instantly share code, notes, and snippets.

@ktknest
Created July 30, 2015 10:42
Show Gist options
  • Save ktknest/5982afa7e22fdf5e2be8 to your computer and use it in GitHub Desktop.
Save ktknest/5982afa7e22fdf5e2be8 to your computer and use it in GitHub Desktop.
LCD×タクトスイッチ文字入力
// LCDキャラクタディスプレイ関数のインクルード
#include <LiquidCrystal.h>
// 接続メモ
// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d0, d1, d2, d3 on pins 5, 4, 3, 2
// 使用するピンの定義
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int UP_KEY = 8;
int RIGHT_KEY = 9;
int BOTTOM_KEY = 7;
int LEFT_KEY = 6;
int pos = 0;
int c = 0x41;
int cnt = 0;
boolean isPushed = false;
void setup()
{
lcd.begin(2, 16);
lcd.clear();
lcd.print("Type Message");
delay(500);
}
void loop()
{
lcd.setCursor(pos, 1);
if (cnt < 5 || isPushedSelect) {
lcd.write(c);
} else {
lcd.write(0xFF);
}
cnt++;
cnt = cnt % 10;
if (isPushed == false) {
if (digitalRead(UP_KEY) == HIGH) {
charStep();
cnt = 0;
} else if (digitalRead(BOTTOM_KEY) == HIGH) {
charBackStep();
cnt = 0;
} else if (digitalRead(RIGHT_KEY) == HIGH) {
lcd.setCursor(pos, 1);
lcd.write(c);
pos++;
pos = pos % 16;
c = 0x20;
cnt = 0;
} else if (digitalRead(LEFT_KEY) == HIGH) {
lcd.setCursor(pos, 1);
lcd.write(c);
pos += 15;
pos = pos % 16;
c = 0x20;
cnt = 0;
}
}
isPushed = (digitalRead(UP_KEY) == HIGH) ||
(digitalRead(BOTTOM_KEY) == HIGH) ||
(digitalRead(RIGHT_KEY) == HIGH) ||
(digitalRead(LEFT_KEY) == HIGH);
delay(100);
}
void charStep()
{
if (c == 0x5A) {
c = 0x20;
} else if (c == 0x20) {
c = 0x41;
} else {
c++;
}
}
void charBackStep()
{
if (c == 0x41) {
c = 0x20;
} else if (c == 0x20) {
c = 0x5A;
} else {
c--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment