0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

前の記事

LEDチカ編

OLED仕様編

使うまで

下準備(OLED表示用:u8g2ライブラリ)
1.Arudion IDE準備[u8g2ライブラリ]
スケッチ→ライブラリをインクルード→ライブラリを管理
ライブラリマネージャで、U8g2を検索
U8g2 by oliverをインストール
事前情報
項目 内容
利用ライブラリ U8g2lib
利用ライブラリURL https://github.com/olikraus/u8g2
利用デバイス U8G2_SSD1306_128X64_NONAME_F_HW_I2C
スクリーン方向 U8G2_R1(90度回転)
デバイスバッファサイズ(幅) 64
デバイスバッファサイズ(高) 132
スクリーンサイズ(幅) 40
スクリーンサイズ(高) 72
日本語 利用しない
フォント u8g2_font_fub20_tr

Arudiono IDEで使う

OLEDで90°回転させて表示する

スケッチ

#include <Wire.h>
#include <U8g2lib.h>

//  128×64ドット
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R1, U8X8_PIN_NONE, 6, 5); //  U8G2_R0:0度, U8G2_R1:90度

// SSD1306 コントローラ バッファ:132x64
const unsigned int bufferWidth  = 64;   //  90度回転
const unsigned int bufferHeight = 132;  //  90度回転
const unsigned int screenWidth  = 40;   //  90度回転
const unsigned int screenHeight = 72;   //  90度回転

const unsigned int xOffset = 4;
const unsigned int yOffset = (bufferHeight - screenHeight) + 4;

const unsigned int xStringOffset = xOffset + (screenWidth/2) + 6;
const unsigned int yStringOffset1stRow = yOffset - 9;                   //  枠1ドット×2+余裕4ドット×2(上下) ※-9を-12にすると上側の余裕3ドット
const unsigned int yStringOffset2ndRow = yOffset - 6 + screenHeight/2;  //  枠1ドット×2+余裕4ドット×2(上下) ※フォントサイズに合わせて、-3だと余裕1ドット。-6にすると下の余裕4ドット

//  HC-SR04
const byte CH1_TRIG_PIN = 3;
const byte CH1_ECHO_PIN = 4;
const double convRate = 0.017;  //  マイクロ秒(μs)単位の往復時間からセンチメートル(cm)単位の片道距離を計算するための変換係数(約 0.017 cm/μs、音速:約340メートル/秒)
const double maxValue = 400;    //  最大値:400cm
const double minValue = 1;      //  最小値:1cm

//  HC-SR04用値外部変数

//  定周期(ms)
const unsigned int loopTerm_ms = 100;

///////////////////////////////////////////////////////
//  初期化(まとめ)※プロトタイプ宣言は不要
///////////////////////////////////////////////////////
void setup(void) {
  delay(1000);

  //  U8g2ライブラリ初期化
  initilizeOLEDU8g2();

  //  HC-SR04設定
  initilizeHCSR04(CH1_TRIG_PIN, CH1_ECHO_PIN);
}

///////////////////////////////////////////////////////
//  メインループ(まとめ)※プロトタイプ宣言は不要
///////////////////////////////////////////////////////
void loop(void) {
  double ch1_distance = 0;

  delay(loopTerm_ms);

  //  HC-SR04
  ch1_distance = measureHCSR04(CH1_TRIG_PIN, CH1_ECHO_PIN);

  //  U8g2ライブラリ描画
  drawOLEDU8g2(ch1_distance);
}

///////////////////////////////////////////////////////
//  U8g2ライブラリ初期化
///////////////////////////////////////////////////////
void initilizeOLEDU8g2(void)
{
  u8g2.begin();
  u8g2.setContrast(250);
  u8g2.setBusClock(400000);

  u8g2.enableUTF8Print();                   //  UTF8を有効化 ※日本語表示のみ必要
  u8g2.setFont(u8g2_font_fub20_tr);         //  日本語は不要なので、大きめフォントを指定
  u8g2.setDrawColor(1);                     //  文字色を白に設定
}

///////////////////////////////////////////////////////
//  U8g2ライブラリ描画
///////////////////////////////////////////////////////
void drawOLEDU8g2(double distance)
{
  //  枠を指定
  u8g2.drawFrame(xOffset + screenWidth / 2, yOffset - screenHeight / 2,  screenWidth, screenHeight);

  //  cmを書く
  //  文字を書く位置を指定
  u8g2.setCursor(xStringOffset, yStringOffset1stRow);
  if (distance > maxValue) {
    u8g2.printf("--");
  } else if (distance < minValue) {
    u8g2.printf("--");
  } else {
   u8g2.printf("%2d", int(distance));
  }

  //  mmを書く
  //  文字を書く位置を指定
  u8g2.setCursor(xStringOffset, yStringOffset2ndRow);
  if (distance > maxValue) {
    u8g2.printf(".-");
  } else if (distance < minValue) {
    u8g2.printf(".-");
  } else {
   u8g2.printf(".%1d", int(distance * 10) % 10);
  }

  u8g2.sendBuffer();
}

///////////////////////////////////////////////////////
//  HC-SR04初期化
///////////////////////////////////////////////////////
void initilizeHCSR04(byte triggerPin, byte echoPin)
{
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

///////////////////////////////////////////////////////
//  HC-SR04測定
///////////////////////////////////////////////////////
double measureHCSR04(byte triggerPin, byte echoPin)
{
  double ch_distance;
  double pulseInTime;
  double calValue = 1;

  delay(loopTerm_ms);

  //  HC-SR04
  digitalWrite(triggerPin, HIGH);
  delay(1);
  digitalWrite(triggerPin, LOW);
  pulseInTime = pulseIn(echoPin, HIGH);
  ch_distance = convRate * pulseInTime * calValue;

  return ch_distance;
}

※ついでの関数化も実施

90度回転 : 5行目

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R1, U8X8_PIN_NONE, 6, 5); //  U8G2_R0:0度, U8G2_R1:90度

U8G2_R1を指定する

最後に

スケッチコードはCC BY-SA 4.0(著作者の情報とCCライセンス継承はお願いします。商用利用・改変・再配布は問題なし)です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?