押しボタンの代わりにロータリーエンコーダーを使用

今までの記事。Arduino UNO とRTCモジュールとキャラクター液晶モジュールで時計を作成。現場でPCが無くても時刻変更ができるように進めてきた。

 

 

 

 

 

最後にタクトスイッチで数値変更するのをロータリーエンコーダーで変更できるようにしようと思う。

 

ロータリーエンコーダーとは簡単に言うと回転方向や回転角度を拾うものだ。

昔からあるものとすれば、工業用の装置でモーターで位置決めが必要な時に使う。

 

モーターの軸に等角度で穴の開いた円盤をつけて、その穴を馬蹄形の光電センサーでOFFとONを拾う。角度差をつけて二個のセンサーを付けると、そのOFF/ONの組み合わせで回転方向や回転の角速度がわかる仕組みだ。

 

でも今回使用するのは形状はボリュームみたいなもの。右や左に回すとクリック感があるので、それでそれぞれの方向に何回クリックしたか信号を出すもの。

 

アマゾンで購入すると単価が高いっちゃ高いけど、送料梱包費度と思えば仕方がない。

県外のパーツショップに行く時間と交通費を考えれば仕方が無い。

つまみは二個で1000円とかすごいぼったくりだ。でも秋月電子さんで数十円だが送料が500円かかる。何かのついでじゃないと勿体ない。

 

軸は押し込むとA接点のスイッチになっていて、ばねで戻って指を離せばOFFになる。

ここが気に入った。

 

  動かしてみる

ライブラリはここの物を使用する。

スケッチに書き込むのに一番スッキリしていそうだったから。

 

 


僕の購入したローターリーエンコーダーは手前に三つピンが出ていて、奥側に二つピンが出ている。

 

3ピンの方は左右をArduino UNO のA2番とA3番にそれぞれ接続。真ん中はGNDへ。

2ピンの方は片側は4番に入れた。

 

ロータリーエンコーダーをそのままブレッドボードに差すと、不安定でぐらつき入力が入らない時がある。

そのためはんだ付けでリード線を付けた。

 

つまみは3Dプリンターで作成。秋月電子さんで一個数十円で販売しているが送料が500円かかる。何かのついでに買えばよい。

 

  ライブラリのサンプル例を試す

Arduino IDEからサンプル例の「polling」を書き込んで試してみる。

エンコーダーを左右に回してクリック感を感じると、回転方向がシリアルモニターに表示される。

 

サンプル例を少し触って、液晶モジュールに表示してみる。

  1. #include <LiquidCrystal_I2C.h>
  2. #include <Rotary.h>
  3. LiquidCrystal_I2C lcd(0x27, 16, 4);
  4. Rotary r = Rotary(2, 3);
  5. int xx = 10;
  6. void setup() {
  7.   Serial.begin(115200);
  8.   lcd.init();
  9.   r.begin(true);
  10.   lcd.clear();
  11.   lcd.backlight();
  12.   
  13. }
  14.  
  15. void loop() {
  16.   lcd.setCursor(0, 0);
  17.   lcd.clear();
  18.   lcd.print(xx);
  19.   unsigned char result = r.process();
  20.   if (result == DIR_NONE) {
  21.     // do nothing
  22.   }
  23.   else if (result == DIR_CW) {
  24.     xx++;
  25.   }
  26.   else if (result == DIR_CCW) {
  27.     xx--;
  28.   }
  29.   //lcd.setCursor(0, 0);
  30.   //lcd.print(xx);;
  31.   //Serial.println(xx);
  32. }

まず数字の10をスタート地点にしてエンコーダーを左右に回すと数値が増減する。

