#include <Wire.h> // I2C通信用ライブラリ
#include <LiquidCrystal_I2C.h> // I2C接続LCDライブラリ
// ロータリーエンコーダのピン番号
const int encoderPinA = 2;
const int encoderPinB = 3;
const int encoderPinSw = 4;
// LCDモジュールのアドレスと行数、列数
const int lcdAddr = 0x27;
const int lcdCols = 16;
const int lcdRows = 2;
// LCDオブジェクトの生成
LiquidCrystal_I2C lcd(lcdAddr, lcdCols, lcdRows);
// グローバル変数
volatile int encoderPos = 0; // ロータリーエンコーダの位置
char selectedChar = '0'; // 選択中の文字
int char_count=0; //文字変更++
int newPos; //ロータリーポジション
boolean turnA=HIGH;
boolean turnB=HIGH;
int char_shift=0; //桁移動++
boolean previousClick=HIGH; //センタークリック現在値
boolean currentClick=HIGH; //センタークリック直前値
void onEncoderInterrupt(){ // ロータリーエンコーダの位置を監視する
turnA=digitalRead(encoderPinA);
turnB=digitalRead(encoderPinB);
if(turnA==HIGH&&turnB==LOW){
newPos=encoderPos+1;
}else if(turnA==LOW&&turnB==HIGH){
newPos=encoderPos-1;
}
char_count=newPos;
if (newPos != 0) {
// 位置が変化した場合、選択中の文字を更新する
selectedChar += newPos;
if (selectedChar < 'A') {
selectedChar = 'Z';
} else if (selectedChar > 'Z') {
selectedChar = 'A';
}
// 選択中の文字を表示する
lcd.setCursor(1, 0);
lcd.print(" ");
}
}
void get_Click(){ //センタークリックで桁移動
// 選択中の文字をLCDに表示する
lcd.setCursor(char_shift, 0);
lcd.print(selectedChar);
currentClick=digitalRead(encoderPinSw);
// ロータリーエンコーダのクリックを監視する
if (currentClick == LOW && previousClick==HIGH) {
// センタークリックされた場合、LCDのカーソルを次の桁に移動する
// char_count++;
char_shift++;
// lcd.setCursor(char_count,0);
// lcd.print(char_count,selectedChar);
lcd.setCursor(char_shift,0);
lcd.print(char_shift,selectedChar);
}
// 選択中の文字をリセットする
selectedChar = '0';
// 一定時間待機する
//delay(50);
}
void setup() {
// シリアル通信の開始
Serial.begin(9600);
// ピンのモード設定
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
pinMode(encoderPinSw, INPUT_PULLUP);
// ロータリーエンコーダの割り込み処理の設定 どちらに回してもonEncoderInterruptに出力
attachInterrupt(digitalPinToInterrupt(encoderPinA), onEncoderInterrupt, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderPinB), onEncoderInterrupt, CHANGE);
// LCDモジュールの初期化
lcd.init();
lcd.backlight();
}
void loop() {
onEncoderInterrupt();
get_Click();
previousClick=currentClick;
Serial.print(" char_shift:");
Serial.print(char_shift);
Serial.print(" char_count:");
Serial.print(char_count);
Serial.println("");
}
/*
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int pinA = 2; //CLK
int pinB = 3; //DT
int encoderPosCount = 0;
int pinALast;
int aVal;
void setup() {
pinMode (pinA,INPUT);
pinMode (pinB,INPUT);
pinALast = digitalRead(pinA);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("KY-040 Test");
lcd.setCursor(0,1);
lcd.print("Volume:");
lcd.setCursor(8,1);
lcd.print(encoderPosCount);
}
void loop() {
aVal = digitalRead(pinA);
if (aVal != pinALast){
// pinBは、回転方向を決定する
if (digitalRead(pinB) != aVal) {
//pinAと値が異なる場合は、時計回り
encoderPosCount ++;
if (encoderPosCount > 20) encoderPosCount = 20;
} else {
encoderPosCount --;
if (encoderPosCount < 0) encoderPosCount = 0;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("KY-040 Test");
lcd.setCursor(0,1);
lcd.print("Volume:");
lcd.setCursor(8,1);
lcd.print(encoderPosCount);
}
pinALast = aVal;
}
*/
/*
int val;
int encoder0PinA = 2;
int encoder0PinB = 3;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
void setup() {
pinMode (encoder0PinA, INPUT);
pinMode (encoder0PinB, INPUT);
Serial.begin (9600);
}
void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--;
delay(abs(encoder0Pos));
} else {
encoder0Pos++;
delay(abs(encoder0Pos));
}
Serial.print (encoder0Pos);
Serial.print ("/");
}
encoder0PinALast = n;
}
*/