#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int ENCA_PIN PROGMEM = 10;
const int ENCB_PIN PROGMEM = 9;
const int BTN_PIN PROGMEM = 8;
#define BUTTON 7
boolean currentButton = HIGH;
boolean previousButton = HIGH;
byte pressCount = 0;
int selectCode = 0;
const int MODV PROGMEM = 96;
int encPos = 0;
int lastPinA = HIGH;
int x = 0;
int y = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C: 0x27, 16x2 LCD
void setup() {
Serial.begin(9600);
pinMode(ENCA_PIN, INPUT);
pinMode(ENCB_PIN, INPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(BUTTON, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.blink();
}
void loop() {
/*
//BUTTONで英字/数字/記号の切替え 0=0x30 A=0x41 a=0x61
currentButton = digitalRead(BUTTON);
if(currentButton == LOW && previousButton == HIGH){
pressCount++;
}
previousButton=currentButton;
if(pressCount>3){
pressCount=0;
}
switch (pressCount){
case 0:
selectCode = 0x30;
break;
case 1:
selectCode = 0x41;
break;
case 2:
selectCode = 0x61;
break;
case 3:
selectCode = 0x7F;
break;
}
*/
int v = digitalRead(ENCA_PIN);
char c; //初期表示はスペース
//エンコーダ回転
if ((lastPinA == LOW) && (v == HIGH)) {
if (digitalRead(ENCB_PIN) == LOW)
encPos++;
else
encPos--;
encPos = (encPos + MODV) % MODV; // encPos MOD 96
c = encPos + 0x20; //ASCIIスペースから回転分進んだ文字
lcd.print(c);
lcd.setCursor(x, y);
}
c = encPos + 0x20;
//センタークリック
if (digitalRead(BTN_PIN) == LOW) {
if (c == 0x7F) { //ASCIIコード 削除 127
lcd.setCursor(x, y);
lcd.print(" ");
if ((y == 1) || (x > 0))
x--;
if (x < 0) {
x = 15;
y--;
}
} else {
if ((y == 0) || (x < 15))
x++;
if (x > 15) {
x = 0;
y++;
}
}
lcd.setCursor(x, y);
lcd.print(c);
lcd.setCursor(x, y);
delay(500);
}
lastPinA = v;
}