ちなみにCWは正転(時計回り)CCWは反転(反時計回り)です。

 

 

 

  スケッチを合体させる

 

  1.  
  2.  
  3. #include <Rotary.h>//エンコーダー
  4. #include <DS3232RTC.h>
  5. #include <Wire.h>
  6. #include <LiquidCrystal_I2C.h>
  7. #if defined(ARDUINO) && ARDUINO >= 100
  8. #define printByte(args) write(args);
  9. #else
  10. #define printByte(args) print(args,BYTE);
  11. #endif
  12. DS3232RTC myRTC;
  13. LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
  14. Rotary rotary = Rotary(2, 3);
  15. char rtcDate[40];//年月日時分秒を収納する文字列
  16.  
  17. int entB = 4; // EnterボタンのピンNo.(エンコーダーの接点を使用
  18. int selB = 0; // SelectボタンのピンNo.
  19. int yy;
  20. int mon;
  21. int dd;
  22. int hh;
  23. int mm;
  24. int xx;
  25.  
  26. void updateClock() {
  27.   setTime(hh, mm, 0, dd, mon, 2000 + yy); //時、分、秒、日、月、年の順番に入力
  28.   myRTC.set(now());
  29.   lcd.clear();
  30. }
  31.  
  32. // 時計の時刻合わせ
  33. void clockSet() {
  34.   int n, nen, tsuki, dayCount;
  35.   lcd.clear();
  36.   lcd.setCursor(0, 0);
  37.   //lcd.print("Clock set mode"); // モード入りメッセージ
  38.   //lcd.printByte(0xc8); lcd.printByte(0xba); lcd.printByte(0xa6); lcd.printByte(0xd5); lcd.printByte(0xd9); lcd.printByte(0xbc);
  39.   lcd.printByte(0xbc); lcd.printByte(0xde); lcd.printByte(0xba); lcd.printByte(0xb8); //時刻設定モードカタカナ表示
  40.   lcd.printByte(0xcd); lcd.printByte(0xdd); lcd.printByte(0xba); lcd.printByte(0xb3);
  41.   while (digitalRead(entB) == LOW) { // Clock EnterスイッチがOFFになるまで待つ
  42.   }
  43.   delay(30);
  44.  
  45.   lcd.setCursor(0, 0);
  46.  
  47.   lcd.print("SEL "); lcd.printByte(0xbd); lcd.printByte(0xb3); lcd.printByte(0xc1);
  48.   lcd.print(" ENT "); lcd.printByte(0xc2); lcd.printByte(0xb7); lcd.printByte(0xde); lcd.printByte(0xcd);
  49.   lcd.setCursor(0, 1);
  50.   sprintf(rtcDate, "%4d/%02d/%02d %02d:%02d:%02d",
  51.           year(), month(), day(), hour(), minute(), second());
  52.  
  53.   lcd.print(rtcDate);
  54.   //Serial.println(rtcDate);
  55.  
  56.  
  57.   //データ位置とmin,max値を指定して数値設定プログラムを呼ぶ
  58.   setV(3, 23, 33); // 年(2023〜2033年まで)
  59.   yy = xx;
  60.   setV(6, 1, 12); // 月
  61.   mon = xx;
  62.   // 日は月の大小と閏年を反映して呼ぶ
  63.   nen = (rtcDate[2] & 0x0f) * 10 + (rtcDate[3] & 0x0f);
  64.   tsuki = (rtcDate[5] & 0x0f) * 10 + (rtcDate[6] & 0x0f);
  65.  
  66.   dayCount = 31;
  67.   if (tsuki == 4 | tsuki == 6 | tsuki == 9 | tsuki == 11) {
  68.     dayCount = 30;
  69.   }
  70.   if (tsuki == 2) { // 2月の処理
  71.     dayCount = 28;
  72.     if ((nen % 4) == 0) {
  73.       dayCount = 29; // うるう年
  74.     }
  75.   }
  76.   setV(9, 1, dayCount); // 日
  77.   dd = xx;
  78.   setV(12, 0, 23); // 時
  79.   hh = xx;
  80.   setV(15, 0, 59); // 分
  81.   mm = xx;
  82.   lcd.noCursor();
  83.   updateClock(); // RTCに日時を設定
  84. }
  85.  
  86. // set ボタンが押される毎にrtcDateの指定位置の値をインクリメント、液晶を更新
  87. // ent ボタンが押されたら戻る
  88. void setV(int p, int minV, int maxV) { // p:ポインタ、minV:最小値、maxV:最大値
  89.   int yy;
  90.   int mon;
  91.   int dd;
  92.   int hh;
  93.   int mim;
  94.  
  95.   //int xx;
  96.   char upper;
  97.   char lower;
  98.   lcd.setCursor(p, 1);// 液晶のカーソルを移動
  99.   lcd.cursor();// カーソル表示
  100.   //Serial.print("p = ");
  101.   //Serial.println(p);
  102.  
  103.   xx = (rtcDate[p - 1] & 0x0f) * 10 + (rtcDate[p] & 0x0f);
  104.  
  105.  
  106.   while (digitalRead(entB) == LOW) { // entBがOFFになるまで待つ
  107.     delay(30);
  108.   }
  109.   //Serial.println("push");
  110.  
  111.   while (digitalRead(entB) == HIGH) { // entBがNOでなければ以下の処理行う
  112.     lcd.setCursor(p, 1);
  113.     lcd.cursor();// カーソル表示
  114.     unsigned char result = rotary.process();
  115.     if (result == DIR_NONE) {
  116.       // do nothing
  117.     }
  118.     else if (result == DIR_CW) { //エンコーダー正転
  119.       xx++;
  120.     }
  121.     else if (result == DIR_CCW) { //エンコーダー反転
  122.       xx--;
  123.     }
  124.     upper = (xx / 10) | 0x30; // 上位をASCIIへ変換
  125.     lower = ( xx % 10) | 0x30; // 下位をASCIIへ変換
  126.     lcd.setCursor(p - 1, 1 );
  127.     lcd.print(upper); // 表示更新
  128.     lcd.print(lower);
  129.     if (xx >= maxV + 1) { // 上限超えてたらゼロに戻す
  130.       xx = minV;
  131.     }
  132.  
  133.     if (xx <= minV - 1) { // 上限超えてたらゼロに戻す
  134.       xx = maxV;
  135.     }
  136.   
  137.  
  138. }
  139. delay(30);
  140. Serial.println(xx);
  141. //upper = (xx / 10) | 0x30; // 上位をASCIIへ変換
  142. //lower = ( xx % 10) | 0x30; // 下位をASCIIへ変換
  143. //lcd.setCursor(p - 1, 1 );
  144. //lcd.print(upper); // 表示更新
  145. //lcd.print(lower);
  146.  
  147.  
  148. rtcDate[p - 1] = upper; // データ文字列更新
  149. rtcDate[p] = lower;
  150.  
  151.  
  152. while (digitalRead(selB) == LOW) { // selBが離されるまで待つ
  153. }
  154.  
  155. delay(30);
  156.  
  157.  
  158. //delay(30);
  159. //lcd.noCursor(); // 液晶のカーソルを消す
  160. }
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. void setup() {
  168.   Serial.begin(115200);
  169.   myRTC.begin();
  170.   rotary.begin(true);//エンコーダーセット
  171.  
  172.   setSyncProvider(myRTC.get); // the function to get the time from the RTC
  173.   if (timeStatus() != timeSet)
  174.     Serial.println("Unable to sync with the RTC");
  175.   else
  176.     Serial.println("RTC has set the system time");
  177.   lcd.init();
  178.   lcd.backlight();
  179.   digitalWrite(6, HIGH);
  180.   Serial.print("ready");
  181.   // オープンドレインなのでプルアップ pinMode(13, OUTPUT); // 動作表示LED
  182.   pinMode(entB, INPUT); // ClockEnterボタン
  183.   pinMode(selB, INPUT); // ClockSelectボタン
  184.   pinMode(selB, INPUT); // ClockSelectボタン
  185.   digitalWrite(entB, HIGH); // プルアップ
  186.   digitalWrite(selB, HIGH); // プルアップ
  187.   delay(50);
  188.   lcd.clear();
  189.  
  190.   if (digitalRead(entB) == LOW) {// Enterボタンが押されていたら
  191.     clockSet(); // RTCの時刻を合わせる
  192.   }
  193. }
  194.  
  195.  
  196.  
  197.  
  198. void loop() {
  199.  
  200.   sprintf(rtcDate, "%4d/%02d/%02d %02d:%02d:%02d",
  201.           year(), month(), day(), hour(), minute(), second());
  202.   //年月日時分を年以外二桁表示
  203.   lcd.setCursor(0, 0);
  204.   lcd.print(rtcDate);
  205.   //lcd.print(hour());lcd.print(":");lcd.print(minute());
  206.   delay(500);
  207.   lcd.setCursor(13, 0); //セミコロンがフリッカして何となく時計が動いている雰囲気
  208.   lcd.print(" ");
  209.   delay(500);
  210. }

 

エンコーダーのつまみを押し込んだ状態で電源ONあるいはリセットさせると、時刻変更のサブルーチンに入る。

カーソルが年の一番最後の桁に表示されるが、エンコーダーを回すと増減する。

設定最大値を超えると、設定最小値の数値となる。逆転させても同様。分表示なんかは0から59まで有るからちまちま増方向でタクトスイッチ押すより楽だとおもう。

動画を撮影。

 

 

 

頑張って自分で作ったように書いているが、全部ネット上の情報の継ぎ接ぎである。

ネットで公開してくださる皆さん、ありがとうございます。

ライブラリを覗いたり、データシートを読むのはまだまだこれから。初心者のみなさん頑張りましょう。

 

  宿題

キャラクター液晶モジュールではなくグラフィック液晶モジュールを使用してみようと思う。せっかくなんでアナログ時計で。

 

 

御来訪いただき有難うございます 

皆様には余り縁の無い話ですが、胃がん癌治療中でもうすぐ手術予定です。

ステージ4なので手術後の生存率はデータ上では癌の中でも良くないです。

糖尿病を併発してやれやれな感じですが、時間の許す限りものづくりしてみたいです。

癌治療中がどんな感じなのかはこちら